Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: PHPMailer

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /phpmailer/smtp.php

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

Documentation generated on Mon, 05 Mar 2007 21:26:18 +0000 by phpDocumentor 1.3.1