Source for file class.smtp.php

Documentation is available at class.smtp.php

  1. <?php
  2.  
  3.  
  4. /**
  5.  * @author  Richard Heyes <richard@phpguru.org>
  6.  * @version $Revision: 1.1 $
  7.  * @package src
  8.  * @subpackage mail
  9.  */
  10.  
  11. /**
  12.  * @staticvar  SMTP_STATUS_NOT_CONNECTED 
  13.  */
  14. define('SMTP_STATUS_NOT_CONNECTED'1true);
  15.  
  16. /**
  17.  * @staticvar  SMTP_STATUS_CONNECTED 
  18.  */
  19. define('SMTP_STATUS_CONNECTED'2true);
  20.  
  21. /**
  22.  * This file is part of the htmlMimeMail5 package (http://www.phpguru.org/)
  23.  *
  24.  * htmlMimeMail5 is free software; you can redistribute it and/or modify
  25.  * it under the terms of the GNU General Public License as published by
  26.  * the Free Software Foundation; either version 2 of the License, or
  27.  * (at your option) any later version.
  28.  *
  29.  * htmlMimeMail5 is distributed in the hope that it will be useful,
  30.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  32.  * GNU General Public License for more details.
  33.  *
  34.  * You should have received a copy of the GNU General Public License
  35.  * along with htmlMimeMail5; if not, write to the Free Software
  36.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  37.  *
  38.  * @author  Richard Heyes <richard@phpguru.org>
  39.  * @version $Revision: 1.1 $
  40.  * @package src
  41.  * @subpackage mail
  42.  */
  43. class src_mail_smtp {
  44.    private $authenticated;
  45.    private $connection;
  46.    private $recipients;
  47.    private $headers;
  48.    private $timeout;
  49.    private $errors;
  50.    private $status;
  51.    private $body;
  52.    private $from;
  53.    private $host;
  54.    private $port;
  55.    private $helo;
  56.    private $auth;
  57.    private $user;
  58.    private $pass;
  59.  
  60.    /**
  61.     * Constructor function. Arguments:
  62.     * $params - An assoc array of parameters:
  63.     *
  64.     *   host    - The hostname of the smtp server       Default: localhost
  65.     *   port    - The port the smtp server runs on      Default: 25
  66.     *   helo    - What to send as the HELO command      Default: localhost
  67.     *             (typically the hostname of the
  68.     *             machine this script runs on)
  69.     *   auth    - Whether to use basic authentication   Default: FALSE
  70.     *   user    - Username for authentication           Default: <blank>
  71.     *   pass    - Password for authentication           Default: <blank>
  72.     *   timeout - The timeout in seconds for the call   Default: 5
  73.     *             to fsockopen()
  74.     */
  75.    public function __construct($params array ()) {
  76.  
  77.       if (!defined('CRLF'))
  78.          define('CRLF'"\r\n"TRUE);
  79.  
  80.       $this->authenticated FALSE;
  81.       $this->timeout 5;
  82.       $this->status SMTP_STATUS_NOT_CONNECTED;
  83.       $this->host 'localhost';
  84.       $this->port 25;
  85.       $this->helo 'localhost';
  86.       $this->auth FALSE;
  87.       $this->user '';
  88.       $this->pass '';
  89.       $this->errors array ();
  90.  
  91.       foreach ($params as $key => $value{
  92.          $this-> $key $value;
  93.       }
  94.    }
  95.  
  96.    /**
  97.     * Connect function. This will, when called
  98.     * statically, create a new smtp object,
  99.     * call the connect function (ie this function)
  100.     * and return it. When not called statically,
  101.     * it will connect to the server and send
  102.     * the HELO command.
  103.     */
  104.    public function connect($params array ()) {
  105.       if (!isset ($this->status)) {
  106.          $obj new smtp($params);
  107.          if ($obj->connect()) {
  108.             $obj->status SMTP_STATUS_CONNECTED;
  109.          }
  110.  
  111.          return $obj;
  112.  
  113.       else {
  114.          $this->connection fsockopen($this->host$this->port$errno$errstr$this->timeout);
  115.          if (function_exists('socket_set_timeout')) {
  116.             socket_set_timeout($this->connection50);
  117.          }
  118.  
  119.          $greeting $this->get_data();
  120.          if (is_resource($this->connection)) {
  121.             return $this->auth $this->ehlo($this->helo();
  122.          else {
  123.             $this->errors['Failed to connect to server: ' $errstr;
  124.             return FALSE;
  125.          }
  126.       }
  127.    }
  128.  
  129.    /**
  130.     * Function which handles sending the mail.
  131.     * Arguments:
  132.     * $params   - Optional assoc array of parameters.
  133.     *            Can contain:
  134.     *              recipients - Indexed array of recipients
  135.     *              from       - The from address. (used in MAIL FROM:),
  136.     *                           this will be the return path
  137.     *              headers    - Indexed array of headers, one header per array entry
  138.     *              body       - The body of the email
  139.     *            It can also contain any of the parameters from the connect()
  140.     *            function
  141.     */
  142.    public function send($params array ()) {
  143.       foreach ($params as $key => $value{
  144.          $this->set($key$value);
  145.       }
  146.  
  147.       if ($this->is_connected()) {
  148.  
  149.          // Do we auth or not? Note the distinction between the auth variable and auth() function
  150.          if ($this->auth AND !$this->authenticated{
  151.             if (!$this->auth())
  152.                return false;
  153.          }
  154.  
  155.          $this->mail($this->from);
  156.  
  157.          if (is_array($this->recipients)) {
  158.             foreach ($this->recipients as $value{
  159.                $this->rcpt($value);
  160.             }
  161.          else {
  162.             $this->rcpt($this->recipients);
  163.          }
  164.  
  165.          if (!$this->data()) {
  166.             return false;
  167.          }
  168.  
  169.          // Transparency
  170.          $headers str_replace(CRLF '.'CRLF '..'trim(implode(CRLF$this->headers)));
  171.          $body str_replace(CRLF '.'CRLF '..'$this->body);
  172.          $body substr($body01== '.' '.' $body $body;
  173.  
  174.          $this->send_data($headers);
  175.          $this->send_data('');
  176.          $this->send_data($body);
  177.          $this->send_data('.');
  178.  
  179.          $result (substr(trim($this->get_data())03=== '250');
  180.          //$this->rset();
  181.          return $result;
  182.       else {
  183.          $this->errors['Not connected!';
  184.          return FALSE;
  185.       }
  186.    }
  187.  
  188.    /**
  189.     * Function to implement HELO cmd
  190.     */
  191.    private function helo({
  192.       if (is_resource($this->connectionAND $this->send_data('HELO ' $this->heloAND substr(trim($error $this->get_data())03=== '250'{
  193.  
  194.          return true;
  195.  
  196.       else {
  197.          $this->errors['HELO command failed, output: ' trim(substr(trim($error)3));
  198.          return false;
  199.       }
  200.    }
  201.  
  202.    /**
  203.     * Function to implement EHLO cmd
  204.     */
  205.    private function ehlo({
  206.       if (is_resource($this->connectionAND $this->send_data('EHLO ' $this->heloAND substr(trim($error $this->get_data())03=== '250'{
  207.  
  208.          return true;
  209.  
  210.       else {
  211.          $this->errors['EHLO command failed, output: ' trim(substr(trim($error)3));
  212.          return false;
  213.       }
  214.    }
  215.  
  216.    /**
  217.     * Function to implement RSET cmd
  218.     */
  219.    private function rset({
  220.       if (is_resource($this->connectionAND $this->send_data('RSET'AND substr(trim($error $this->get_data())03=== '250'{
  221.  
  222.          return true;
  223.  
  224.       else {
  225.          $this->errors['RSET command failed, output: ' trim(substr(trim($error)3));
  226.          return false;
  227.       }
  228.    }
  229.  
  230.    /**
  231.     * Function to implement QUIT cmd
  232.     */
  233.    private function quit({
  234.       if (is_resource($this->connectionAND $this->send_data('QUIT'AND substr(trim($error $this->get_data())03=== '221'{
  235.  
  236.          fclose($this->connection);
  237.          $this->status SMTP_STATUS_NOT_CONNECTED;
  238.          return true;
  239.  
  240.       else {
  241.          $this->errors['QUIT command failed, output: ' trim(substr(trim($error)3));
  242.          return false;
  243.       }
  244.    }
  245.  
  246.    /**
  247.     * Function to implement AUTH cmd
  248.     */
  249.    private function auth({
  250.             if (is_resource($this->connectionAND $this->send_data('AUTH LOGIN'AND substr(trim($error $this->get_data())03=== '334' AND $this->send_data(base64_encode($this->user)) // Send username
  251.             AND substr(trim($error $this->get_data())03=== '334' AND $this->send_data(base64_encode($this->pass)) // Send password
  252.             AND substr(trim($error $this->get_data())03=== '235'{
  253.  
  254.          $this->authenticated true;
  255.          return true;
  256.  
  257.       else {
  258.          $this->errors['AUTH command failed: ' trim(substr(trim($error)3));
  259.          return false;
  260.       }
  261.    }
  262.  
  263.    /**
  264.     * Function that handles the MAIL FROM: cmd
  265.     */
  266.    private function mail($from{
  267.       if ($this->is_connected(AND $this->send_data('MAIL FROM:<' $from '>'AND substr(trim($this->get_data())02=== '250'{
  268.  
  269.          return true;
  270.  
  271.       else {
  272.          return false;
  273.       }
  274.    }
  275.  
  276.    /**
  277.     * Function that handles the RCPT TO: cmd
  278.     */
  279.    private function rcpt($to{
  280.       if ($this->is_connected(AND $this->send_data('RCPT TO:<' $to '>'AND substr(trim($error $this->get_data())02=== '25'{
  281.  
  282.          return true;
  283.  
  284.       else {
  285.          $this->errors[trim(substr(trim($error)3));
  286.          return false;
  287.       }
  288.    }
  289.  
  290.    /**
  291.     * Function that sends the DATA cmd
  292.     */
  293.    private function data({
  294.       if ($this->is_connected(AND $this->send_data('DATA'AND substr(trim($error $this->get_data())03=== '354'{
  295.  
  296.          return true;
  297.  
  298.       else {
  299.          $this->errors[trim(substr(trim($error)3));
  300.          return false;
  301.       }
  302.    }
  303.  
  304.    /**
  305.     * Function to determine if this object
  306.     * is connected to the server or not.
  307.     */
  308.    private function is_connected({
  309.       return (is_resource($this->connectionAND ($this->status === SMTP_STATUS_CONNECTED));
  310.    }
  311.  
  312.    /**
  313.     * Function to send a bit of data
  314.     */
  315.    private function send_data($data{
  316.       if (is_resource($this->connection)) {
  317.          return fwrite($this->connection$data CRLFstrlen($data2);
  318.  
  319.       else {
  320.          return false;
  321.       }
  322.    }
  323.  
  324.    /**
  325.     * Function to get data.
  326.     */
  327.    private function get_data({
  328.       $return '';
  329.       $line '';
  330.       $loops 0;
  331.  
  332.       if (is_resource($this->connection)) {
  333.          while ((strpos($returnCRLF=== FALSE OR substr($line31!== ' 'AND $loops 100{
  334.             $line fgets($this->connection512);
  335.             $return .= $line;
  336.             $loops++;
  337.          }
  338.          return $return;
  339.  
  340.       else
  341.          return false;
  342.    }
  343.  
  344.    /**
  345.     * Sets a variable
  346.     */
  347.    public function set($var$value{
  348.       $this-> $var $value;
  349.       return true;
  350.    }
  351.  
  352.    /**
  353.     * Function to return the errors array
  354.     */
  355.    public function getErrors({
  356.       return $this->errors;
  357.    }
  358.  
  359. // End of class
  360. ?>

Documentation generated on Sat, 24 Mar 2007 10:00:05 +0100 by phpDocumentor 1.3.1