Advertisement
Guest User

smail

a guest
Oct 26th, 2017
862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.54 KB | None | 0 0
  1. <?php
  2.   ############################################################################
  3.  # smtp mailer                                                              #
  4.  # Written by Itzchak Rehberg <izzysoft@qumran.org>                         #
  5.  # ------------------------------------------------                         #
  6.  #  This module should replace php's mail() function. It is fully syntax    #
  7.  #  compatible. In addition, when an error occures, a detailed error info   #
  8.  #  is stored in the array $smtp->err                                       #
  9.  # ------------------------------------------------------------------------ #
  10.  # It is recommended to define YOUR local settings.   Please, do so only in #
  11.  # the function send() - unless you are really sure what you are doing!     #
  12.  ############################################################################
  13.  
  14.   /* $Id: smtp.inc,v 1.4 2003/10/29 18:22:41 izzy Exp $ */
  15.  
  16.   /** Extended SMTP module
  17.    * @package Api
  18.    * @class smtp
  19.    * @author Itzchak Rehberg (info@izzysoft.de)
  20.    * @copyright (c) 2001-2003 by Itzchak Rehberg
  21.    */
  22.   class smtp {
  23.  
  24.     var $err       = array("code","msg","desc");
  25.     var $to_res    = array();
  26.  
  27.     /** Class initialization
  28.      * @constructor smtp
  29.      * @param optional string hostname hostname to identify with at the SMTP server
  30.      * @param optional string fromuser default sender (and "Return-Path")
  31.      * @param optional string server smtp server to use
  32.      * @param optional integer port port of this smtp server (default: 25)
  33.      * @param optional string charset charset for the message (default: iso-8859-15)
  34.      */
  35.     function smtp($hostname="",$fromuser="",$server="",$port=25,$charset="utf-8") {
  36.       global $HOSTNAME;
  37.       if ($hostname) { $this->hostname = $hostname; }
  38.         else {  $this->hostname = $HOSTNAME; }
  39.       if ( !strpos($this->hostname,".") ) $this->hostname .= ".qumran.org";
  40.       if ($fromuser) { $this->fromuser    = "izzy@qumran.org"; }
  41.         else { $this->fromuser = $fromuser; }
  42.       if ($server) { $this->smtpserver = $server; }
  43.         else { $this->smtpserver  = "jona.qumran.org"; }
  44.       $this->port        = $port;
  45.       $this->mimeversion = "1.0";
  46.       $this->charset     = $charset;
  47.       // don't change anything below here - unless you know what you're doing!
  48.       $this->err["code"] = "000";
  49.       $this->err["msg"]  = "init";
  50.       $this->err["desc"] = "Session is just initializing.";
  51.     }
  52.  
  53.  // ==================================================[ some sub-functions ]===
  54.  
  55.  function socket2msg($socket) {
  56.    $followme = "-"; $this->err["msg"] = "";
  57.    do {
  58.      $rmsg = fgets($socket,255);
  59. //     echo "< $rmsg<br>\n";
  60.      $this->err["code"] = substr($rmsg,0,3);
  61.      $followme = substr($rmsg,3,1);
  62.      $this->err["msg"] = substr($rmsg,4);
  63.      if (substr($this->err["code"],0,1) != 2 && substr($this->err["code"],0,1) != 3) {
  64.        $rc  = fclose($socket);
  65.        return false;
  66.      }
  67.      if ($followme = " ") { break; }
  68.    } while ($followme = "-");
  69.    return true;
  70.  }
  71.  
  72.  function msg2socket($socket,$message) { // send single line\n
  73. //  echo "> $message<BR>\n";
  74.   $rc = fputs($socket,"$message");
  75.   if (!$rc) {
  76.     $this->err["code"] = "420";
  77.     $this->err["msg"]  = "lost connection";
  78.     $this->err["desc"] = "Lost connection to smtp server.";
  79.     $rc  = fclose($socket);
  80.     return false;
  81.   }
  82.   return true;
  83.  }
  84.  
  85.  function put2socket($socket,$message) { // check for multiple lines 1st
  86.   $this->err["code"] = "000";
  87.   $this->err["msg"]  = "init";
  88.   $this->err["desc"] = "The session is still to be initiated.";
  89.   $pos = strpos($message,"\n");
  90.   if (!is_int($pos)) { // no new line found
  91.     $message .= "\n";
  92.     $this->msg2socket($socket,$message);
  93.   } else {                         // multiple lines, we have to split it
  94.     do {
  95.       $msglen = $pos + 1;
  96.       $msg = substr($message,0,$msglen);
  97.       $message = substr($message,$msglen);
  98.       $pos = strpos($msg,"\n");
  99.       if (!is_int($pos)) { // line not terminated
  100.         $msg .= "\n";
  101.       }
  102.       $pos = strpos($msg,".");  // escape leading periods
  103.       if (is_int($pos) && !$pos):
  104.         $msg = "." . $msg;
  105.       endif;
  106.       if (!$this->msg2socket($socket,$msg)): return false; endif;
  107.       $pos = strpos($message,"\n");
  108.     } while (strlen($message)>0);
  109.   }
  110.   return true;
  111.  }
  112.  
  113.  function check_header($to,$subject,$header) { // check if header contains subject and
  114.                                                // recipient and is correctly terminated
  115.   // first we check for the subject
  116.   if ( !(is_string($subject) && !$subject) ) { // subject specified?
  117.    $theader = strtolower($header);
  118.    $nl  = strpos($theader,"\nsubject:"); // found after a new line
  119.    $beg = strpos($theader,"subject:");   // found at start
  120.    if ( !(is_int($nl)) || (is_int($beg) && !$beg) ) {
  121.     $subject = "Subject: " . stripslashes($subject);
  122.     $pos = substr($subject,"\n");
  123.     if (!is_int($pos)) $subject .= "\n";
  124.     $header .= $subject;
  125.    }
  126.   }
  127.   // now we check for the recipient
  128.   if ( !(is_string($to) && !$to) ) { // recipient specified?
  129.    $nl  = strpos($theader,"\nto:"); // found after a new line
  130.    $beg = strpos($theader,"to:");   // found at start
  131.    if ( !(is_int($nl)) || (is_int($beg) && !$beg) ) {
  132.     $pos = substr($to,"\n");
  133.     if (!is_int($pos)) $subject .= "\n";
  134.     $to = "To: " .$to ."\n";
  135.     $header .= $to;
  136.    }
  137.   }
  138.   // so what about the mime version...
  139.   $nl  = strpos($theader,"\nmime-version:"); // found after a new line
  140.   $beg = strpos($theader,"mime-version:");   // found at start
  141.   if ( !(is_int($nl)) || (is_int($beg) && !$beg) ) {
  142.    $header .= "MIME-Version: " . $this->mimeversion . "\n";
  143.   }
  144.   // and the content type?
  145.   $nl  = strpos($theader,"\ncontent-type:"); // found after a new line
  146.   $beg = strpos($theader,"content-type:");   // found at start
  147.   if ( !(is_int($nl)) || (is_int($beg) && !$beg) ) {
  148.    $header .= "Content-type: text/plain; charset=" . $this->charset . "\n";
  149.   }
  150.   // now we complete the header (make sure it's correct terminated)
  151.   $header = chop($header);
  152.   $header .= "\n";
  153.   return $header;
  154.  }
  155.  
  156.  function make_header($to,$subject) { // generate the mails header
  157.   $now = getdate();
  158.   $header  = "Date: " . gmdate("D, d M Y H:i:s") . " +0000\n";
  159.   $header .= "To: $to\n";
  160.   $header .= "Subject: " . stripslashes($subject) . "\n";
  161.   $header .= "MIME-Version: " . $this->mimeversion . "\n";
  162.   $header .= "Content-type: text/plain; charset=" . $this->charset . "\n";
  163.   return $header;
  164.  }
  165.  
  166.  // ==============================================[ main function: smail() ]===
  167.  
  168.  /** Send a mail via SMTP
  169.   * @class smtp
  170.   * @method smail
  171.   * @param string to target address
  172.   * @param string subject mail subject
  173.   * @param string message the message body
  174.   * @param optional string header mail header (will be generated automatically)
  175.   * @return boolean success
  176.   */
  177.  function smail($to,$subject,$message,$header="") {
  178.   $errcode = ""; $errmsg = ""; // error code and message of failed connection
  179.   $timeout = 5;                // timeout in secs
  180.  
  181.   // now we try to open the socket and check, if any smtp server responds
  182.   $socket = fsockopen($this->smtpserver,$this->port,$errcode,$errmsg,$timeout);
  183.   if (!$socket) {
  184.     $this->err["code"] = "420";
  185.     $this->err["msg"]  = "$errcode:$errmsg";
  186.     $this->err["desc"] = "Connection to ".$this->smtpserver.":".$this->port." failed - could not open socket.";
  187.     return false;
  188.   } else {
  189.     $rrc = $this->socket2msg($socket);
  190.   }
  191.  
  192.   // now we can send our message. 1st we identify ourselves and the sender
  193.   $cmds = array (
  194.      "\$src = \$this->msg2socket(\$socket,\"HELO \$this->hostname\n\");",
  195.      "\$rrc = \$this->socket2msg(\$socket);",
  196.      "\$src = \$this->msg2socket(\$socket,\"MAIL FROM:<\$this->fromuser>\n\");",
  197.      "\$rrc = \$this->socket2msg(\$socket);"
  198.   );
  199.   for ($src=true,$rrc=true,$i=0; $i<count($cmds);$i++) {
  200.    eval ($cmds[$i]);
  201.    if (!$src || !$rrc) return false;
  202.   }
  203.  
  204.   // now we've got to evaluate the $to's
  205.   $toaddr = explode(",",$to);
  206.   $numaddr = count($toaddr);
  207.   for ($i=0; $i<$numaddr; $i++) {
  208.     $src = $this->msg2socket($socket,"RCPT TO:<$toaddr[$i]>\n");
  209.     $rrc = $this->socket2msg($socket);
  210.     $this->to_res[$i]['addr'] = $toaddr[$i];     // for lateron validation
  211.     $this->to_res[$i]['code'] = $this->err["code"];
  212.     $this->to_res[$i]['msg']  = $this->err["msg"];
  213.     $this->to_res[$i]['desc'] = $this->err["desc"];
  214.   }
  215.  
  216.   //now we have to make sure that at least one $to-address was accepted
  217.   $stop = 1;
  218.   for ($i=0;$i<count($this->to_res);$i++) {
  219.     $rc = substr($this->to_res[$i]['code'],0,1);
  220.     if ($rc == 2) { // at least to this address we can deliver
  221.       $stop = 0;
  222.     }
  223.   }
  224.   if ($stop) return false;  // no address found we can deliver to
  225.  
  226.   // now we can go to deliver the message!
  227.   if (!$this->msg2socket($socket,"DATA\n")) return false;
  228.   if (!$this->socket2msg($socket)) return false;
  229.   if ($header != "") {
  230.     $header = $this->check_header($to,$subject,$header);
  231.   } else {
  232.     $header = $this->make_header($to,$subject);
  233.   }
  234.   if (!$this->put2socket($socket,$header)) return false;
  235.   if (!$this->put2socket($socket,"\n")) return false;
  236.   $message  = chop($message);
  237.   $message .= "\n";
  238.   if (!$this->put2socket($socket,$message)) return false;
  239.   if (!$this->msg2socket($socket,".\n")) return false;
  240.   if (!$this->socket2msg($socket)) return false;
  241.   if (!$this->msg2socket($socket,"QUIT\n")) return false;
  242.   Do {
  243.    $closing = $this->socket2msg($socket);
  244.   } while ($closing);
  245.   return true;
  246.  }
  247.  
  248. } // end of class
  249. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement