Support Joomla!

Packages

Package: PHPMailer

License

Content on this site is copyright © 2005 - 2008 Open Source Matters Inc and can be used in accordance with the Joomla! Electronic Documentation License. Some parts of this website may be subject to other licenses.
Source code for file /phpmailer/phpmailer.php

Documentation is available at phpmailer.php

  1. <?php
  2. ////////////////////////////////////////////////////
  3. // PHPMailer - PHP email class
  4. //
  5. // Class for sending email using either
  6. // sendmail, PHP mail(), or SMTP.  Methods are
  7. // based upon the standard AspEmail(tm) classes.
  8. //
  9. // Copyright (C) 2001 - 2003  Brent R. Matzelle
  10. //
  11. // License: LGPL, see LICENSE
  12. ////////////////////////////////////////////////////
  13.  
  14. /**
  15.  * PHPMailer - PHP email transport class
  16.  * @package PHPMailer
  17.  * @author Brent R. Matzelle
  18.  * @copyright 2001 - 2003 Brent R. Matzelle
  19.  */
  20. class PHPMailer
  21. {
  22.     /////////////////////////////////////////////////
  23.     // PUBLIC VARIABLES
  24.     /////////////////////////////////////////////////
  25.  
  26.     
  27.     /**
  28.      * Email priority (1 = High, 3 = Normal, 5 = low).
  29.      * @var int 
  30.      */
  31.     var $Priority          = 3;
  32.  
  33.     /**
  34.      * Sets the CharSet of the message.
  35.      * @var string 
  36.      */
  37.     var $CharSet           = "utf-8";
  38.  
  39.     /**
  40.      * Sets the Content-type of the message.
  41.      * @var string 
  42.      */
  43.     var $ContentType        = "text/plain";
  44.  
  45.     /**
  46.      * Sets the Encoding of the message. Options for this are "8bit",
  47.      * "7bit", "binary", "base64", and "quoted-printable".
  48.      * @var string 
  49.      */
  50.     var $Encoding          = "8bit";
  51.  
  52.     /**
  53.      * Holds the most recent mailer error message.
  54.      * @var string 
  55.      */
  56.     var $ErrorInfo         = "";
  57.  
  58.     /**
  59.      * Sets the From email address for the message.
  60.      * @var string 
  61.      */
  62.     var $From               = "root@localhost";
  63.  
  64.     /**
  65.      * Sets the From name of the message.
  66.      * @var string 
  67.      */
  68.     var $FromName           = "Root User";
  69.  
  70.     /**
  71.      * Sets the Sender email (Return-Path) of the message.  If not empty,
  72.      * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
  73.      * @var string 
  74.      */
  75.     var $Sender            = "";
  76.  
  77.     /**
  78.      * Sets the Subject of the message.
  79.      * @var string 
  80.      */
  81.     var $Subject           = "";
  82.  
  83.     /**
  84.      * Sets the Body of the message.  This can be either an HTML or text body.
  85.      * If HTML then run IsHTML(true).
  86.      * @var string 
  87.      */
  88.     var $Body               = "";
  89.  
  90.     /**
  91.      * Sets the text-only body of the message.  This automatically sets the
  92.      * email to multipart/alternative.  This body can be read by mail
  93.      * clients that do not have HTML email capability such as mutt. Clients
  94.      * that can read HTML will view the normal Body.
  95.      * @var string 
  96.      */
  97.     var $AltBody           = "";
  98.  
  99.     /**
  100.      * Sets word wrapping on the body of the message to a given number of
  101.      * characters.
  102.      * @var int 
  103.      */
  104.     var $WordWrap          = 0;
  105.  
  106.     /**
  107.      * Method to send mail: ("mail", "sendmail", or "smtp").
  108.      * @var string 
  109.      */
  110.     var $Mailer            = "mail";
  111.  
  112.     /**
  113.      * Sets the path of the sendmail program.
  114.      * @var string 
  115.      */
  116.     var $Sendmail          = "/usr/sbin/sendmail";
  117.  
  118.     /**
  119.      * Path to PHPMailer plugins.  This is now only useful if the SMTP class
  120.      * is in a different directory than the PHP include path.
  121.      * @var string 
  122.      */
  123.     var $PluginDir         = "";
  124.  
  125.     /**
  126.      *  Holds PHPMailer version.
  127.      *  @var string 
  128.      */
  129.     var $Version           = "1.73";
  130.  
  131.     /**
  132.      * Sets the email address that a reading confirmation will be sent.
  133.      * @var string 
  134.      */
  135.     var $ConfirmReadingTo  = "";
  136.  
  137.     /**
  138.      *  Sets the hostname to use in Message-Id and Received headers
  139.      *  and as default HELO string. If empty, the value returned
  140.      *  by SERVER_NAME is used or 'localhost.localdomain'.
  141.      *  @var string 
  142.      */
  143.     var $Hostname          = "";
  144.  
  145.     /////////////////////////////////////////////////
  146.     // SMTP VARIABLES
  147.     /////////////////////////////////////////////////
  148.  
  149.     
  150.     /**
  151.      *  Sets the SMTP hosts.  All hosts must be separated by a
  152.      *  semicolon.  You can also specify a different port
  153.      *  for each host by using this format: [hostname:port]
  154.      *  (e.g. "smtp1.example.com:25;smtp2.example.com").
  155.      *  Hosts will be tried in order.
  156.      *  @var string 
  157.      */
  158.     var $Host        = "localhost";
  159.  
  160.     /**
  161.      *  Sets the default SMTP server port.
  162.      *  @var int 
  163.      */
  164.     var $Port        = 25;
  165.  
  166.     /**
  167.      *  Sets the SMTP HELO of the message (Default is $Hostname).
  168.      *  @var string 
  169.      */
  170.     var $Helo        = "";
  171.  
  172.     /**
  173.      *  Sets SMTP authentication. Utilizes the Username and Password variables.
  174.      *  @var bool 
  175.      */
  176.     var $SMTPAuth     = false;
  177.  
  178.     /**
  179.      *  Sets SMTP username.
  180.      *  @var string 
  181.      */
  182.     var $Username     = "";
  183.  
  184.     /**
  185.      *  Sets SMTP password.
  186.      *  @var string 
  187.      */
  188.     var $Password     = "";
  189.  
  190.     /**
  191.      *  Sets the SMTP server timeout in seconds. This function will not
  192.      *  work with the win32 version.
  193.      *  @var int 
  194.      */
  195.     var $Timeout      = 10;
  196.  
  197.     /**
  198.      *  Sets SMTP class debugging on or off.
  199.      *  @var bool 
  200.      */
  201.     var $SMTPDebug    = false;
  202.  
  203.     /**
  204.      * Prevents the SMTP connection from being closed after each mail
  205.      * sending.  If this is set to true then to close the connection
  206.      * requires an explicit call to SmtpClose().
  207.      * @var bool 
  208.      */
  209.     var $SMTPKeepAlive = false;
  210.  
  211.     /**#@+
  212.      * @access private
  213.      */
  214.     var $smtp            NULL;
  215.     var $to              array();
  216.     var $cc              array();
  217.     var $bcc             array();
  218.     var $ReplyTo         array();
  219.     var $attachment      array();
  220.     var $CustomHeader    array();
  221.     var $message_type    "";
  222.     var $boundary        array();
  223.     var $language        array();
  224.     var $error_count     0;
  225.     var $LE              "\n";
  226.     /**#@-*/
  227.  
  228.     /////////////////////////////////////////////////
  229.     // VARIABLE METHODS
  230.     /////////////////////////////////////////////////
  231.  
  232.     
  233.     /**
  234.      * Sets message type to HTML.
  235.      * @param bool $bool 
  236.      * @return void 
  237.      */
  238.     function IsHTML($bool{
  239.         if($bool == true)
  240.             $this->ContentType = "text/html";
  241.         else
  242.             $this->ContentType = "text/plain";
  243.     }
  244.  
  245.     /**
  246.      * Sets Mailer to send message using SMTP.
  247.      * @return void 
  248.      */
  249.     function IsSMTP({
  250.         $this->Mailer = "smtp";
  251.     }
  252.  
  253.     /**
  254.      * Sets Mailer to send message using PHP mail() function.
  255.      * @return void 
  256.      */
  257.     function IsMail({
  258.         $this->Mailer = "mail";
  259.     }
  260.  
  261.     /**
  262.      * Sets Mailer to send message using the $Sendmail program.
  263.      * @return void 
  264.      */
  265.     function IsSendmail({
  266.         $this->Mailer = "sendmail";
  267.     }
  268.  
  269.     /**
  270.      * Sets Mailer to send message using the qmail MTA.
  271.      * @return void 
  272.      */
  273.     function IsQmail({
  274.         $this->Sendmail = "/var/qmail/bin/sendmail";
  275.         $this->Mailer = "sendmail";
  276.     }
  277.  
  278.  
  279.     /////////////////////////////////////////////////
  280.     // RECIPIENT METHODS
  281.     /////////////////////////////////////////////////
  282.  
  283.     
  284.     /**
  285.      * Adds a "To" address.
  286.      * @param string $address 
  287.      * @param string $name 
  288.      * @return void 
  289.      */
  290.     function AddAddress($address$name ""{
  291.         $cur count($this->to);
  292.         $this->to[$cur][0trim($address);
  293.         $this->to[$cur][1$name;
  294.     }
  295.  
  296.     /**
  297.      * Adds a "Cc" address. Note: this function works
  298.      * with the SMTP mailer on win32, not with the "mail"
  299.      * mailer.
  300.      * @param string $address 
  301.      * @param string $name 
  302.      * @return void 
  303.     */
  304.     function AddCC($address$name ""{
  305.         $cur count($this->cc);
  306.         $this->cc[$cur][0trim($address);
  307.         $this->cc[$cur][1$name;
  308.     }
  309.  
  310.     /**
  311.      * Adds a "Bcc" address. Note: this function works
  312.      * with the SMTP mailer on win32, not with the "mail"
  313.      * mailer.
  314.      * @param string $address 
  315.      * @param string $name 
  316.      * @return void 
  317.      */
  318.     function AddBCC($address$name ""{
  319.         $cur count($this->bcc);
  320.         $this->bcc[$cur][0trim($address);
  321.         $this->bcc[$cur][1$name;
  322.     }
  323.  
  324.     /**
  325.      * Adds a "Reply-to" address.
  326.      * @param string $address 
  327.      * @param string $name 
  328.      * @return void 
  329.      */
  330.     function AddReplyTo($address$name ""{
  331.         $cur count($this->ReplyTo);
  332.         $this->ReplyTo[$cur][0trim($address);
  333.         $this->ReplyTo[$cur][1$name;
  334.     }
  335.  
  336.  
  337.     /////////////////////////////////////////////////
  338.     // MAIL SENDING METHODS
  339.     /////////////////////////////////////////////////
  340.  
  341.     
  342.     /**
  343.      * Creates message and assigns Mailer. If the message is
  344.      * not sent successfully then it returns false.  Use the ErrorInfo
  345.      * variable to view description of the error.
  346.      * @return bool 
  347.      */
  348.     function Send({
  349.         $header "";
  350.         $body "";
  351.         $result true;
  352.  
  353.         if((count($this->tocount($this->cccount($this->bcc)) 1)
  354.         {
  355.             $this->SetError($this->Lang("provide_address"));
  356.             return false;
  357.         }
  358.  
  359.         // Set whether the message is multipart/alternative
  360.         if(!empty($this->AltBody))
  361.             $this->ContentType = "multipart/alternative";
  362.  
  363.         $this->error_count 0// reset errors
  364.         $this->SetMessageType();
  365.         $header .= $this->CreateHeader();
  366.         $body $this->CreateBody();
  367.  
  368.         if($body == ""return false}
  369.  
  370.         // Choose the mailer
  371.         switch($this->Mailer)
  372.         {
  373.             case "sendmail":
  374.                 $result $this->SendmailSend($header$body);
  375.                 break;
  376.             case "mail":
  377.                 $result $this->MailSend($header$body);
  378.                 break;
  379.             case "smtp":
  380.                 $result $this->SmtpSend($header$body);
  381.                 break;
  382.             default:
  383.             $this->SetError($this->Mailer . $this->Lang("mailer_not_supported"));
  384.                 $result false;
  385.                 break;
  386.         }
  387.  
  388.         return $result;
  389.     }
  390.  
  391.     /**
  392.      * Sends mail using the $Sendmail program.
  393.      * @access private
  394.      * @return bool 
  395.      */
  396.     function SendmailSend($header$body{
  397.         if ($this->Sender != ""{
  398.             $this->Sender = escapeshellcmd($this->Sender);
  399.             $sendmail sprintf("%s -oi -f %s -t"$this->Sendmail$this->Sender);
  400.         else {
  401.             $sendmail sprintf("%s -oi -t"$this->Sendmail);
  402.         }
  403.         if(!@$mail popen($sendmail"w"))
  404.         {
  405.             $this->SetError($this->Lang("execute"$this->Sendmail);
  406.             return false;
  407.         }
  408.  
  409.         fputs($mail$header);
  410.         fputs($mail$body);
  411.  
  412.         $result pclose($mail>> 0xFF;
  413.         if($result != 0)
  414.         {
  415.             $this->SetError($this->Lang("execute"$this->Sendmail);
  416.             return false;
  417.         }
  418.  
  419.         return true;
  420.     }
  421.  
  422.     /**
  423.      * Sends mail using the PHP mail() function.
  424.      * @access private
  425.      * @return bool 
  426.      */
  427.     function MailSend($header$body{
  428.         $to "";
  429.         for($i 0$i count($this->to)$i++)
  430.         {
  431.             if($i != 0$to .= ", "}
  432.             $to .= $this->to[$i][0];
  433.         }
  434.  
  435.         if ($this->Sender != "" && strlen(ini_get("safe_mode"))1)
  436.         {
  437.             $old_from ini_get("sendmail_from");
  438.             ini_set("sendmail_from"$this->Sender);
  439.             $params sprintf("-oi -f %s"$this->Sender);
  440.             $rt @mail($to$this->EncodeHeader($this->Subject)$body,
  441.                         $header$params);
  442.         }
  443.         else
  444.             $rt @mail($to$this->EncodeHeader($this->Subject)$body$header);
  445.  
  446.         if (isset($old_from))
  447.             ini_set("sendmail_from"$old_from);
  448.  
  449.         if(!$rt)
  450.         {
  451.             $this->SetError($this->Lang("instantiate"));
  452.             return false;
  453.         }
  454.  
  455.         return true;
  456.     }
  457.  
  458.     /**
  459.      * Sends mail via SMTP using PhpSMTP (Author:
  460.      * Chris Ryan).  Returns bool.  Returns false if there is a
  461.      * bad MAIL FROM, RCPT, or DATA input.
  462.      * @access private
  463.      * @return bool 
  464.      */
  465.     function SmtpSend($header$body{
  466.         include_once($this->PluginDir . "smtp.php");
  467.         $error "";
  468.         $bad_rcpt array();
  469.  
  470.         if(!$this->SmtpConnect())
  471.             return false;
  472.  
  473.         $smtp_from ($this->Sender == ""$this->From : $this->Sender;
  474.         if(!$this->smtp->Mail($smtp_from))
  475.         {
  476.             $error $this->Lang("from_failed"$smtp_from;
  477.             $this->SetError($error);
  478.             $this->smtp->Reset();
  479.             return false;
  480.         }
  481.  
  482.         // Attempt to send attach all recipients
  483.         for($i 0$i count($this->to)$i++)
  484.         {
  485.             if(!$this->smtp->Recipient($this->to[$i][0]))
  486.                 $bad_rcpt[$this->to[$i][0];
  487.         }
  488.         for($i 0$i count($this->cc)$i++)
  489.         {
  490.             if(!$this->smtp->Recipient($this->cc[$i][0]))
  491.                 $bad_rcpt[$this->cc[$i][0];
  492.         }
  493.         for($i 0$i count($this->bcc)$i++)
  494.         {
  495.             if(!$this->smtp->Recipient($this->bcc[$i][0]))
  496.                 $bad_rcpt[$this->bcc[$i][0];
  497.         }
  498.  
  499.         if(count($bad_rcpt0// Create error message
  500.         {
  501.             for($i 0$i count($bad_rcpt)$i++)
  502.             {
  503.                 if($i != 0$error .= ", "}
  504.                 $error .= $bad_rcpt[$i];
  505.             }
  506.             $error $this->Lang("recipients_failed"$error;
  507.             $this->SetError($error);
  508.             $this->smtp->Reset();
  509.             return false;
  510.         }
  511.  
  512.         if(!$this->smtp->Data($header $body))
  513.         {
  514.             $this->SetError($this->Lang("data_not_accepted"));
  515.             $this->smtp->Reset();
  516.             return false;
  517.         }
  518.         if($this