linea21-externals
[ class tree: linea21-externals ] [ index: linea21-externals ] [ all elements ]

Source for file class.smtp.php

Documentation is available at class.smtp.php

  1. <?php
  2. /*~ class.smtp.php
  3.  .---------------------------------------------------------------------------.
  4.  |  Software: PHPMailer - PHP email class                                    |
  5.  |   Version: 5.1                                                            |
  6.  |   Contact: via sourceforge.net support pages (also www.codeworxtech.com)  |
  7.  |      Info: http://phpmailer.sourceforge.net                               |
  8.  |   Support: http://sourceforge.net/projects/phpmailer/                     |
  9.  | ------------------------------------------------------------------------- |
  10.  |     Admin: Andy Prevost (project admininistrator)                         |
  11.  |   Authors: Andy Prevost (codeworxtech) codeworxtech@users.sourceforge.net |
  12.  |          : Marcus Bointon (coolbru) coolbru@users.sourceforge.net         |
  13.  |   Founder: Brent R. Matzelle (original founder)                           |
  14.  | Copyright (c) 2004-2009, Andy Prevost. All Rights Reserved.               |
  15.  | Copyright (c) 2001-2003, Brent R. Matzelle                                |
  16.  | ------------------------------------------------------------------------- |
  17.  |   License: Distributed under the Lesser General Public License (LGPL)     |
  18.  |            http://www.gnu.org/copyleft/lesser.html                        |
  19.  | This program is distributed in the hope that it will be useful - WITHOUT  |
  20.  | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     |
  21.  | FITNESS FOR A PARTICULAR PURPOSE.                                         |
  22.  | ------------------------------------------------------------------------- |
  23.  | We offer a number of paid services (www.codeworxtech.com):                |
  24.  | - Web Hosting on highly optimized fast and secure servers                 |
  25.  | - Technology Consulting                                                   |
  26.  | - Oursourcing (highly qualified programmers and graphic designers)        |
  27.  '---------------------------------------------------------------------------'
  28.  */
  29.  
  30. /**
  31.  * PHPMailer - PHP SMTP email transport class
  32.  * NOTE: Designed for use with PHP version 5 and up
  33.  * @package linea21.externals
  34.  * @subpackage PHPMailer
  35.  * @author Andy Prevost
  36.  * @author Marcus Bointon
  37.  * @copyright 2004 - 2008 Andy Prevost
  38.  * @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
  39.  * @version $Id: class.smtp.php 444 2009-05-05 11:22:26Z coolbru $
  40.  */
  41.  
  42. /**
  43.  * SMTP is rfc 821 compliant and implements all the rfc 821 SMTP
  44.  * commands except TURN which will always return a not implemented
  45.  * error. SMTP also provides some utility methods for sending mail
  46.  * to an SMTP server.
  47.  * original author: Chris Ryan
  48.  */
  49.  
  50. class SMTP {
  51.   /**
  52.    *  SMTP server port
  53.    *  @var int 
  54.    */
  55.   public $SMTP_PORT = 25;
  56.  
  57.   /**
  58.    *  SMTP reply line ending
  59.    *  @var string 
  60.    */
  61.   public $CRLF = "\r\n";
  62.  
  63.   /**
  64.    *  Sets whether debugging is turned on
  65.    *  @var bool 
  66.    */
  67.   public $do_debug;       // the level of debug to perform
  68.  
  69.   /**
  70.    *  Sets VERP use on/off (default is off)
  71.    *  @var bool 
  72.    */
  73.   public $do_verp = false;
  74.  
  75.   /////////////////////////////////////////////////
  76.   // PROPERTIES, PRIVATE AND PROTECTED
  77.   /////////////////////////////////////////////////
  78.  
  79.   private $smtp_conn; // the socket to the server
  80.   private $error;     // error if any on the last call
  81.   private $helo_rply; // the reply the server sent to us for HELO
  82.  
  83.   /**
  84.    * Initialize the class so that the data is in a known state.
  85.    * @access public
  86.    * @return void 
  87.    */
  88.   public function __construct() {
  89.     $this->smtp_conn = 0;
  90.     $this->error = null;
  91.     $this->helo_rply = null;
  92.  
  93.     $this->do_debug = 0;
  94.   }
  95.  
  96.   /////////////////////////////////////////////////
  97.   // CONNECTION FUNCTIONS
  98.   /////////////////////////////////////////////////
  99.  
  100.   /**
  101.    * Connect to the server specified on the port specified.
  102.    * If the port is not specified use the default SMTP_PORT.
  103.    * If tval is specified then a connection will try and be
  104.    * established with the server for that number of seconds.
  105.    * If tval is not specified the default is 30 seconds to
  106.    * try on the connection.
  107.    *
  108.    * SMTP CODE SUCCESS: 220
  109.    * SMTP CODE FAILURE: 421
  110.    * @access public
  111.    * @return bool 
  112.    */
  113.   public function Connect($host, $port = 0, $tval = 30) {
  114.     // set the error val to null so there is no confusion
  115.     $this->error = null;
  116.  
  117.     // make sure we are __not__ connected
  118.     if($this->connected()) {
  119.       // already connected, generate error
  120.       $this->error = array("error" => "Already connected to a server");
  121.       return false;
  122.     }
  123.  
  124.     if(empty($port)) {
  125.       $port = $this->SMTP_PORT;
  126.     }
  127.  
  128.     // connect to the smtp server
  129.     $this->smtp_conn = @fsockopen($host,    // the host of the server
  130.     $port,    // the port to use
  131.     $errno,   // error number if any
  132.     $errstr,  // error message if any
  133.     $tval);   // give up after ? secs
  134.     // verify we connected properly
  135.     if(empty($this->smtp_conn)) {
  136.       $this->error = array("error" => "Failed to connect to server",
  137.                            "errno" => $errno,
  138.                            "errstr" => $errstr);
  139.       if($this->do_debug >= 1) {
  140.         echo "SMTP -> ERROR: " . $this->error["error"] . ": $errstr ($errno)" . $this->CRLF . '<br />';
  141.       }
  142.       return false;
  143.     }
  144.  
  145.     // SMTP server can take longer to respond, give longer timeout for first read
  146.     // Windows does not have support for this timeout function
  147.     if(substr(PHP_OS, 0, 3) != "WIN")
  148.     socket_set_timeout($this->smtp_conn, $tval, 0);
  149.  
  150.     // get any announcement
  151.     $announce = $this->get_lines();
  152.  
  153.     if($this->do_debug >= 2) {
  154.       echo "SMTP -> FROM SERVER:" . $announce . $this->CRLF . '<br />';
  155.     }
  156.  
  157.     return true;
  158.   }
  159.  
  160.   /**
  161.    * Initiate a TLS communication with the server.
  162.    *
  163.    * SMTP CODE 220 Ready to start TLS
  164.    * SMTP CODE 501 Syntax error (no parameters allowed)
  165.    * SMTP CODE 454 TLS not available due to temporary reason
  166.    * @access public
  167.    * @return bool success
  168.    */
  169.   public function StartTLS() {
  170.     $this->error = null; # to avoid confusion
  171.  
  172.     if(!$this->connected()) {
  173.       $this->error = array("error" => "Called StartTLS() without being connected");
  174.       return false;
  175.     }
  176.  
  177.     fputs($this->smtp_conn,"STARTTLS" . $this->CRLF);
  178.  
  179.     $rply = $this->get_lines();
  180.     $code = substr($rply,0,3);
  181.  
  182.     if($this->do_debug >= 2) {
  183.       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
  184.     }
  185.  
  186.     if($code != 220) {
  187.       $this->error =
  188.       array("error"     => "STARTTLS not accepted from server",
  189.                "smtp_code" => $code,
  190.                "smtp_msg"  => substr($rply,4));
  191.       if($this->do_debug >= 1) {
  192.         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  193.       }
  194.       return false;
  195.     }
  196.  
  197.     // Begin encrypted connection
  198.     if(!stream_socket_enable_crypto($this->smtp_conn, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
  199.       return false;
  200.     }
  201.  
  202.     return true;
  203.   }
  204.  
  205.   /**
  206.    * Performs SMTP authentication.  Must be run after running the
  207.    * Hello() method.  Returns true if successfully authenticated.
  208.    * @access public
  209.    * @return bool 
  210.    */
  211.   public function Authenticate($username, $password) {
  212.     // Start authentication
  213.     fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF);
  214.  
  215.     $rply = $this->get_lines();
  216.     $code = substr($rply,0,3);
  217.  
  218.     if($code != 334) {
  219.       $this->error =
  220.       array("error" => "AUTH not accepted from server",
  221.               "smtp_code" => $code,
  222.               "smtp_msg" => substr($rply,4));
  223.       if($this->do_debug >= 1) {
  224.         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  225.       }
  226.       return false;
  227.     }
  228.  
  229.     // Send encoded username
  230.     fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
  231.  
  232.     $rply = $this->get_lines();
  233.     $code = substr($rply,0,3);
  234.  
  235.     if($code != 334) {
  236.       $this->error =
  237.       array("error" => "Username not accepted from server",
  238.               "smtp_code" => $code,
  239.               "smtp_msg" => substr($rply,4));
  240.       if($this->do_debug >= 1) {
  241.         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  242.       }
  243.       return false;
  244.     }
  245.  
  246.     // Send encoded password
  247.     fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
  248.  
  249.     $rply = $this->get_lines();
  250.     $code = substr($rply,0,3);
  251.  
  252.     if($code != 235) {
  253.       $this->error =
  254.       array("error" => "Password not accepted from server",
  255.               "smtp_code" => $code,
  256.               "smtp_msg" => substr($rply,4));
  257.       if($this->do_debug >= 1) {
  258.         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  259.       }
  260.       return false;
  261.     }
  262.  
  263.     return true;
  264.   }
  265.  
  266.   /**
  267.    * Returns true if connected to a server otherwise false
  268.    * @access public
  269.    * @return bool 
  270.    */
  271.   public function Connected() {
  272.     if(!empty($this->smtp_conn)) {
  273.       $sock_status = socket_get_status($this->smtp_conn);
  274.       if($sock_status["eof"]) {
  275.         // the socket is valid but we are not connected
  276.         if($this->do_debug >= 1) {
  277.           echo "SMTP -> NOTICE:" . $this->CRLF . "EOF caught while checking if connected";
  278.         }
  279.         $this->Close();
  280.         return false;
  281.       }
  282.       return true; // everything looks good
  283.     }
  284.     return false;
  285.   }
  286.  
  287.   /**
  288.    * Closes the socket and cleans up the state of the class.
  289.    * It is not considered good to use this function without
  290.    * first trying to use QUIT.
  291.    * @access public
  292.    * @return void 
  293.    */
  294.   public function Close() {
  295.     $this->error = null; // so there is no confusion
  296.     $this->helo_rply = null;
  297.     if(!empty($this->smtp_conn)) {
  298.       // close the connection and cleanup
  299.       fclose($this->smtp_conn);
  300.       $this->smtp_conn = 0;
  301.     }
  302.   }
  303.  
  304.   /////////////////////////////////////////////////
  305.   // SMTP COMMANDS
  306.   /////////////////////////////////////////////////
  307.  
  308.   /**
  309.    * Issues a data command and sends the msg_data to the server
  310.    * finializing the mail transaction. $msg_data is the message
  311.    * that is to be send with the headers. Each header needs to be
  312.    * on a single line followed by a <CRLF> with the message headers
  313.    * and the message body being seperated by and additional <CRLF>.
  314.    *
  315.    * Implements rfc 821: DATA <CRLF>
  316.    *
  317.    * SMTP CODE INTERMEDIATE: 354
  318.    *     [data]
  319.    *     <CRLF>.<CRLF>
  320.    *     SMTP CODE SUCCESS: 250
  321.    *     SMTP CODE FAILURE: 552,554,451,452
  322.    * SMTP CODE FAILURE: 451,554
  323.    * SMTP CODE ERROR  : 500,501,503,421
  324.    * @access public
  325.    * @return bool 
  326.    */
  327.   public function Data($msg_data) {
  328.     $this->error = null; // so no confusion is caused
  329.  
  330.     if(!$this->connected()) {
  331.       $this->error = array(
  332.               "error" => "Called Data() without being connected");
  333.       return false;
  334.     }
  335.  
  336.     fputs($this->smtp_conn,"DATA" . $this->CRLF);
  337.  
  338.     $rply = $this->get_lines();
  339.     $code = substr($rply,0,3);
  340.  
  341.     if($this->do_debug >= 2) {
  342.       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
  343.     }
  344.  
  345.     if($code != 354) {
  346.       $this->error =
  347.       array("error" => "DATA command not accepted from server",
  348.               "smtp_code" => $code,
  349.               "smtp_msg" => substr($rply,4));
  350.       if($this->do_debug >= 1) {
  351.         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  352.       }
  353.       return false;
  354.     }
  355.  
  356.     /* the server is ready to accept data!
  357.      * according to rfc 821 we should not send more than 1000
  358.      * including the CRLF
  359.      * characters on a single line so we will break the data up
  360.      * into lines by \r and/or \n then if needed we will break
  361.      * each of those into smaller lines to fit within the limit.
  362.      * in addition we will be looking for lines that start with
  363.      * a period '.' and append and additional period '.' to that
  364.      * line. NOTE: this does not count towards limit.
  365.      */
  366.  
  367.     // normalize the line breaks so we know the explode works
  368.     $msg_data = str_replace("\r\n","\n",$msg_data);
  369.     $msg_data = str_replace("\r","\n",$msg_data);
  370.     $lines = explode("\n",$msg_data);
  371.  
  372.     /* we need to find a good way to determine is headers are
  373.      * in the msg_data or if it is a straight msg body
  374.      * currently I am assuming rfc 822 definitions of msg headers
  375.      * and if the first field of the first line (':' sperated)
  376.      * does not contain a space then it _should_ be a header
  377.      * and we can process all lines before a blank "" line as
  378.      * headers.
  379.      */
  380.  
  381.     $field = substr($lines[0],0,strpos($lines[0],":"));
  382.     $in_headers = false;
  383.     if(!empty($field) && !strstr($field," ")) {
  384.       $in_headers = true;
  385.     }
  386.  
  387.     $max_line_length = 998; // used below; set here for ease in change
  388.  
  389.     while(list(,$line) = @each($lines)) {
  390.       $lines_out = null;
  391.       if($line == "" && $in_headers) {
  392.         $in_headers = false;
  393.       }
  394.       // ok we need to break this line up into several smaller lines
  395.       while(strlen($line) > $max_line_length) {
  396.         $pos = strrpos(substr($line,0,$max_line_length)," ");
  397.  
  398.         // Patch to fix DOS attack
  399.         if(!$pos) {
  400.           $pos = $max_line_length - 1;
  401.           $lines_out[] = substr($line,0,$pos);
  402.           $line = substr($line,$pos);
  403.         } else {
  404.           $lines_out[] = substr($line,0,$pos);
  405.           $line = substr($line,$pos + 1);
  406.         }
  407.  
  408.         /* if processing headers add a LWSP-char to the front of new line
  409.          * rfc 822 on long msg headers
  410.          */
  411.         if($in_headers) {
  412.           $line = "\t" . $line;
  413.         }
  414.       }
  415.       $lines_out[] = $line;
  416.  
  417.       // send the lines to the server
  418.       while(list(,$line_out) = @each($lines_out)) {
  419.         if(strlen($line_out) > 0)
  420.         {
  421.           if(substr($line_out, 0, 1) == ".") {
  422.             $line_out = "." . $line_out;
  423.           }
  424.         }
  425.         fputs($this->smtp_conn,$line_out . $this->CRLF);
  426.       }
  427.     }
  428.  
  429.     // message data has been sent
  430.     fputs($this->smtp_conn, $this->CRLF . "." . $this->CRLF);
  431.  
  432.     $rply = $this->get_lines();
  433.     $code = substr($rply,0,3);
  434.  
  435.     if($this->do_debug >= 2) {
  436.       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
  437.     }
  438.  
  439.     if($code != 250) {
  440.       $this->error =
  441.       array("error" => "DATA not accepted from server",
  442.               "smtp_code" => $code,
  443.               "smtp_msg" => substr($rply,4));
  444.       if($this->do_debug >= 1) {
  445.         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  446.       }
  447.       return false;
  448.     }
  449.     return true;
  450.   }
  451.  
  452.   /**
  453.    * Sends the HELO command to the smtp server.
  454.    * This makes sure that we and the server are in
  455.    * the same known state.
  456.    *
  457.    * Implements from rfc 821: HELO <SP> <domain> <CRLF>
  458.    *
  459.    * SMTP CODE SUCCESS: 250
  460.    * SMTP CODE ERROR  : 500, 501, 504, 421
  461.    * @access public
  462.    * @return bool 
  463.    */
  464.   public function Hello($host = '') {
  465.     $this->error = null; // so no confusion is caused
  466.  
  467.     if(!$this->connected()) {
  468.       $this->error = array(
  469.             "error" => "Called Hello() without being connected");
  470.       return false;
  471.     }
  472.  
  473.     // if hostname for HELO was not specified send default
  474.     if(empty($host)) {
  475.       // determine appropriate default to send to server
  476.       $host = "localhost";
  477.     }
  478.  
  479.     // Send extended hello first (RFC 2821)
  480.     if(!$this->SendHello("EHLO", $host)) {
  481.       if(!$this->SendHello("HELO", $host)) {
  482.         return false;
  483.       }
  484.     }
  485.  
  486.     return true;
  487.   }
  488.  
  489.   /**
  490.    * Sends a HELO/EHLO command.
  491.    * @access private
  492.    * @return bool 
  493.    */
  494.   private function SendHello($hello, $host) {
  495.     fputs($this->smtp_conn, $hello . " " . $host . $this->CRLF);
  496.  
  497.     $rply = $this->get_lines();
  498.     $code = substr($rply,0,3);
  499.  
  500.     if($this->do_debug >= 2) {
  501.       echo "SMTP -> FROM SERVER: " . $rply . $this->CRLF . '<br />';
  502.     }
  503.  
  504.     if($code != 250) {
  505.       $this->error =
  506.       array("error" => $hello . " not accepted from server",
  507.               "smtp_code" => $code,
  508.               "smtp_msg" => substr($rply,4));
  509.       if($this->do_debug >= 1) {
  510.         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  511.       }
  512.       return false;
  513.     }
  514.  
  515.     $this->helo_rply = $rply;
  516.  
  517.     return true;
  518.   }
  519.  
  520.   /**
  521.    * Starts a mail transaction from the email address specified in
  522.    * $from. Returns true if successful or false otherwise. If True
  523.    * the mail transaction is started and then one or more Recipient
  524.    * commands may be called followed by a Data command.
  525.    *
  526.    * Implements rfc 821: MAIL <SP> FROM:<reverse-path> <CRLF>
  527.    *
  528.    * SMTP CODE SUCCESS: 250
  529.    * SMTP CODE SUCCESS: 552,451,452
  530.    * SMTP CODE SUCCESS: 500,501,421
  531.    * @access public
  532.    * @return bool 
  533.    */
  534.   public function Mail($from) {
  535.     $this->error = null; // so no confusion is caused
  536.  
  537.     if(!$this->connected()) {
  538.       $this->error = array(
  539.               "error" => "Called Mail() without being connected");
  540.       return false;
  541.     }
  542.  
  543.     $useVerp = ($this->do_verp ? "XVERP" : "");
  544.     fputs($this->smtp_conn,"MAIL FROM:<" . $from . ">" . $useVerp . $this->CRLF);
  545.  
  546.     $rply = $this->get_lines();
  547.     $code = substr($rply,0,3);
  548.  
  549.     if($this->do_debug >= 2) {
  550.       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
  551.     }
  552.  
  553.     if($code != 250) {
  554.       $this->error =
  555.       array("error" => "MAIL not accepted from server",
  556.               "smtp_code" => $code,
  557.               "smtp_msg" => substr($rply,4));
  558.       if($this->do_debug >= 1) {
  559.         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  560.       }
  561.       return false;
  562.     }
  563.     return true;
  564.   }
  565.  
  566.   /**
  567.    * Sends the quit command to the server and then closes the socket
  568.    * if there is no error or the $close_on_error argument is true.
  569.    *
  570.    * Implements from rfc 821: QUIT <CRLF>
  571.    *
  572.    * SMTP CODE SUCCESS: 221
  573.    * SMTP CODE ERROR  : 500
  574.    * @access public
  575.    * @return bool 
  576.    */
  577.   public function Quit($close_on_error = true) {
  578.     $this->error = null; // so there is no confusion
  579.  
  580.     if(!$this->connected()) {
  581.       $this->error = array(
  582.               "error" => "Called Quit() without being connected");
  583.       return false;
  584.     }
  585.  
  586.     // send the quit command to the server
  587.     fputs($this->smtp_conn,"quit" . $this->CRLF);
  588.  
  589.     // get any good-bye messages
  590.     $byemsg = $this->get_lines();
  591.  
  592.     if($this->do_debug >= 2) {
  593.       echo "SMTP -> FROM SERVER:" . $byemsg . $this->CRLF . '<br />';
  594.     }
  595.  
  596.     $rval = true;
  597.     $e = null;
  598.  
  599.     $code = substr($byemsg,0,3);
  600.     if($code != 221) {
  601.       // use e as a tmp var cause Close will overwrite $this->error
  602.       $e = array("error" => "SMTP server rejected quit command",
  603.                  "smtp_code" => $code,
  604.                  "smtp_rply" => substr($byemsg,4));
  605.       $rval = false;
  606.       if($this->do_debug >= 1) {
  607.         echo "SMTP -> ERROR: " . $e["error"] . ": " . $byemsg . $this->CRLF . '<br />';
  608.       }
  609.     }
  610.  
  611.     if(empty($e) || $close_on_error) {
  612.       $this->Close();
  613.     }
  614.  
  615.     return $rval;
  616.   }
  617.  
  618.   /**
  619.    * Sends the command RCPT to the SMTP server with the TO: argument of $to.
  620.    * Returns true if the recipient was accepted false if it was rejected.
  621.    *
  622.    * Implements from rfc 821: RCPT <SP> TO:<forward-path> <CRLF>
  623.    *
  624.    * SMTP CODE SUCCESS: 250,251
  625.    * SMTP CODE FAILURE: 550,551,552,553,450,451,452
  626.    * SMTP CODE ERROR  : 500,501,503,421
  627.    * @access public
  628.    * @return bool 
  629.    */
  630.   public function Recipient($to) {
  631.     $this->error = null; // so no confusion is caused
  632.  
  633.     if(!$this->connected()) {
  634.       $this->error = array(
  635.               "error" => "Called Recipient() without being connected");
  636.       return false;
  637.     }
  638.  
  639.     fputs($this->smtp_conn,"RCPT TO:<" . $to . ">" . $this->CRLF);
  640.  
  641.     $rply = $this->get_lines();
  642.     $code = substr($rply,0,3);
  643.  
  644.     if($this->do_debug >= 2) {
  645.       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
  646.     }
  647.  
  648.     if($code != 250 && $code != 251) {
  649.       $this->error =
  650.       array("error" => "RCPT not accepted from server",
  651.               "smtp_code" => $code,
  652.               "smtp_msg" => substr($rply,4));
  653.       if($this->do_debug >= 1) {
  654.         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  655.       }
  656.       return false;
  657.     }
  658.     return true;
  659.   }
  660.  
  661.   /**
  662.    * Sends the RSET command to abort and transaction that is
  663.    * currently in progress. Returns true if successful false
  664.    * otherwise.
  665.    *
  666.    * Implements rfc 821: RSET <CRLF>
  667.    *
  668.    * SMTP CODE SUCCESS: 250
  669.    * SMTP CODE ERROR  : 500,501,504,421
  670.    * @access public
  671.    * @return bool 
  672.    */
  673.   public function Reset() {
  674.     $this->error = null; // so no confusion is caused
  675.  
  676.     if(!$this->connected()) {
  677.       $this->error = array(
  678.               "error" => "Called Reset() without being connected");
  679.       return false;
  680.     }
  681.  
  682.     fputs($this->smtp_conn,"RSET" . $this->CRLF);
  683.  
  684.     $rply = $this->get_lines();
  685.     $code = substr($rply,0,3);
  686.  
  687.     if($this->do_debug >= 2) {
  688.       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
  689.     }
  690.  
  691.     if($code != 250) {
  692.       $this->error =
  693.       array("error" => "RSET failed",
  694.               "smtp_code" => $code,
  695.               "smtp_msg" => substr($rply,4));
  696.       if($this->do_debug >= 1) {
  697.         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  698.       }
  699.       return false;
  700.     }
  701.  
  702.     return true;
  703.   }
  704.  
  705.   /**
  706.    * Starts a mail transaction from the email address specified in
  707.    * $from. Returns true if successful or false otherwise. If True
  708.    * the mail transaction is started and then one or more Recipient
  709.    * commands may be called followed by a Data command. This command
  710.    * will send the message to the users terminal if they are logged
  711.    * in and send them an email.
  712.    *
  713.    * Implements rfc 821: SAML <SP> FROM:<reverse-path> <CRLF>
  714.    *
  715.    * SMTP CODE SUCCESS: 250
  716.    * SMTP CODE SUCCESS: 552,451,452
  717.    * SMTP CODE SUCCESS: 500,501,502,421
  718.    * @access public
  719.    * @return bool 
  720.    */
  721.   public function SendAndMail($from) {
  722.     $this->error = null; // so no confusion is caused
  723.  
  724.     if(!$this->connected()) {
  725.       $this->error = array(
  726.           "error" => "Called SendAndMail() without being connected");
  727.       return false;
  728.     }
  729.  
  730.     fputs($this->smtp_conn,"SAML FROM:" . $from . $this->CRLF);
  731.  
  732.     $rply = $this->get_lines();
  733.     $code = substr($rply,0,3);
  734.  
  735.     if($this->do_debug >= 2) {
  736.       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
  737.     }
  738.  
  739.     if($code != 250) {
  740.       $this->error =
  741.       array("error" => "SAML not accepted from server",
  742.               "smtp_code" => $code,
  743.               "smtp_msg" => substr($rply,4));
  744.       if($this->do_debug >= 1) {
  745.         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  746.       }
  747.       return false;
  748.     }
  749.     return true;
  750.   }
  751.  
  752.   /**
  753.    * This is an optional command for SMTP that this class does not
  754.    * support. This method is here to make the RFC821 Definition
  755.    * complete for this class and __may__ be implimented in the future
  756.    *
  757.    * Implements from rfc 821: TURN <CRLF>
  758.    *
  759.    * SMTP CODE SUCCESS: 250
  760.    * SMTP CODE FAILURE: 502
  761.    * SMTP CODE ERROR  : 500, 503
  762.    * @access public
  763.    * @return bool 
  764.    */
  765.   public function Turn() {
  766.     $this->error = array("error" => "This method, TURN, of the SMTP ".
  767.                                     "is not implemented");
  768.     if($this->do_debug >= 1) {
  769.       echo "SMTP -> NOTICE: " . $this->error["error"] . $this->CRLF . '<br />';
  770.     }
  771.     return false;
  772.   }
  773.  
  774.   /**
  775.    * Get the current error
  776.    * @access public
  777.    * @return array 
  778.    */
  779.   public function getError() {
  780.     return $this->error;
  781.   }
  782.  
  783.   /////////////////////////////////////////////////
  784.   // INTERNAL FUNCTIONS
  785.   /////////////////////////////////////////////////
  786.  
  787.   /**
  788.    * Read in as many lines as possible
  789.    * either before eof or socket timeout occurs on the operation.
  790.    * With SMTP we can tell if we have more lines to read if the
  791.    * 4th character is '-' symbol. If it is a space then we don't
  792.    * need to read anything else.
  793.    * @access private
  794.    * @return string 
  795.    */
  796.   private function get_lines() {
  797.     $data = "";
  798.     while($str = @fgets($this->smtp_conn,515)) {
  799.       if($this->do_debug >= 4) {
  800.         echo "SMTP -> get_lines(): \$data was \"$data\"" . $this->CRLF . '<br />';
  801.         echo "SMTP -> get_lines(): \$str is \"$str\"" . $this->CRLF . '<br />';
  802.       }
  803.       $data .= $str;
  804.       if($this->do_debug >= 4) {
  805.         echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF . '<br />';
  806.       }
  807.       // if 4th character is a space, we are done reading, break the loop
  808.       if(substr($str,3,1) == " ") { break; }
  809.     }
  810.     return $data;
  811.   }
  812.  
  813. }
  814.  
  815. ?>

Documentation generated on Thu, 03 May 2012 15:03:13 +0200 by phpDocumentor 1.4.1