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. ////////////////////////////////////////////////////
  3. // SMTP - PHP SMTP class
  4. //
  5. // Version 1.02
  6. //
  7. // Define an SMTP class that can be used to connect
  8. // and communicate with any SMTP server. It implements
  9. // all the SMTP functions defined in RFC821 except TURN.
  10. //
  11. // Author: Chris Ryan
  12. //
  13. // License: LGPL, see LICENSE
  14. ////////////////////////////////////////////////////
  15.  
  16. /**
  17.  * SMTP is rfc 821 compliant and implements all the rfc 821 SMTP
  18.  * commands except TURN which will always return a not implemented
  19.  * error. SMTP also provides some utility methods for sending mail
  20.  * to an SMTP server.
  21.  * @package linea21.externals
  22.  * @subpackage PHPMailer
  23.  * @author Chris Ryan
  24.  */
  25.  
  26. class SMTP
  27. {
  28.     /**
  29.      *  SMTP server port
  30.      *  @var int 
  31.      */
  32.     var $SMTP_PORT = 25;
  33.     
  34.     /**
  35.      *  SMTP reply line ending
  36.      *  @var string 
  37.      */
  38.     var $CRLF = "\r\n";
  39.     
  40.     /**
  41.      *  Sets whether debugging is turned on
  42.      *  @var bool 
  43.      */
  44.     var $do_debug;       # the level of debug to perform
  45.  
  46.     /**#@+
  47.      * @access private
  48.      */
  49.     var $smtp_conn;      # the socket to the server
  50.     var $error;          # error if any on the last call
  51.     var $helo_rply;      # the reply the server sent to us for HELO
  52.     /**#@-*/
  53.  
  54.     /**
  55.      * Initialize the class so that the data is in a known state.
  56.      * @access public
  57.      * @return void 
  58.      */
  59.     function SMTP({
  60.         $this->smtp_conn 0;
  61.         $this->error null;
  62.         $this->helo_rply null;
  63.  
  64.         $this->do_debug = 0;
  65.     }
  66.  
  67.     /*************************************************************
  68.      *                    CONNECTION FUNCTIONS                  *
  69.      ***********************************************************/
  70.  
  71.     /**
  72.      * Connect to the server specified on the port specified.
  73.      * If the port is not specified use the default SMTP_PORT.
  74.      * If tval is specified then a connection will try and be
  75.      * established with the server for that number of seconds.
  76.      * If tval is not specified the default is 30 seconds to
  77.      * try on the connection.
  78.      *
  79.      * SMTP CODE SUCCESS: 220
  80.      * SMTP CODE FAILURE: 421
  81.      * @access public
  82.      * @return bool 
  83.      */
  84.     function Connect($host,$port=0,$tval=30{
  85.         # set the error val to null so there is no confusion
  86.         $this->error null;
  87.  
  88.         # make sure we are __not__ connected
  89.         if($this->connected()) {
  90.             # ok we are connected! what should we do?
  91.             # for now we will just give an error saying we
  92.             # are already connected
  93.             $this->error =
  94.                 array("error" => "Already connected to a server");
  95.             return false;
  96.         }
  97.  
  98.         if(empty($port)) {
  99.             $port $this->SMTP_PORT;
  100.         }
  101.  
  102.         #connect to the smtp server
  103.         $this->smtp_conn fsockopen($host,    # the host of the server
  104.                                      $port,    # the port to use
  105.                                      $errno,   # error number if any
  106.                                      $errstr,  # error message if any
  107.                                      $tval);   # give up after ? secs
  108.         # verify we connected properly
  109.         if(empty($this->smtp_conn)) {
  110.             $this->error array("error" => "Failed to connect to server",
  111.                                  "errno" => $errno,
  112.                                  "errstr" => $errstr);
  113.             if($this->do_debug >= 1{
  114.                 echo "SMTP -> ERROR: " $this->error["error".
  115.                          "$errstr ($errno)$this->CRLF;
  116.             }
  117.             return false;
  118.         }
  119.  
  120.         # sometimes the SMTP server takes a little longer to respond
  121.         # so we will give it a longer timeout for the first read
  122.         // Windows still does not have support for this timeout function
  123.         if(substr(PHP_OS03!= "WIN")
  124.            socket_set_timeout($this->smtp_conn$tval0);
  125.  
  126.         # get any announcement stuff
  127.         $announce $this->get_lines();
  128.  
  129.         # set the timeout  of any socket functions at 1/10 of a second
  130.         //if(function_exists("socket_set_timeout"))
  131.         //   socket_set_timeout($this->smtp_conn, 0, 100000);
  132.  
  133.         if($this->do_debug >= 2{
  134.             echo "SMTP -> FROM SERVER:" $this->CRLF . $announce;
  135.         }
  136.  
  137.         return true;
  138.     }
  139.  
  140.     /**
  141.      * Performs SMTP authentication.  Must be run after running the
  142.      * Hello() method.  Returns true if successfully authenticated.
  143.      * @access public
  144.      * @return bool 
  145.      */
  146.     function Authenticate($username$password{
  147.         // Start authentication
  148.         fputs($this->smtp_conn,"AUTH LOGIN" $this->CRLF);
  149.  
  150.         $rply $this->get_lines();
  151.         $code substr($rply,0,3);
  152.  
  153.         if($code != 334{
  154.             $this->error =
  155.                 array("error" => "AUTH not accepted from server",
  156.                       "smtp_code" => $code,
  157.                       "smtp_msg" => substr($rply,4));
  158.             if($this->do_debug >= 1{
  159.                 echo "SMTP -> ERROR: " $this->error["error".
  160.                          ": " $rply $this->CRLF;
  161.             }
  162.             return false;
  163.         }
  164.  
  165.         // Send encoded username
  166.         fputs($this->smtp_connbase64_encode($username$this->CRLF);
  167.  
  168.         $rply $this->get_lines();
  169.         $code substr($rply,0,3);
  170.  
  171.         if($code != 334{
  172.             $this->error =
  173.                 array("error" => "Username not accepted from server",
  174.                       "smtp_code" => $code,
  175.                       "smtp_msg" => substr($rply,4));
  176.             if($this->do_debug >= 1{
  177.                 echo "SMTP -> ERROR: " $this->error["error".
  178.                          ": " $rply $this->CRLF;
  179.             }
  180.             return false;
  181.         }
  182.  
  183.         // Send encoded password
  184.         fputs($this->smtp_connbase64_encode($password$this->CRLF);
  185.  
  186.         $rply $this->get_lines();
  187.         $code substr($rply,0,3);
  188.  
  189.         if($code != 235{
  190.             $this->error =
  191.                 array("error" => "Password not accepted from server",
  192.                       "smtp_code" => $code,
  193.                       "smtp_msg" => substr($rply,4));
  194.             if($this->do_debug >= 1{
  195.                 echo "SMTP -> ERROR: " $this->error["error".
  196.                          ": " $rply $this->CRLF;
  197.             }
  198.             return false;
  199.         }
  200.  
  201.         return true;
  202.     }
  203.  
  204.     /**
  205.      * Returns true if connected to a server otherwise false
  206.      * @access private
  207.      * @return bool 
  208.      */
  209.     function Connected({
  210.         if(!empty($this->smtp_conn)) {
  211.             $sock_status socket_get_status($this->smtp_conn);
  212.             if($sock_status["eof"]{
  213.                 # hmm this is an odd situation... the socket is
  214.                 # valid but we aren't connected anymore
  215.                 if($this->do_debug >= 1{
  216.                     echo "SMTP -> NOTICE:" $this->CRLF .
  217.                          "EOF caught while checking if connected";
  218.                 }
  219.                 $this->Close();
  220.                 return false;
  221.             }
  222.             return true# everything looks good
  223.         }
  224.         return false;
  225.     }
  226.  
  227.     /**
  228.      * Closes the socket and cleans up the state of the class.
  229.      * It is not considered good to use this function without
  230.      * first trying to use QUIT.
  231.      * @access public
  232.      * @return void 
  233.      */
  234.     function Close({
  235.         $this->error null# so there is no confusion
  236.         $this->helo_rply null;
  237.         if(!empty($this->smtp_conn)) {
  238.             # close the connection and cleanup
  239.             fclose($this->smtp_conn);
  240.             $this->smtp_conn 0;
  241.         }
  242.     }
  243.  
  244.  
  245.     /***************************************************************
  246.      *                        SMTP COMMANDS                       *
  247.      *************************************************************/
  248.  
  249.     /**
  250.      * Issues a data command and sends the msg_data to the server
  251.      * finializing the mail transaction. $msg_data is the message
  252.      * that is to be send with the headers. Each header needs to be
  253.      * on a single line followed by a <CRLF> with the message headers
  254.      * and the message body being seperated by and additional <CRLF>.
  255.      *
  256.      * Implements rfc 821: DATA <CRLF>
  257.      *
  258.      * SMTP CODE INTERMEDIATE: 354
  259.      *     [data]
  260.      *     <CRLF>.<CRLF>
  261.      *     SMTP CODE SUCCESS: 250
  262.      *     SMTP CODE FAILURE: 552,554,451,452
  263.      * SMTP CODE FAILURE: 451,554
  264.      * SMTP CODE ERROR  : 500,501,503,421
  265.      * @access public
  266.      * @return bool 
  267.      */
  268.     function Data($msg_data{
  269.         $this->error null# so no confusion is caused
  270.  
  271.         if(!$this->connected()) {
  272.             $this->error array(
  273.                     "error" => "Called Data() without being connected");
  274.             return false;
  275.         }
  276.  
  277.         fputs($this->smtp_conn,"DATA" $this->CRLF);
  278.  
  279.         $rply $this->get_lines();
  280.         $code substr($rply,0,3);
  281.  
  282.         if($this->do_debug >= 2{
  283.             echo "SMTP -> FROM SERVER:" $this->CRLF . $rply;
  284.         }
  285.  
  286.         if($code != 354{
  287.             $this->error =
  288.                 array("error" => "DATA command not accepted from server",
  289.                       "smtp_code" => $code,
  290.                       "smtp_msg" => substr($rply,4));
  291.             if($this->do_debug >= 1{
  292.                 echo "SMTP -> ERROR: " $this->error["error".
  293.                          ": " $rply $this->CRLF;
  294.             }
  295.             return false;
  296.         }
  297.  
  298.         # the server is ready to accept data!
  299.         # according to rfc 821 we should not send more than 1000
  300.         # including the CRLF
  301.         # characters on a single line so we will break the data up
  302.         # into lines by \r and/or \n then if needed we will break
  303.         # each of those into smaller lines to fit within the limit.
  304.         # in addition we will be looking for lines that start with
  305.         # a period '.' and append and additional period '.' to that
  306.         # line. NOTE: this does not count towards are limit.
  307.  
  308.         # normalize the line breaks so we know the explode works
  309.         $msg_data str_replace("\r\n","\n",$msg_data);
  310.         $msg_data str_replace("\r","\n",$msg_data);
  311.         $lines explode("\n",$msg_data);
  312.  
  313.         # we need to find a good way to determine is headers are
  314.         # in the msg_data or if it is a straight msg body
  315.         # currently I'm assuming rfc 822 definitions of msg headers
  316.         # and if the first field of the first line (':' sperated)
  317.         # does not contain a space then it _should_ be a header
  318.         # and we can process all lines before a blank "" line as
  319.         # headers.
  320.         $field substr($lines[0],0,strpos($lines[0],":"));
  321.         $in_headers false;
  322.         if(!empty($field&& !strstr($field," ")) {
  323.             $in_headers true;
  324.         }
  325.  
  326.         $max_line_length 998# used below; set here for ease in change
  327.  
  328.         while(list(,$line@each($lines)) {
  329.             $lines_out null;
  330.             if($line == "" && $in_headers{
  331.                 $in_headers false;
  332.             }
  333.             # ok we need to break this line up into several
  334.             # smaller lines
  335.             while(strlen($line$max_line_length{
  336.                 $pos strrpos(substr($line,0,$max_line_length)," ");
  337.                 $lines_out[substr($line,0,$pos);
  338.                 $line substr($line,$pos 1);
  339.                 # if we are processing headers we need to
  340.                 # add a LWSP-char to the front of the new line
  341.                 # rfc 822 on long msg headers
  342.                 if($in_headers{
  343.                     $line "\t" $line;
  344.                 }
  345.             }
  346.             $lines_out[$line;
  347.  
  348.             # now send the lines to the server
  349.             while(list(,$line_out@each($lines_out)) {
  350.                 if(strlen($line_out0)
  351.                 {
  352.                     if(substr($line_out01== "."{
  353.                         $line_out "." $line_out;
  354.                     }
  355.                 }
  356.                 fputs($this->smtp_conn,$line_out $this->CRLF);
  357.             }
  358.         }
  359.  
  360.         # ok all the message data has been sent so lets get this
  361.         # over with aleady
  362.         fputs($this->smtp_conn$this->CRLF . "." $this->CRLF);
  363.  
  364.         $rply $this->get_lines();
  365.         $code substr($rply,0,3);
  366.  
  367.         if($this->do_debug >= 2{
  368.             echo "SMTP -> FROM SERVER:" $this->CRLF . $rply;
  369.         }
  370.  
  371.         if($code != 250{
  372.             $this->error =
  373.                 array("error" => "DATA not accepted from server",
  374.                       "smtp_code" => $code,
  375.                       "smtp_msg" => substr($rply,4));
  376.             if($this->do_debug >= 1{
  377.                 echo "SMTP -> ERROR: " $this->error["error".
  378.                          ": " $rply $this->CRLF;
  379.             }
  380.             return false;
  381.         }
  382.         return true;
  383.     }
  384.  
  385.     /**
  386.      * Expand takes the name and asks the server to list all the
  387.      * people who are members of the _list_. Expand will return
  388.      * back and array of the result or false if an error occurs.
  389.      * Each value in the array returned has the format of:
  390.      *     [ <full-name> <sp> ] <path>
  391.      * The definition of <path> is defined in rfc 821
  392.      *
  393.      * Implements rfc 821: EXPN <SP> <string> <CRLF>
  394.      *
  395.      * SMTP CODE SUCCESS: 250
  396.      * SMTP CODE FAILURE: 550
  397.      * SMTP CODE ERROR  : 500,501,502,504,421
  398.      * @access public
  399.      * @return string array
  400.      */
  401.     function Expand($name{
  402.         $this->error null# so no confusion is caused
  403.  
  404.         if(!$this->connected()) {
  405.             $this->error array(
  406.                     "error" => "Called Expand() without being connected");
  407.             return false;
  408.         }
  409.  
  410.         fputs($this->smtp_conn,"EXPN " $name $this->CRLF);
  411.  
  412.         $rply $this->get_lines();
  413.         $code substr($rply,0,3);
  414.  
  415.         if($this->do_debug >= 2{
  416.             echo "SMTP -> FROM SERVER:" $this->CRLF . $rply;
  417.         }
  418.  
  419.         if($code != 250{
  420.             $this->error =
  421.                 array("error" => "EXPN not accepted from server",
  422.                       "smtp_code" => $code,
  423.                       "smtp_msg" => substr($rply,4));
  424.             if($this->do_debug >= 1{
  425.                 echo "SMTP -> ERROR: " $this->error["error".
  426.                          ": " $rply $this->CRLF;
  427.             }
  428.             return false;
  429.         }
  430.  
  431.         # parse the reply and place in our array to return to user
  432.         $entries explode($this->CRLF,$rply);
  433.         while(list(,$l@each($entries)) {
  434.             $list[substr($l,4);
  435.         }
  436.  
  437.         return $list;
  438.     }
  439.  
  440.     /**
  441.      * Sends the HELO command to the smtp server.
  442.      * This makes sure that we and the server are in
  443.      * the same known state.
  444.      *
  445.      * Implements from rfc 821: HELO <SP> <domain> <CRLF>
  446.      *
  447.      * SMTP CODE SUCCESS: 250
  448.      * SMTP CODE ERROR  : 500, 501, 504, 421
  449.      * @access public
  450.      * @return bool 
  451.      */
  452.     function Hello($host=""{
  453.         $this->error null# so no confusion is caused
  454.  
  455.         if(!$this->connected()) {
  456.             $this->error array(
  457.                     "error" => "Called Hello() without being connected");
  458.             return false;
  459.         }
  460.  
  461.         # if a hostname for the HELO wasn't specified determine
  462.         # a suitable one to send
  463.         if(empty($host)) {
  464.             # we need to determine some sort of appopiate default
  465.             # to send to the server
  466.             $host "localhost";
  467.         }
  468.  
  469.         // Send extended hello first (RFC 2821)
  470.         if(!$this->SendHello("EHLO"$host))
  471.         {
  472.             if(!$this->SendHello("HELO"$host))
  473.                 return false;
  474.         }
  475.  
  476.         return true;
  477.     }
  478.  
  479.     /**
  480.      * Sends a HELO/EHLO command.
  481.      * @access private
  482.      * @return bool 
  483.      */
  484.     function SendHello($hello$host{
  485.         fputs($this->smtp_conn$hello " " $host $this->CRLF);
  486.  
  487.         $rply $this->get_lines();
  488.         $code substr($rply,0,3);
  489.  
  490.         if($this->do_debug >= 2{
  491.             echo "SMTP -> FROM SERVER: " $this->CRLF . $rply;
  492.         }
  493.  
  494.         if($code != 250{
  495.             $this->error =
  496.                 array("error" => $hello " not accepted from server",
  497.                       "smtp_code" => $code,
  498.                       "smtp_msg" => substr($rply,4));
  499.             if($this->do_debug >= 1{
  500.                 echo "SMTP -> ERROR: " $this->error["error".
  501.                          ": " $rply $this->CRLF;
  502.             }
  503.             return false;
  504.         }
  505.  
  506.         $this->helo_rply $rply;
  507.         
  508.         return true;
  509.     }
  510.  
  511.     /**
  512.      * Gets help information on the keyword specified. If the keyword
  513.      * is not specified then returns generic help, ussually contianing
  514.      * A list of keywords that help is available on. This function
  515.      * returns the results back to the user. It is up to the user to
  516.      * handle the returned data. If an error occurs then false is
  517.      * returned with $this->error set appropiately.
  518.      *
  519.      * Implements rfc 821: HELP [ <SP> <string> ] <CRLF>
  520.      *
  521.      * SMTP CODE SUCCESS: 211,214
  522.      * SMTP CODE ERROR  : 500,501,502,504,421
  523.      * @access public
  524.      * @return string 
  525.      */
  526.     function Help($keyword=""{
  527.         $this->error null# to avoid confusion
  528.  
  529.         if(!$this->connected()) {
  530.             $this->error array(
  531.                     "error" => "Called Help() without being connected");
  532.             return false;
  533.         }
  534.  
  535.         $extra "";
  536.         if(!empty($keyword)) {
  537.             $extra " " $keyword;
  538.         }
  539.  
  540.         fputs($this->smtp_conn,"HELP" $extra $this->CRLF);
  541.  
  542.         $rply $this->get_lines();
  543.         $code substr($rply,0,3);
  544.  
  545.         if($this->do_debug >= 2{
  546.             echo "SMTP -> FROM SERVER:" $this->CRLF . $rply;
  547.         }
  548.  
  549.         if($code != 211 && $code != 214{
  550.             $this->error =
  551.                 array("error" => "HELP not accepted from server",
  552.                       "smtp_code" => $code,
  553.                       "smtp_msg" => substr($rply,4));
  554.             if($this->do_debug >= 1{
  555.                 echo "SMTP -> ERROR: " $this->error["error".
  556.                          ": " $rply $this->CRLF;
  557.             }
  558.             return false;
  559.         }
  560.  
  561.         return $rply;
  562.     }
  563.  
  564.     /**
  565.      * Starts a mail transaction from the email address specified in
  566.      * $from. Returns true if successful or false otherwise. If True
  567.      * the mail transaction is started and then one or more Recipient
  568.      * commands may be called followed by a Data command.
  569.      *
  570.      * Implements rfc 821: MAIL <SP> FROM:<reverse-path> <CRLF>
  571.      *
  572.      * SMTP CODE SUCCESS: 250
  573.      * SMTP CODE SUCCESS: 552,451,452
  574.      * SMTP CODE SUCCESS: 500,501,421
  575.      * @access public
  576.      * @return bool 
  577.      */
  578.     function Mail($from{
  579.         $this->error null# so no confusion is caused
  580.  
  581.         if(!$this->connected()) {
  582.             $this->error array(
  583.                     "error" => "Called Mail() without being connected");
  584.             return false;
  585.         }
  586.  
  587.         fputs($this->smtp_conn,"MAIL FROM:<" $from ">" $this->CRLF);
  588.  
  589.         $rply $this->get_lines();
  590.         $code substr($rply,0,3);
  591.  
  592.         if($this->do_debug >= 2{
  593.             echo "SMTP -> FROM SERVER:" $this->CRLF . $rply;
  594.         }
  595.  
  596.         if($code != 250{
  597.             $this->error =
  598.                 array("error" => "MAIL not accepted from server",
  599.                       "smtp_code" => $code,
  600.                       "smtp_msg" => substr($rply,4));
  601.             if($this->do_debug >= 1{
  602.                 echo "SMTP -> ERROR: " $this->error["error".
  603.                          ": " $rply $this->CRLF;
  604.             }
  605.             return false;
  606.         }
  607.         return true;
  608.     }
  609.  
  610.     /**
  611.      * Sends the command NOOP to the SMTP server.
  612.      *
  613.      * Implements from rfc 821: NOOP <CRLF>
  614.      *
  615.      * SMTP CODE SUCCESS: 250
  616.      * SMTP CODE ERROR  : 500, 421
  617.      * @access public
  618.      * @return bool 
  619.      */
  620.     function Noop({
  621.         $this->error null# so no confusion is caused
  622.  
  623.         if(!$this->connected()) {
  624.             $this->error array(
  625.                     "error" => "Called Noop() without being connected");
  626.             return false;
  627.         }
  628.  
  629.         fputs($this->smtp_conn,"NOOP" $this->CRLF);
  630.  
  631.         $rply $this->get_lines();
  632.         $code substr($rply,0,3);
  633.  
  634.         if($this->do_debug >= 2{
  635.             echo "SMTP -> FROM SERVER:" $this->CRLF . $rply;
  636.         }
  637.  
  638.         if($code != 250{
  639.             $this->error =
  640.                 array("error" => "NOOP not accepted from server",
  641.                       "smtp_code" => $code,
  642.                       "smtp_msg" => substr($rply,4));
  643.             if($this->do_debug >= 1{
  644.                 echo "SMTP -> ERROR: " $this->error["error".
  645.                          ": " $rply $this->CRLF;
  646.             }
  647.             return false;
  648.         }
  649.         return true;
  650.     }
  651.  
  652.     /**
  653.      * Sends the quit command to the server and then closes the socket
  654.      * if there is no error or the $close_on_error argument is true.
  655.      *
  656.      * Implements from rfc 821: QUIT <CRLF>
  657.      *
  658.      * SMTP CODE SUCCESS: 221
  659.      * SMTP CODE ERROR  : 500
  660.      * @access public
  661.      * @return bool 
  662.      */
  663.     function Quit($close_on_error=true{
  664.         $this->error null# so there is no confusion
  665.  
  666.         if(!$this->connected()) {
  667.             $this->error array(
  668.                     "error" => "Called Quit() without being connected");
  669.             return false;
  670.         }
  671.  
  672.         # send the quit command to the server
  673.         fputs($this->smtp_conn,"quit" $this->CRLF);
  674.  
  675.         # get any good-bye messages
  676.         $byemsg $this->get_lines();
  677.  
  678.         if($this->do_debug >= 2{
  679.             echo "SMTP -> FROM SERVER:" $this->CRLF . $byemsg;
  680.         }
  681.  
  682.         $rval true;
  683.         $e null;
  684.  
  685.         $code substr($byemsg,0,3);
  686.         if($code != 221{
  687.             # use e as a tmp var cause Close will overwrite $this->error
  688.             $e array("error" => "SMTP server rejected quit command",
  689.                        "smtp_code" => $code,
  690.                        "smtp_rply" => substr($byemsg,4));
  691.             $rval false;
  692.             if($this->do_debug >= 1{
  693.                 echo "SMTP -> ERROR: " $e["error"": " .
  694.                          $byemsg $this->CRLF;
  695.             }
  696.         }
  697.  
  698.         if(empty($e|| $close_on_error{
  699.             $this->Close();
  700.         }
  701.  
  702.         return $rval;
  703.     }
  704.  
  705.     /**
  706.      * Sends the command RCPT to the SMTP server with the TO: argument of $to.
  707.      * Returns true if the recipient was accepted false if it was rejected.
  708.      *
  709.      * Implements from rfc 821: RCPT <SP> TO:<forward-path> <CRLF>
  710.      *
  711.      * SMTP CODE SUCCESS: 250,251
  712.      * SMTP CODE FAILURE: 550,551,552,553,450,451,452
  713.      * SMTP CODE ERROR  : 500,501,503,421
  714.      * @access public
  715.      * @return bool 
  716.      */
  717.     function Recipient($to{
  718.         $this->error null# so no confusion is caused
  719.  
  720.         if(!$this->connected()) {
  721.             $this->error array(
  722.                     "error" => "Called Recipient() without being connected");
  723.             return false;
  724.         }
  725.  
  726.         fputs($this->smtp_conn,"RCPT TO:<" $to ">" $this->CRLF);
  727.  
  728.         $rply $this->get_lines();
  729.         $code substr($rply,0,3);
  730.  
  731.         if($this->do_debug >= 2{
  732.             echo "SMTP -> FROM SERVER:" $this->CRLF . $rply;
  733.         }
  734.  
  735.         if($code != 250 && $code != 251{
  736.             $this->error =
  737.                 array("error" => "RCPT not accepted from server",
  738.                       "smtp_code" => $code,
  739.                       "smtp_msg" => substr($rply,4));
  740.             if($this->do_debug >= 1{
  741.                 echo "SMTP -> ERROR: " $this->error["error".
  742.                          ": " $rply $this->CRLF;
  743.             }
  744.             return false;
  745.         }
  746.         return true;
  747.     }
  748.  
  749.     /**
  750.      * Sends the RSET command to abort and transaction that is
  751.      * currently in progress. Returns true if successful false
  752.      * otherwise.
  753.      *
  754.      * Implements rfc 821: RSET <CRLF>
  755.      *
  756.      * SMTP CODE SUCCESS: 250
  757.      * SMTP CODE ERROR  : 500,501,504,421
  758.      * @access public
  759.      * @return bool 
  760.      */
  761.     function Reset({
  762.         $this->error null# so no confusion is caused
  763.  
  764.         if(!$this->connected()) {
  765.             $this->error array(
  766.                     "error" => "Called Reset() without being connected");
  767.             return false;
  768.         }
  769.  
  770.         fputs($this->smtp_conn,"RSET" $this->CRLF);
  771.  
  772.         $rply $this->get_lines();
  773.         $code substr($rply,0,3);
  774.  
  775.         if($this->do_debug >= 2{
  776.             echo "SMTP -> FROM SERVER:" $this->CRLF . $rply;
  777.         }
  778.  
  779.         if($code != 250{
  780.             $this->error =
  781.                 array("error" => "RSET failed",
  782.                       "smtp_code" => $code,
  783.                       "smtp_msg" => substr($rply,4));
  784.             if($this->do_debug >= 1{
  785.                 echo "SMTP -> ERROR: " $this->error["error".
  786.                          ": " $rply $this->CRLF;
  787.             }
  788.             return false;
  789.         }
  790.  
  791.         return true;
  792.     }
  793.  
  794.     /**
  795.      * Starts a mail transaction from the email address specified in
  796.      * $from. Returns true if successful or false otherwise. If True
  797.      * the mail transaction is started and then one or more Recipient
  798.      * commands may be called followed by a Data command. This command
  799.      * will send the message to the users terminal if they are logged
  800.      * in.
  801.      *
  802.      * Implements rfc 821: SEND <SP> FROM:<reverse-path> <CRLF>
  803.      *
  804.      * SMTP CODE SUCCESS: 250
  805.      * SMTP CODE SUCCESS: 552,451,452
  806.      * SMTP CODE SUCCESS: 500,501,502,421
  807.      * @access public
  808.      * @return bool 
  809.      */
  810.     function Send($from{
  811.         $this->error null# so no confusion is caused
  812.  
  813.         if(!$this->connected()) {
  814.             $this->error array(
  815.                     "error" => "Called Send() without being connected");
  816.             return false;
  817.         }
  818.  
  819.         fputs($this->smtp_conn,"SEND FROM:" $from $this->CRLF);
  820.  
  821.         $rply $this->get_lines();
  822.         $code substr($rply,0,3);
  823.  
  824.         if($this->do_debug >= 2{
  825.             echo "SMTP -> FROM SERVER:" $this->CRLF . $rply;
  826.         }
  827.  
  828.         if($code != 250{
  829.             $this->error =
  830.                 array("error" => "SEND not accepted from server",
  831.                       "smtp_code" => $code,
  832.                       "smtp_msg" => substr($rply,4));
  833.             if($this->do_debug >= 1{
  834.                 echo "SMTP -> ERROR: " $this->error["error".
  835.                          ": " $rply $this->CRLF;
  836.             }
  837.             return false;
  838.         }
  839.         return true;
  840.     }
  841.  
  842.     /**
  843.      * Starts a mail transaction from the email address specified in
  844.      * $from. Returns true if successful or false otherwise. If True
  845.      * the mail transaction is started and then one or more Recipient
  846.      * commands may be called followed by a Data command. This command
  847.      * will send the message to the users terminal if they are logged
  848.      * in and send them an email.
  849.      *
  850.      * Implements rfc 821: SAML <SP> FROM:<reverse-path> <CRLF>
  851.      *
  852.      * SMTP CODE SUCCESS: 250
  853.      * SMTP CODE SUCCESS: 552,451,452
  854.      * SMTP CODE SUCCESS: 500,501,502,421
  855.      * @access public
  856.      * @return bool 
  857.      */
  858.     function SendAndMail($from{
  859.         $this->error null# so no confusion is caused
  860.  
  861.         if(!$this->connected()) {
  862.             $this->error array(
  863.                 "error" => "Called SendAndMail() without being connected");
  864.             return false;
  865.         }
  866.  
  867.         fputs($this->smtp_conn,"SAML FROM:" $from $this->CRLF);
  868.  
  869.         $rply $this->get_lines();
  870.         $code substr($rply,0,3);
  871.  
  872.         if($this->do_debug >= 2{
  873.             echo "SMTP -> FROM SERVER:" $this->CRLF . $rply;
  874.         }
  875.  
  876.         if($code != 250{
  877.             $this->error =
  878.                 array("error" => "SAML not accepted from server",
  879.                       "smtp_code" => $code,
  880.                       "smtp_msg" => substr($rply,4));
  881.             if($this->do_debug >= 1{
  882.                 echo "SMTP -> ERROR: " $this->error["error".
  883.                          ": " $rply $this->CRLF;
  884.             }
  885.             return false;
  886.         }
  887.         return true;
  888.     }
  889.  
  890.     /**
  891.      * Starts a mail transaction from the email address specified in
  892.      * $from. Returns true if successful or false otherwise. If True
  893.      * the mail transaction is started and then one or more Recipient
  894.      * commands may be called followed by a Data command. This command
  895.      * will send the message to the users terminal if they are logged
  896.      * in or mail it to them if they are not.
  897.      *
  898.      * Implements rfc 821: SOML <SP> FROM:<reverse-path> <CRLF>
  899.      *
  900.      * SMTP CODE SUCCESS: 250
  901.      * SMTP CODE SUCCESS: 552,451,452
  902.      * SMTP CODE SUCCESS: 500,501,502,421
  903.      * @access public
  904.      * @return bool 
  905.      */
  906.     function SendOrMail($from{
  907.         $this->error null# so no confusion is caused
  908.  
  909.         if(!$this->connected()) {
  910.             $this->error array(
  911.                 "error" => "Called SendOrMail() without being connected");
  912.             return false;
  913.         }
  914.  
  915.         fputs($this->smtp_conn,"SOML FROM:" $from $this->CRLF);
  916.  
  917.         $rply $this->get_lines();
  918.         $code substr($rply,0,3);
  919.  
  920.         if($this->do_debug >= 2{
  921.             echo "SMTP -> FROM SERVER:" $this->CRLF . $rply;
  922.         }
  923.  
  924.         if($code != 250{
  925.             $this->error =
  926.                 array("error" => "SOML not accepted from server",
  927.                       "smtp_code" => $code,
  928.                       "smtp_msg" => substr($rply,4));
  929.             if($this->do_debug >= 1{
  930.                 echo "SMTP -> ERROR: " $this->error["error".
  931.                          ": " $rply $this->CRLF;
  932.             }
  933.             return false;
  934.         }
  935.         return true;
  936.     }
  937.  
  938.     /**
  939.      * This is an optional command for SMTP that this class does not
  940.      * support. This method is here to make the RFC821 Definition
  941.      * complete for this class and __may__ be implimented in the future
  942.      *
  943.      * Implements from rfc 821: TURN <CRLF>
  944.      *
  945.      * SMTP CODE SUCCESS: 250
  946.      * SMTP CODE FAILURE: 502
  947.      * SMTP CODE ERROR  : 500, 503
  948.      * @access public
  949.      * @return bool 
  950.      */
  951.     function Turn({
  952.         $this->error array("error" => "This method, TURN, of the SMTP ".
  953.                                         "is not implemented");
  954.         if($this->do_debug >= 1{
  955.             echo "SMTP -> NOTICE: " $this->error["error"$this->CRLF;
  956.         }
  957.         return false;
  958.     }
  959.  
  960.     /**
  961.      * Verifies that the name is recognized by the server.
  962.      * Returns false if the name could not be verified otherwise
  963.      * the response from the server is returned.
  964.      *
  965.      * Implements rfc 821: VRFY <SP> <string> <CRLF>
  966.      *
  967.      * SMTP CODE SUCCESS: 250,251
  968.      * SMTP CODE FAILURE: 550,551,553
  969.      * SMTP CODE ERROR  : 500,501,502,421
  970.      * @access public
  971.      * @return int 
  972.      */
  973.     function Verify($name{
  974.         $this->error null# so no confusion is caused
  975.  
  976.         if(!$this->connected()) {
  977.             $this->error array(
  978.                     "error" => "Called Verify() without being connected");
  979.             return false;
  980.         }
  981.  
  982.         fputs($this->smtp_conn,"VRFY " $name $this->CRLF);
  983.  
  984.         $rply $this->get_lines();
  985.         $code substr($rply,0,3);
  986.  
  987.         if($this->do_debug >= 2{
  988.             echo "SMTP -> FROM SERVER:" $this->CRLF . $rply;
  989.         }
  990.  
  991.         if($code != 250 && $code != 251{
  992.             $this->error =
  993.                 array("error" => "VRFY failed on name '$name'",
  994.                       "smtp_code" => $code,
  995.                       "smtp_msg" => substr($rply,4));
  996.             if($this->do_debug >= 1{
  997.                 echo "SMTP -> ERROR: " $this->error["error".
  998.                          ": " $rply $this->CRLF;
  999.             }
  1000.             return false;
  1001.         }
  1002.         return $rply;
  1003.     }
  1004.  
  1005.     /*******************************************************************
  1006.      *                       INTERNAL FUNCTIONS                       *
  1007.      ******************************************************************/
  1008.  
  1009.     /**
  1010.      * Read in as many lines as possible
  1011.      * either before eof or socket timeout occurs on the operation.
  1012.      * With SMTP we can tell if we have more lines to read if the
  1013.      * 4th character is '-' symbol. If it is a space then we don't
  1014.      * need to read anything else.
  1015.      * @access private
  1016.      * @return string 
  1017.      */
  1018.     function get_lines({
  1019.         $data "";
  1020.         while($str fgets($this->smtp_conn,515)) {
  1021.             if($this->do_debug >= 4{
  1022.                 echo "SMTP -> get_lines(): \$data was \"$data\".
  1023.                          $this->CRLF;
  1024.                 echo "SMTP -> get_lines(): \$str is \"$str\".
  1025.                          $this->CRLF;
  1026.             }
  1027.             $data .= $str;
  1028.             if($this->do_debug >= 4{
  1029.                 echo "SMTP -> get_lines(): \$data is \"$data\"$this->CRLF;
  1030.             }
  1031.             # if the 4th character is a space then we are done reading
  1032.             # so just break the loop
  1033.             if(substr($str,3,1== " "break}
  1034.         }
  1035.         return $data;
  1036.     }
  1037.  
  1038. }
  1039.  
  1040.  
  1041.  ?>

Documentation generated on Fri, 16 Oct 2009 09:30:24 +0200 by phpDocumentor 1.4.1