fabi0

Untitled

May 27th, 2014
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 35.43 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Nomad MIME Mail Copyright (C) 2003-2008 Alejandro Garcia Gonzalez
  4.  *
  5.  * This library is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU Lesser General Public License as
  7.  * published by the Free Software Foundation; either version 2.1 of the
  8.  * License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13.  * GNU Lesser General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Lesser General Public
  16.  * License along with this library; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18.  */
  19.  
  20. /**
  21.  * Nomad MIME Mail (formerly known as Nexus MIME Mail)
  22.  *
  23.  * a class for handling and sending mail MIME type, with support for
  24.  * the dispatch by SMTP and SMTP Auth.
  25.  *
  26.  *    * Plain Text
  27.  *    * HTML
  28.  *    * Plain Text with Attachments
  29.  *    * HTML with Attachments
  30.  *    * HTML with Embedded Images
  31.  *    * HTML with Embedded Images and Attachments
  32.  *    * Send email messages via SMTP and Auth SMTP
  33.  *
  34.  * @author          Alejandro Garcia Gonzalez <nexus at developarts dot com>
  35.  * @package         nomad_mimemail
  36.  * @version         1.6.2
  37.  * @link            http://www.developarts.com/nomad_mimemail
  38.  * @link            http://www.phpclasses.org/browse/package/1267.html
  39.  * @copyright       Copyright (c) 2008, Alejandro Garcia Gonzalez
  40.  * @license         http://www.opensource.org/licenses/lgpl-license.html GNU Lesser General Public License (LGPL)
  41.  */
  42. class nomad_mimemail
  43. {
  44.   /**
  45.      * Debug Status set how show the errors. "yes" by default.
  46.      * If "yes" show a line with error and continue
  47.      * If "no" Don't show anything and continue
  48.      * If "halt" show a line whit error and stop script
  49.      * @see _debug()
  50.      * @var string yes|no|halt
  51.      * @access private
  52.      */
  53.     var $debug_status   = "yes";
  54.  
  55.     /**
  56.      * The charser of MIME construction. "ISO-8859-1" by default
  57.      * @see function set_charset()
  58.      * @var string
  59.      * @access private
  60.      */
  61.     var $charset        = "ISO-8859-1";
  62.  
  63.     /**
  64.      * Subject text. "No subject" by default
  65.      * @see function set_subject()
  66.      * @var string
  67.      * @access private
  68.      */
  69.     var $mail_subject   = "No subject";
  70.  
  71.     /**
  72.      * Email sender. "Anonymous <noreply@fake.com>" by default
  73.      * @see function set_from()
  74.      * @var string
  75.      * @access private
  76.      */
  77.     var $mail_from      = "Anonymous <noreply@fake.com>";
  78.  
  79.     /**
  80.      * Collection of recipients email address separated by comma
  81.      * @see function set_to()
  82.      * @see function add_to()
  83.      * @var string
  84.      * @todo must be an array
  85.      * @access private
  86.      */
  87.     var $mail_to;
  88.  
  89.     /**
  90.      * Collection of carbon copy recipients email address separated by comma
  91.      * @see function set_cc()
  92.      * @see function add_cc()
  93.      * @var string
  94.      * @todo must be an array
  95.      * @access private
  96.      */
  97.     var $mail_cc;
  98.  
  99.     /**
  100.      * Collection of bind carbon copy recipients email address separated by comma
  101.      * @see function set_bcc()
  102.      * @see function add_bcc()
  103.      * @var string
  104.      * @todo must be an array
  105.      * @access private
  106.      */
  107.     var $mail_bcc;
  108.  
  109.     /**
  110.      * The plain text message of email
  111.      * @see function set_text()
  112.      * @var string
  113.      * @access private
  114.      */
  115.     var $mail_text;
  116.  
  117.     /**
  118.      * The HTML message of email
  119.      * @see function set_html()
  120.      * @var string
  121.      * @access private
  122.      */
  123.     var $mail_html;
  124.  
  125.     /**
  126.      * Numeric identifier based in elements of email
  127.      * @see _parse_elements()
  128.      * @var int
  129.      * @access private
  130.      */
  131.     var $mail_type;
  132.  
  133.     /**
  134.      * Header construction of email
  135.      * @see function _build_header
  136.      * @var string
  137.      * @access private
  138.      */
  139.     var $mail_header;
  140.  
  141.     /**
  142.      * Body construction of email
  143.      * @see function _build_body
  144.      * @var string
  145.      * @access private
  146.      */
  147.     var $mail_body;
  148.  
  149.     /**
  150.      * The reply email address
  151.      * @see function set_reply_to
  152.      * @var string
  153.      * @access private
  154.      */
  155.     var $mail_reply_to;
  156.  
  157.     /**
  158.      * The devilvery error return email address
  159.      * @see function set_return_path
  160.      * @var string
  161.      * @access private
  162.      */
  163.     var $mail_return_path;
  164.  
  165.     /**
  166.      * Attachments Index
  167.      * @see function add_attachment()
  168.      * @var int
  169.      * @access private
  170.      */
  171.     var $attachments_index;
  172.  
  173.     /**
  174.      * Mixed Attachments data
  175.      * @see function add_attachment()
  176.      * @var array
  177.      * @access private
  178.      */
  179.     var $attachments = array();
  180.  
  181.     /**
  182.      * Mixed Attachments images data
  183.      * @see function add_attachment()
  184.      * @var array
  185.      * @access private
  186.      */
  187.     var $attachments_img = array();
  188.  
  189.     /**
  190.      * Boundary Mixed Hash
  191.      * @see function nomad_mimemail()
  192.      * @var string
  193.      * @access private
  194.      */
  195.     var $boundary_mix;
  196.  
  197.     /**
  198.      * Boundary Related Hash
  199.      * @see function nomad_mimemail()
  200.      * @var string
  201.      * @access private
  202.      */
  203.     var $boundary_rel;
  204.  
  205.     /**
  206.      * Boundary Alternative Hash
  207.      * @see function nomad_mimemail()
  208.      * @var string
  209.      * @access private
  210.      */
  211.     var $boundary_alt;
  212.  
  213.     /**
  214.      * Mark if mail has been sent
  215.      * @see function send()
  216.      * @var init
  217.      * @access private
  218.      */
  219.     var $sended_index;
  220.  
  221.     /**
  222.      * SMTP connection pointer
  223.      * @see function _open_smtp_conn()
  224.      * @var resource
  225.      * @access private
  226.      */
  227.     var $smtp_conn;
  228.  
  229.     /**
  230.      * SMTP host name or IP
  231.      * @see function set_smtp_host()
  232.      * @var string
  233.      * @access private
  234.      */
  235.     var $smtp_host;
  236.  
  237.     /**
  238.      * SMTP host access port
  239.      * @see function set_smtp_host()
  240.      * @var int
  241.      * @access private
  242.      */
  243.     var $smtp_port;
  244.  
  245.     /**
  246.      * SMTP Username
  247.      * @see function set_smtp_auth()
  248.      * @var string
  249.      * @access private
  250.      */
  251.     var $smtp_user;
  252.  
  253.     /**
  254.      * SMTP Password
  255.      * @see function set_smtp_auth()
  256.      * @var string
  257.      * @access private
  258.      */
  259.     var $smtp_pass;
  260.  
  261.     /**
  262.      * SMTP log
  263.      * @see function set_smtp_log()
  264.      * @var bool
  265.      * @access private
  266.      */
  267.     var $smtp_log = false;
  268.  
  269.     /**
  270.      * SMTP log messages text
  271.      * @see function get_smtp_log()
  272.      * @var string
  273.      * @access private
  274.      */
  275.     var $smtp_msg;
  276.  
  277.     /**
  278.      * Error string array
  279.      * @see function _debug()
  280.      * @var array
  281.      * @access private
  282.      */
  283.     var $error_msg = array(
  284.             1   =>  'Mail was not sent',
  285.             2   =>  'Body Build Incomplete',
  286.             3   =>  'Need a mail recipient in mail_to',
  287.             4   =>  'No valid Email Address: ',
  288.             5   =>  'Could not Open File',
  289.             6   =>  'Could not connect to SMTP server.',
  290.             7   =>  'Unespected SMTP answer: '
  291.     );
  292.  
  293.     /**
  294.      * Support MIME types
  295.      * @see _get_mimetype()
  296.      * @var array
  297.      * @access private
  298.      */
  299.     var $mime_types = array(
  300.             'gif'   => 'image/gif',
  301.             'jpg'   => 'image/jpeg',
  302.             'jpeg'  => 'image/jpeg',
  303.             'jpe'   => 'image/jpeg',
  304.             'bmp'   => 'image/bmp',
  305.             'png'   => 'image/png',
  306.             'tif'   => 'image/tiff',
  307.             'tiff'  => 'image/tiff',
  308.             'swf'   => 'application/x-shockwave-flash',
  309.             'doc'   => 'application/msword',
  310.             'xls'   => 'application/vnd.ms-excel',
  311.             'ppt'   => 'application/vnd.ms-powerpoint',
  312.             'pdf'   => 'application/pdf',
  313.             'ps'    => 'application/postscript',
  314.             'eps'   => 'application/postscript',
  315.             'rtf'   => 'application/rtf',
  316.             'bz2'   => 'application/x-bzip2',
  317.             'gz'    => 'application/x-gzip',
  318.             'tgz'   => 'application/x-gzip',
  319.             'tar'   => 'application/x-tar',
  320.             'zip'   => 'application/zip',
  321.             'html'  => 'text/html',
  322.             'htm'   => 'text/html',
  323.             'txt'   => 'text/plain',
  324.             'css'   => 'text/css',
  325.             'js'    => 'text/javascript'
  326.     );
  327.  
  328.  
  329.     /**
  330.      * Constructor
  331.      * void nomad_mimemail()
  332.      */
  333.     function nomad_mimemail()
  334.     {
  335.         $this->boundary_mix         = "=-nxs_mix_" . md5(uniqid(rand()));
  336.         $this->boundary_rel         = "=-nxs_rel_" . md5(uniqid(rand()));
  337.         $this->boundary_alt         = "=-nxs_alt_" . md5(uniqid(rand()));
  338.         $this->attachments_index    = 0;
  339.         $this->sended_index         = 0;
  340.  
  341.         // Line Break BR
  342.         if(!defined('BR')){
  343.             define('BR', "\r\n", TRUE);
  344.         }
  345.     }
  346.  
  347.  
  348.     /**
  349.      * void set_from(string mail_from, [string name])
  350.      * Set the "from" email address. "Anonymous <fake@mail.com>" by default
  351.      * @access public
  352.      * @param string mail_from The email from address
  353.      * @param string name Optional name contact
  354.      * @return void
  355.      */
  356.     function set_from($mail_from, $name = "")
  357.     {
  358.         if ($this->_validate_mail($mail_from)){
  359.             $this->mail_from = !empty($name) ? "$name <$mail_from>" : $mail_from;
  360.         }
  361.         else {
  362.             $this->mail_from = "Anonymous <noreply@fake.com>";
  363.         }
  364.     }
  365.  
  366.  
  367.     /**
  368.      * bool set_to(string mail_to, [string name])
  369.      * Set the recipient email address
  370.      * @access public
  371.      * @param string mail_to The recipient email address
  372.      * @param string name Optional name contact
  373.      * @return bool
  374.      */
  375.     function set_to($mail_to, $name = "")
  376.     {
  377.         if ($this->_validate_mail($mail_to)){
  378.             $this->mail_to = !empty($name) ? "$name <$mail_to>" : $mail_to;
  379.             return true;
  380.         }
  381.         return false;
  382.     }
  383.  
  384.  
  385.     /**
  386.      * bool set_cc(string mail_cc, [string name])
  387.      * Set the carbon copy recipient email address
  388.      * @access public
  389.      * @param string mail_cc The carbon copy recipient email address
  390.      * @param string name Optional name contact
  391.      * @return bool
  392.      */
  393.     function set_cc($mail_cc, $name = "")
  394.     {
  395.         if ($this->_validate_mail($mail_cc)){
  396.             $this->mail_cc = !empty($name) ? "$name <$mail_cc>" : $mail_cc;
  397.             return true;
  398.         }
  399.         return false;
  400.     }
  401.  
  402.  
  403.     /**
  404.      * bool set_bcc(string mail_bcc, [string name])
  405.      * Set the blind carbon copy recipient email address
  406.      * @access public
  407.      * @param string mail_bcc The blind carbon copy recipient email address
  408.      * @param string name Optional name contact
  409.      * @return bool
  410.      */
  411.     function set_bcc($mail_bcc, $name = "")
  412.     {
  413.         if ($this->_validate_mail($mail_bcc)){
  414.             $this->mail_bcc = !empty($name) ? "$name <$mail_bcc>" : $mail_bcc;
  415.             return true;
  416.         }
  417.         return false;
  418.     }
  419.  
  420.  
  421.     /**
  422.      * bool set_reply_to(string mail_reply_to, [string name])
  423.      * Set the reply email address. If this var is not set, the reply mail are the "from" email address
  424.      * @access public
  425.      * @param string mail_reply_to The reply email address
  426.      * @param string name Optional name contact
  427.      * @return bool
  428.      */
  429.     function set_reply_to($mail_reply_to, $name = "")
  430.     {
  431.         if ($this->_validate_mail($mail_reply_to)){
  432.             $this->mail_reply_to = !empty($name) ? "$name <$mail_reply_to>" : $mail_reply_to;
  433.             return true;
  434.         }
  435.         return false;
  436.     }
  437.  
  438.  
  439.     /**
  440.      * bool add_to(string mail_to, [string name])
  441.      * Set or add a new recipient email address
  442.      * @access public
  443.      * @param string mail_to The recipient email address
  444.      * @param string name Optional name contact
  445.      * @return bool
  446.      */
  447.     function add_to($mail_to, $name = "")
  448.     {
  449.         if ($this->_validate_mail($mail_to)){
  450.             $mail_to = !empty($name) ? "$name <$mail_to>" : $mail_to;
  451.             $this->mail_to = !empty($this->mail_to) ? $this->mail_to . ", " . $mail_to : $mail_to;
  452.             return true;
  453.         }
  454.         return false;
  455.     }
  456.  
  457.  
  458.     /**
  459.      * bool add_cc(string mail_cc, [string name])
  460.      * Set or add a new carbon copy recipient email address
  461.      * @access public
  462.      * @param string mail_cc The carbon copy recipient email address
  463.      * @param string name Optional name contact
  464.      * @return bool
  465.      */
  466.     function add_cc($mail_cc, $name = "")
  467.     {
  468.         if ($this->_validate_mail($mail_cc)){
  469.             $mail_cc = !empty($name) ? "$name <$mail_cc>" : $mail_cc;
  470.             $this->mail_cc = !empty($this->mail_cc) ? $this->mail_cc . ", " . $mail_cc : $mail_cc;
  471.             return true;
  472.         }
  473.         return false;
  474.     }
  475.  
  476.  
  477.     /**
  478.      * bool add_bcc(string mail_bcc, [string name])
  479.      * Set or add a new blind carbon copy recipient email address
  480.      * @access public
  481.      * @param string mail_bcc The blind carbon copy recipient email address
  482.      * @param string name Optional name contact
  483.      * @return bool
  484.      */
  485.     function add_bcc($mail_bcc, $name = "")
  486.     {
  487.         if ($this->_validate_mail($mail_bcc)){
  488.             $mail_bcc = !empty($name) ? "$name <$mail_bcc>" : $mail_bcc;
  489.             $this->mail_bcc = !empty($this->mail_bcc) ? $this->mail_bcc . ", " . $mail_bcc : $mail_bcc;
  490.             return true;
  491.         }
  492.         return false;
  493.     }
  494.  
  495.  
  496.     /**
  497.      * bool add_reply_to(string mail_reply_to, [string name])
  498.      * Set or add a new reply email address. If this var is not set, the reply mail are the "from" email address
  499.      * @access public
  500.      * @param string mail_reply_to The reply email address
  501.      * @param string name Optional name contact
  502.      * @return bool
  503.      */
  504.     function add_reply_to($mail_reply_to, $name = "")
  505.     {
  506.         if ($this->_validate_mail($mail_reply_to)){
  507.             $mail_reply_to = !empty($name) ? "$name <$mail_reply_to>" : $mail_reply_to;
  508.             $this->mail_reply_to = !empty($this->mail_reply_to) ? $this->mail_reply_to . ", " . $mail_reply_to : $mail_reply_to;
  509.             return true;
  510.         }
  511.         return false;
  512.     }
  513.  
  514.  
  515.     /**
  516.      * bool set_return_path(string mail_return_path)
  517.      * Set the devilvery error return email address
  518.      * @access public
  519.      * @param string mail_return_path The delivery error email account
  520.      * @return bool
  521.      */
  522.     function set_return_path($mail_return_path)
  523.     {
  524.         if ($this->_validate_mail($mail_return_path)){
  525.             $this->mail_return_path = $mail_return_path;
  526.             return true;
  527.         }
  528.         return false;
  529.     }
  530.  
  531.  
  532.     /**
  533.      * void set_subject(string subject)
  534.      * Set the email subject string. "No subject" by default
  535.      * @access public
  536.      * @param string subject
  537.      * @return void
  538.      */
  539.     function set_subject($subject)
  540.     {
  541.         $this->mail_subject = !empty($subject) ? trim($subject) : "No subject";
  542.     }
  543.  
  544.  
  545.     /**
  546.      * void set_text(string text)
  547.      * Set the plain text message in body of email
  548.      * @access public
  549.      * @param string text The plain text message
  550.      * @return void
  551.      */
  552.     function set_text($text)
  553.     {
  554.         if (!empty($text)){
  555.             $this->mail_text = preg_replace("(\r\n|\r|\n)", BR, $text);
  556.         }
  557.     }
  558.  
  559.  
  560.     /**
  561.      * void set_html(string html)
  562.      * Set the HTML message in body of email
  563.      * @access public
  564.      * @param string html The HTML message
  565.      * @return void
  566.      */
  567.     function set_html($html)
  568.     {
  569.         if (!empty($html)){
  570.             $this->mail_html = preg_replace("(\r\n|\r|\n)", BR, $html);
  571.         }
  572.     }
  573.  
  574.  
  575.     /**
  576.      * void set_charset(string charset)
  577.      * Set the charset if email
  578.      * @access public
  579.      * @param string charset The CharSet
  580.      * @return void
  581.      */
  582.     function set_charset($charset)
  583.     {
  584.         if (!empty($charset)){
  585.             $this->charset = $charset;
  586.         }
  587.     }
  588.  
  589.  
  590.     /**
  591.      * bool set_smtp_host(string host, [int port])
  592.      * Set the SMTP host and port, if you call this method with valid parameters, the class sends email through SMTP
  593.      * @access public
  594.      * @param string host The Hostname/IP of the SMTP server
  595.      * @param int port Optional, the port to connect to SMTP server
  596.      * @return bool
  597.      */
  598.     function set_smtp_host($host, $port = 25)
  599.     {
  600.         if (!empty($host) && is_numeric($port)){
  601.             $this->smtp_host = $host;
  602.             $this->smtp_port = $port;
  603.             return true;
  604.         }
  605.         return false;
  606.     }
  607.  
  608.  
  609.     /**
  610.      * bool set_smtp_host(string host, [int port])
  611.      * Set the Auth SMTP user and password, you need to call method set_smtp_host before
  612.      * @access public
  613.      * @param string user The Username Authentication account
  614.      * @param string pass The Password Authentication account
  615.      * @return bool
  616.      */
  617.     function set_smtp_auth($user, $pass)
  618.     {
  619.         if(!empty($user) && !empty($pass)){
  620.             $this->smtp_user = $user;
  621.             $this->smtp_pass = $pass;
  622.             return true;
  623.         }
  624.         return false;
  625.     }
  626.  
  627.  
  628.     /**
  629.      * string get_eml()
  630.      * Get the EML format message of the email
  631.      * @access public
  632.      * @return mixed string if message has build, false if not
  633.      */
  634.     function get_eml()
  635.     {
  636.         if ($this->_build_body()){
  637.             return
  638.                 $this->mail_header . BR .
  639.                 'Subject: ' . $this->mail_subject . BR .
  640.                 $this->mail_body;
  641.         }
  642.         return false;
  643.     }
  644.  
  645.  
  646.     /**
  647.      * bool add_attachment(mixed file, string name, [string type])
  648.      * Add a file attachment
  649.      * @access public
  650.      * @param string file
  651.      * @param string name
  652.      * @param string type
  653.      * @return bool
  654.      */
  655.     function add_attachment($file, $name, $type = "")
  656.     {
  657.         if (($content = $this->_open_file($file))){
  658.             $this->attachments[$this->attachments_index] = array(
  659.                 'content' => chunk_split(base64_encode($content), 76, BR),
  660.                 'name' => $name,
  661.                 'type' => (empty($type) ? $this->_get_mimetype($name): $type),
  662.                 'embedded' => false
  663.             );
  664.             $this->attachments_index++;
  665.         }
  666.     }
  667.  
  668.  
  669.     /**
  670.      * bool add_content_attachment(mixed file, string name, [string type])
  671.      * Add a content to an attachment
  672.      * @access public
  673.      * @param string content
  674.      * @param string name
  675.      * @param string type
  676.      * @return bool
  677.      */
  678.     function add_content_attachment($content, $name, $type = "")
  679.     {
  680.         $this->attachments[$this->attachments_index] = array(
  681.             'content' => chunk_split(base64_encode($content), 76, BR),
  682.             'name' => $name,
  683.             'type' => (empty($type) ? $this->_get_mimetype($name): $type),
  684.             'embedded' => false
  685.         );
  686.         $this->attachments_index++;
  687.     }
  688.  
  689.  
  690.     /**
  691.      * void new_mail([mixed from], [mixed to], [string subject], [string text], [string html])
  692.      * Method shortcut to create an email
  693.      * @access public
  694.      * @return void
  695.      */
  696.     function new_mail($from = "", $to = "", $subject = "", $text = "", $html = "")
  697.     {
  698.         // First, clear all vars
  699.         $this->mail_subject = "";
  700.         $this->mail_from = "";
  701.         $this->mail_to = "";
  702.         $this->mail_cc = "";
  703.         $this->mail_bcc = "";
  704.         $this->mail_text = "";
  705.         $this->mail_html = "";
  706.         $this->mail_header = "";
  707.         $this->mail_body = "";
  708.         $this->mail_reply_to = "";
  709.         $this->mail_return_path = "";
  710.         $this->attachments_index = 0;
  711.         $this->sended_index = 0;
  712.  
  713.         // Clear Array Vars
  714.         $this->attachments = array();
  715.         $this->attachments_img = array();
  716.  
  717.         // Asign vars
  718.         if (is_array($from)){
  719.             $this->set_from($from[0],$from[1]);
  720.             $this->set_return_path($from[0]);
  721.         }
  722.         else {
  723.             $this->set_from($from);
  724.             $this->set_return_path($from);
  725.         }
  726.  
  727.         if (is_array($to)){
  728.             $this->set_to($to[0],$to[1]);
  729.         }
  730.         else {
  731.             $this->set_to($to);
  732.         }
  733.  
  734.         $this->set_subject($subject);
  735.         $this->set_text($text);
  736.         $this->set_html($html);
  737.     }
  738.  
  739.  
  740.     /**
  741.      * bool send()
  742.      * Send the email message
  743.      * @access public
  744.      * @return bool
  745.      */
  746.     function send()
  747.     {
  748.         if ($this->sended_index == 0 && !$this->_build_body()){
  749.             $this->_debug(1);
  750.             return false;
  751.         }
  752.  
  753.         if (empty($this->smtp_host) && !empty($this->mail_return_path) && $this->_php_version_check('4.0.5') && !($this->_php_version_check('4.2.3') && ini_get('safe_mode'))){
  754.             return mail($this->mail_to, $this->mail_subject, $this->mail_body, $this->mail_header, '-f'.$this->mail_return_path);
  755.         }
  756.         elseif (empty($this->smtp_host)) {
  757.             return mail($this->mail_to, $this->mail_subject, $this->mail_body, $this->mail_header);
  758.         }
  759.         elseif (!empty($this->smtp_host)){
  760.             return $this->_smtp_send();
  761.         }
  762.         else {
  763.             return false;
  764.         }
  765.     }
  766.  
  767.  
  768.     /**
  769.      * void _build_header()
  770.      * Build all the headers of email
  771.      * @access private
  772.      * @param text content_type The Content Type of email
  773.      * @return void
  774.      */
  775.     function _build_header($content_type)
  776.     {
  777.         $this->mail_header = "";
  778.         if (!empty($this->smtp_host)){
  779.             $this->mail_header .= "Subject: " . $this->mail_subject . BR;
  780.         }
  781.         if (!empty($this->mail_from)){
  782.             $this->mail_header .= "From: " . $this->mail_from . BR;
  783.             $this->mail_header .= !empty($this->mail_reply_to) ? "Reply-To: " . $this->mail_reply_to . BR : "Reply-To: " . $this->mail_from . BR;
  784.         }
  785.         if (!empty($this->mail_to) && !empty($this->smtp_host)){    // FixBug: http://www.developarts.com/version_14_de_nomad_mime_mail#comment-294
  786.             $this->mail_header .= "To: " . $this->mail_to . BR;
  787.         }
  788.         if (!empty($this->mail_cc)){
  789.             $this->mail_header .= "Cc: " . $this->mail_cc . BR;
  790.         }
  791.         if (!empty($this->mail_bcc) && empty($this->smtp_host)){
  792.             $this->mail_header .= "Bcc: " . $this->mail_bcc . BR;
  793.         }
  794.         if (!empty($this->mail_return_path)){
  795.             $this->mail_header .= "Return-Path: " . $this->mail_return_path . BR;
  796.         }
  797.         $this->mail_header .= $content_type . BR;
  798.         if (!empty($this->smtp_host)){
  799.             $this->mail_header .= "Date: " . date("r") . BR;
  800.         }
  801.         $this->mail_header .= "Message-Id: <" . md5(uniqid(rand())) . ".nomad_mimemail@" . $_SERVER['SERVER_ADDR'] . ">" . BR;
  802.         $this->mail_header .= "MIME-Version: 1.0" . BR;
  803.         $this->mail_header .= "X-Mailer: Nomad MIME Mail ". $this->get_version() . BR . BR;
  804.     }
  805.  
  806.  
  807.     /**
  808.      * bool _build_body()
  809.      * Build body email message
  810.      * @access private
  811.      * @return bool
  812.      */
  813.     function _build_body()
  814.     {
  815.         switch ($this->_parse_elements()){
  816.             case 1: // Plain Text
  817.                 $this->_build_header("Content-Type: text/plain; charset=\"$this->charset\"");
  818.                 $this->mail_body = $this->mail_text;
  819.                 break;
  820.             case 3: // Plain Text + HTML
  821.                 $this->_build_header("Content-Type: multipart/alternative; boundary=\"$this->boundary_alt\"");
  822.                 $this->mail_body .= "--" . $this->boundary_alt . BR;
  823.                 $this->mail_body .= "Content-Type: text/plain; charset=\"$this->charset\"" . BR;
  824.                 $this->mail_body .= "Content-Transfer-Encoding: 7bit" . BR . BR;
  825.                 $this->mail_body .= $this->mail_text . BR . BR;
  826.                 $this->mail_body .= "--" . $this->boundary_alt . BR;
  827.                 $this->mail_body .= "Content-Type: text/html; charset=\"$this->charset\"" . BR;
  828.                 $this->mail_body .= "Content-Transfer-Encoding: 7bit" . BR . BR;
  829.                 $this->mail_body .= $this->mail_html . BR;
  830.                 $this->mail_body .= "--" . $this->boundary_alt . "--" . BR;
  831.                 break;
  832.             case 5: // Plain Text + Attachments
  833.                 $this->_build_header("Content-Type: multipart/mixed; boundary=\"$this->boundary_mix\"");
  834.                 $this->mail_body .= "--" . $this->boundary_mix . BR;
  835.                 $this->mail_body .= "Content-Type: text/plain; charset=\"$this->charset\"" . BR;
  836.                 $this->mail_body .= "Content-Transfer-Encoding: 7bit" . BR . BR;
  837.                 $this->mail_body .= $this->mail_text . BR . BR;
  838.                 foreach($this->attachments as $value){
  839.                     $this->mail_body .= "--" . $this->boundary_mix . BR;
  840.                     $this->mail_body .= "Content-Type: " . $value['type'] . "; name=\"" . $value['name'] . "\"" . BR;
  841.                     $this->mail_body .= "Content-Disposition: attachment; filename=\"" . $value['name'] . "\"" . BR;
  842.                     $this->mail_body .= "Content-Transfer-Encoding: base64" . BR . BR;
  843.                     $this->mail_body .= $value['content'] . BR . BR;
  844.                 }
  845.                 $this->mail_body .= "--" . $this->boundary_mix . "--" . BR;
  846.                 break;
  847.             case 7:  // Plain Text + HTML + Attachments
  848.                 $this->_build_header("Content-Type: multipart/mixed; boundary=\"$this->boundary_mix\"");
  849.                 $this->mail_body .= "--" . $this->boundary_mix . BR;
  850.                 $this->mail_body .= "Content-Type: multipart/alternative; boundary=\"$this->boundary_alt\"" . BR . BR;
  851.                 $this->mail_body .= "--" . $this->boundary_alt . BR;
  852.                 $this->mail_body .= "Content-Type: text/plain; charset=\"$this->charset\"" . BR;
  853.                 $this->mail_body .= "Content-Transfer-Encoding: 7bit" . BR . BR;
  854.                 $this->mail_body .= $this->mail_text . BR . BR;
  855.                 $this->mail_body .= "--" . $this->boundary_alt . BR . BR;
  856.                 $this->mail_body .= "Content-Type: text/html; charset=\"$this->charset\"" . BR;
  857.                 $this->mail_body .= "Content-Transfer-Encoding: 7bit" . BR;
  858.                 $this->mail_body .= $this->mail_html . BR . BR;
  859.                 $this->mail_body .= "--" . $this->boundary_alt . "--" . BR . BR;
  860.                 foreach($this->attachments as $value){
  861.                     $this->mail_body .= "--" . $this->boundary_mix . BR;
  862.                     $this->mail_body .= "Content-Type: " . $value['type'] . "; name=\"" . $value['name'] . "\"" . BR;
  863.                     $this->mail_body .= "Content-Disposition: attachment; filename=\"" . $value['name'] . "\"" . BR;
  864.                     $this->mail_body .= "Content-Transfer-Encoding: base64" . BR . BR;
  865.                     $this->mail_body .= $value['content'] . BR . BR;
  866.                 }
  867.                 $this->mail_body .= "--" . $this->boundary_mix . "--" . BR;
  868.                 break;
  869.             case 11: // Plain Text + HTML + Embedded Images
  870.                 $this->_build_header("Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"$this->boundary_rel\"");
  871.                 $this->mail_body .= "--" . $this->boundary_rel . BR;
  872.                 $this->mail_body .= "Content-Type: multipart/alternative; boundary=\"$this->boundary_alt\"" . BR . BR;
  873.                 $this->mail_body .= "--" . $this->boundary_alt . BR;
  874.                 $this->mail_body .= "Content-Type: text/plain; charset=\"$this->charset\"" . BR;
  875.                 $this->mail_body .= "Content-Transfer-Encoding: 7bit" . BR . BR;
  876.                 $this->mail_body .= $this->mail_text . BR . BR;
  877.                 $this->mail_body .= "--" . $this->boundary_alt . BR;
  878.                 $this->mail_body .= "Content-Type: text/html; charset=\"$this->charset\"" . BR;
  879.                 $this->mail_body .= "Content-Transfer-Encoding: 7bit" . BR . BR;
  880.                 $this->mail_body .= $this->mail_html . BR . BR;
  881.                 $this->mail_body .= "--" . $this->boundary_alt . "--" . BR . BR;
  882.                 foreach($this->attachments as $value){
  883.                     if ($value['embedded']){
  884.                         $this->mail_body .= "--" . $this->boundary_rel . BR;
  885.                         $this->mail_body .= "Content-ID: <" . $value['embedded'] . ">" . BR;
  886.                         $this->mail_body .= "Content-Type: " . $value['type'] . "; name=\"" . $value['name'] . "\"" . BR;
  887.                         $this->mail_body .= "Content-Disposition: attachment; filename=\"" . $value['name'] . "\"" . BR;
  888.                         $this->mail_body .= "Content-Transfer-Encoding: base64" . BR . BR;
  889.                         $this->mail_body .= $value['content'] . BR . BR;
  890.                     }
  891.                 }
  892.                 $this->mail_body .= "--" . $this->boundary_rel . "--" . BR;
  893.                 break;
  894.             case 15: // Plain Text + HTML + Embedded Images + Attachments
  895.                 $this->_build_header("Content-Type: multipart/mixed; boundary=\"$this->boundary_mix\"");
  896.                 $this->mail_body .= "--" . $this->boundary_mix . BR;
  897.                 $this->mail_body .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"$this->boundary_rel\"" . BR . BR;
  898.                 $this->mail_body .= "--" . $this->boundary_rel . BR;
  899.                 $this->mail_body .= "Content-Type: multipart/alternative; boundary=\"$this->boundary_alt\"" . BR . BR;
  900.                 $this->mail_body .= "--" . $this->boundary_alt . BR;
  901.                 $this->mail_body .= "Content-Type: text/plain; charset=\"$this->charset\"" . BR;
  902.                 $this->mail_body .= "Content-Transfer-Encoding: 7bit" . BR . BR;
  903.                 $this->mail_body .= $this->mail_text . BR . BR;
  904.                 $this->mail_body .= "--" . $this->boundary_alt . BR;
  905.                 $this->mail_body .= "Content-Type: text/html; charset=\"$this->charset\"" . BR;
  906.                 $this->mail_body .= "Content-Transfer-Encoding: 7bit" . BR . BR;
  907.                 $this->mail_body .= $this->mail_html . BR . BR;
  908.                 $this->mail_body .= "--" . $this->boundary_alt . "--" . BR . BR;
  909.                 foreach($this->attachments as $value){
  910.                     if ($value['embedded']){
  911.                         $this->mail_body .= "--" . $this->boundary_rel . BR;
  912.                         $this->mail_body .= "Content-ID: <" . $value['embedded'] . ">" . BR;
  913.                         $this->mail_body .= "Content-Type: " . $value['type'] . "; name=\"" . $value['name'] . "\"" . BR;
  914.                         $this->mail_body .= "Content-Disposition: attachment; filename=\"" . $value['name'] . "\"" . BR;
  915.                         $this->mail_body .= "Content-Transfer-Encoding: base64" . BR . BR;
  916.                         $this->mail_body .= $value['content'] . BR . BR;
  917.                     }
  918.                 }
  919.                 $this->mail_body .= "--" . $this->boundary_rel . "--" . BR . BR;
  920.                 foreach($this->attachments as $value){
  921.                     if (!$value['embedded']){
  922.                         $this->mail_body .= "--" . $this->boundary_mix . BR;
  923.                         $this->mail_body .= "Content-Type: " . $value['type'] . "; name=\"" . $value['name'] . "\"" . BR;
  924.                         $this->mail_body .= "Content-Disposition: attachment; filename=\"" . $value['name'] . "\"" . BR;
  925.                         $this->mail_body .= "Content-Transfer-Encoding: base64" . BR . BR;
  926.                         $this->mail_body .= $value['content'] . BR . BR;
  927.                     }
  928.                 }
  929.                 $this->mail_body .= "--" . $this->boundary_mix . "--" . BR;
  930.                 break;
  931.             default:
  932.                 return $this->_debug(2);
  933.         }
  934.         $this->sended_index++;
  935.         return true;
  936.     }
  937.  
  938.  
  939.     /**
  940.      * bool _php_version_check(string vercheck)
  941.      * Check if current version of PHP is above than other
  942.      * @access private
  943.      * @param string vercheck The compare version of PHP
  944.      * @return bool
  945.      */
  946.     function _php_version_check($vercheck)
  947.     {
  948.         if (version_compare(PHP_VERSION, $vercheck) === 1){
  949.             return true;
  950.         }
  951.         return false;
  952.     }
  953.  
  954.  
  955.     /**
  956.      * mixed _parse_elements()
  957.      * Check all email message elements and return a identifier
  958.      * @access private
  959.      * @return mixed int|false
  960.      */
  961.     function _parse_elements()
  962.     {
  963.         if (empty($this->mail_to)){
  964.             return $this->_debug(3);
  965.         }
  966.         $this->_search_images();
  967.         $this->mail_type = 0; // None
  968.         if (!empty($this->mail_text)){
  969.             $this->mail_type = $this->mail_type + 1; // Plain Text
  970.         }
  971.         if (!empty($this->mail_html)){
  972.             $this->mail_type = $this->mail_type + 2; // HTML
  973.             if (empty($this->mail_text)){
  974.                 $this->mail_text = strip_tags(eregi_replace("<br>", BR, $this->mail_html));
  975.                 $this->mail_type = $this->mail_type + 1; // Plain Text
  976.             }
  977.         }
  978.         if ($this->attachments_index != 0){
  979.             if (count($this->attachments_img) != 0){
  980.                 $this->mail_type = $this->mail_type + 8; // Embedded Images
  981.             }
  982.             if ((count($this->attachments) - count($this->attachments_img)) >= 1){
  983.                 $this->mail_type = $this->mail_type + 4; // Attachments
  984.             }
  985.         }
  986.         return $this->mail_type;
  987.     }
  988.  
  989.  
  990.     /**
  991.      * void _search_images()
  992.      * Search all embeded images in HTML and attachments
  993.      * @access private
  994.      * @return void
  995.      */
  996.     function _search_images()
  997.     {
  998.         if ($this->attachments_index != 0){
  999.             foreach($this->attachments as $key => $value){
  1000.                 if (preg_match('/(css|image)/i', $value['type']) && preg_match('/\s(background|href|src)\s*=\s*[\"|\'](' . $value['name'] . ')[\"|\'].*>/is', $this->mail_html)) {
  1001.                     $img_id = md5($value['name']) . ".nomad@mimemail";
  1002.                     $this->mail_html = preg_replace('/\s(background|href|src)\s*=\s*[\"|\'](' . $value['name'] . ')[\"|\']/is', ' \\1="cid:' . $img_id . '"', $this->mail_html);
  1003.                     $this->attachments[$key]['embedded'] = $img_id;
  1004.                     $this->attachments_img[] = $value['name'];
  1005.                 }
  1006.             }
  1007.         }
  1008.     }
  1009.  
  1010.  
  1011.     /**
  1012.      * bool _validate_mail(string mail)
  1013.      * Validate an email address
  1014.      * @access private
  1015.      * @param string mail The email address string
  1016.      * @return bool
  1017.      */
  1018.     function _validate_mail($mail)
  1019.     {
  1020.         if (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.'@'.'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',$mail)){
  1021.             return true;
  1022.         }
  1023.         return $this->_debug(4, $mail);
  1024.     }
  1025.  
  1026.  
  1027.     /**
  1028.      * mixed _extract_email(string parse)
  1029.      * Extract all email addresses from a string. If extracted more than one
  1030.      * return an array. If extraded only one email return string. Else return false
  1031.      * @access private
  1032.      * @param string parse String with one or more email addresses
  1033.      * @return mixed array|string|false
  1034.      */
  1035.     function _extract_email($parse)
  1036.     {
  1037.         preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $parse, $matches);
  1038.         if (count($matches[0]) == 1){
  1039.             return $matches[0][0];
  1040.         }
  1041.         elseif (!count($matches[0])){
  1042.             return false;
  1043.         }
  1044.         else {
  1045.             return $matches[0];
  1046.         }
  1047.     }
  1048.  
  1049.  
  1050.     /**
  1051.      * string _get_mimetype(string name)
  1052.      * Search a mime type based in it's extension filename
  1053.      * @access private
  1054.      * @param string name The file name
  1055.      * @return mixed string
  1056.      */
  1057.     function _get_mimetype($name)
  1058.     {
  1059.         $ext_array = explode(".", $name);
  1060.         if (($last = count($ext_array) - 1) != 0){
  1061.             $ext = $ext_array[$last];
  1062.             if (isset($this->mime_types[$ext]))
  1063.                 return $this->mime_types[$ext];
  1064.         }
  1065.         return "application/octet-stream";
  1066.     }
  1067.  
  1068.  
  1069.     /**
  1070.      * mixed _open_file(string file)
  1071.      * Opens a file and returns it's content
  1072.      * @access private
  1073.      * @param string file The file path
  1074.      * @return mixed string|false
  1075.      */
  1076.     function _open_file($file)
  1077.     {
  1078.         if(($fp = @fopen($file, 'r'))){
  1079.             $content = fread($fp, filesize($file));
  1080.             fclose($fp);
  1081.             return $content;
  1082.         }
  1083.         return $this->_debug(5, $file);
  1084.     }
  1085.  
  1086.  
  1087.     /**
  1088.      * bool false _debug(int msg, [string element])
  1089.      * Printa a error and returns false
  1090.      * @access private
  1091.      * @param int msg The id error
  1092.      * @param string element Optional The extra message error
  1093.      * @return bool false
  1094.      */
  1095.     function _debug($msg, $element="")
  1096.     {
  1097.         if ($this->debug_status == "yes"){
  1098.             echo "<br><b>Error:</b> " . $this->error_msg[$msg] . " $element<br>";
  1099.         }
  1100.         elseif ($this->debug_status == "halt"){
  1101.             die ("<br><b>Error:</b> " . $this->error_msg[$msg] . " $element<br>");
  1102.         }
  1103.         return false;
  1104.     }
  1105.  
  1106.  
  1107.     /**
  1108.      * bool _open_smtp_conn()
  1109.      * Opens a socket connection to SMTP server
  1110.      * @access private
  1111.      * @return bool
  1112.      */
  1113.     function _open_smtp_conn()
  1114.     {
  1115.         if ($this->smtp_conn = @fsockopen ($this->smtp_host, $this->smtp_port)){
  1116.             if (in_array($this->_get_smtp_response(), array(220, 250, 354))){
  1117.                 return true;
  1118.             }
  1119.         }
  1120.         return $this->_debug(6);
  1121.     }
  1122.  
  1123.  
  1124.     /**
  1125.      * void _close_smtp_conn()
  1126.      * Close SMTP connection
  1127.      * @access private
  1128.      * @return void
  1129.      */
  1130.     function _close_smtp_conn()
  1131.     {
  1132.         $this->_send_smtp_command("QUIT");
  1133.         @fclose($this->smtp_conn);
  1134.     }
  1135.  
  1136.  
  1137.     /**
  1138.      * bool _send_smtp_command(string command, [array number])
  1139.      * Sends a Command to SMTP server
  1140.      * @access private
  1141.      * @param string command String of Command to send
  1142.      * @param array number Optional array of accepted numbers for response
  1143.      * @return bool
  1144.      */
  1145.     function _send_smtp_command($command, $number="")
  1146.     {
  1147.         if (@fwrite($this->smtp_conn, $command . BR)){
  1148.             $this->smtp_msg .= $this->smtp_log == true ? $command . "\n" : "";
  1149.             if (!empty($number)){
  1150.                 if (!in_array($this->_get_smtp_response(), (array)$number)){
  1151.                     $this->_close_smtp_conn();
  1152.                     return $this->_debug(7);
  1153.                 }
  1154.             }
  1155.             return true;
  1156.         }
  1157.         return false;
  1158.     }
  1159.  
  1160.  
  1161.     /**
  1162.      * int _get_smtp_response()
  1163.      * Check the id number response from SMTP server
  1164.      * @access private
  1165.      * @return int
  1166.      */
  1167.     function _get_smtp_response()
  1168.     {
  1169.         do {
  1170.             $response = chop(@fgets($this->smtp_conn, 1024));
  1171.             $this->smtp_msg .= $this->smtp_log == true ? $response . "\n" : "";
  1172.         } while($response{3} == "-");
  1173.         return intval(substr($response,0,3));
  1174.     }
  1175.  
  1176.  
  1177.     /**
  1178.      * bool _smtp_send()
  1179.      * Sends the email message via SMTP
  1180.      * @access private
  1181.      * @return bool
  1182.      */
  1183.     function _smtp_send()
  1184.     {
  1185.         if ($this->_open_smtp_conn()){
  1186.             if (!$this->_send_smtp_command("helo {$this->smtp_host}", array(220, 250, 354))){return false;}
  1187.             if(!empty($this->smtp_user) && !empty($this->smtp_pass)){
  1188.                 if (!$this->_send_smtp_command("EHLO {$this->smtp_host}", array(220, 250, 354))){return false;}
  1189.                 if (!$this->_send_smtp_command("AUTH LOGIN", array(334))){return false;}
  1190.                 if (!$this->_send_smtp_command(base64_encode($this->smtp_user), array(334))){return false;}
  1191.                 if (!$this->_send_smtp_command(base64_encode($this->smtp_pass), array(235))){return false;}
  1192.             }
  1193.             if (!$this->_send_smtp_command("MAIL FROM:" . $this->_extract_email($this->mail_from), array(220, 250, 354))){return false;}    // FixBug: http://www.developarts.com/version_14_de_nomad_mime_mail#comment-19
  1194.             $all_email = $this->_extract_email(implode(", ", array($this->mail_to, $this->mail_cc, $this->mail_bcc)));
  1195.             foreach ((array)$all_email as $email){
  1196.                 if (!$this->_send_smtp_command("RCPT TO:{$email}", array(220, 250, 354))){return false;}
  1197.             }
  1198.             if (!$this->_send_smtp_command("DATA", array(220, 250, 354))){return false;}
  1199.             $this->_send_smtp_command($this->mail_header);
  1200.             $this->_send_smtp_command($this->mail_body);
  1201.             if (!$this->_send_smtp_command(".", array(220, 250, 354))){return false;}
  1202.             $this->_close_smtp_conn();
  1203.             return true;
  1204.         }
  1205.         return false;
  1206.     }
  1207.  
  1208.  
  1209.     /**
  1210.      * void set_smtp_log(bool log)
  1211.      * Activate or Deactivate SMTP log messages
  1212.      * @access public
  1213.      * @param bool log True if you can log SMTP messages, false by default
  1214.      * @return void
  1215.      */
  1216.     function set_smtp_log($log = false)
  1217.     {
  1218.         if ($log == true){
  1219.             $this->smtp_log = true;
  1220.         }
  1221.         else {
  1222.             $this->smtp_log = false;
  1223.         }
  1224.     }
  1225.  
  1226.  
  1227.     /**
  1228.      * string get_smtp_log()
  1229.      * Get all SMTP log
  1230.      * @access public
  1231.      * @return string
  1232.      */
  1233.     function get_smtp_log()
  1234.     {
  1235.         if ($this->smtp_log == true){
  1236.             return $this->smtp_msg;
  1237.         }
  1238.         else {
  1239.             return "No logs activated";
  1240.         }
  1241.     }
  1242.  
  1243.  
  1244.     /**
  1245.      * string get_version()
  1246.      * Return the version of this class
  1247.      * @access public
  1248.      * @return string
  1249.      */
  1250.     function get_version()
  1251.     {
  1252.         return "1.6.2";
  1253.     }
  1254. }
  1255.  
  1256. ?>
Add Comment
Please, Sign In to add comment