home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / classes / smtp.class.inc < prev    next >
Text File  |  2004-03-08  |  6KB  |  225 lines

  1. <?php
  2. /*
  3.  * $Header: /cvsroot/group-office/groupoffice/classes/smtp.class.inc,v 1.1.1.1 2003/07/03 15:03:26 mschering Exp $
  4.  *
  5.  * Copyright 2001 Nicolas Chalanset <nicocha@free.fr>
  6.  * Copyright 2001 Olivier Cahagne <cahagn_o@epita.fr>
  7.  *
  8.  * See the enclosed file COPYING for license information (GPL).  If you
  9.  * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
  10.  *
  11.  * Class based on a work from Unk <rgroesb_garbage@triple-it_garbage.nl>
  12.  */
  13. class smtp
  14. {
  15.     var $smtp_server;
  16.     var $port;
  17.     var $from;
  18.     var $to;
  19.     var $cc;
  20.     var $bcc;
  21.     var $subject;
  22.     var $data;
  23.     var $sessionlog = '';
  24.  
  25.     // This function is the constructor don't forget this one
  26.     function smtp()
  27.     {
  28.         $this->smtp_server = '';
  29.         $this->port = '';
  30.         $this->from = '';
  31.         $this->to = Array();
  32.         $this->cc = Array();
  33.         $this->bcc = Array();
  34.         $this->subject = '';
  35.         $this->data = '';
  36.     }
  37.  
  38.     function smtp_open()
  39.     {
  40.         global $SMTP_GLOBAL_STATUS;
  41.         $smtp = fsockopen($this->smtp_server, $this->port, $errno, $errstr);
  42.         if (!$smtp)
  43.             return false;
  44.  
  45.         $line = fgets($smtp, 1024);
  46.         $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] = substr($line, 0, 1);
  47.         $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULTTXT'] = substr($line, 0, 1024);
  48.  
  49.         if ($SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] !=  '2')
  50.             return (false);
  51.  
  52.         return $smtp;
  53.     }
  54.  
  55.     function smtp_helo($smtp)
  56.     {
  57.         global $SMTP_GLOBAL_STATUS;
  58.  
  59.         /* 'localhost' always works [Unk] */
  60.         fputs($smtp, "helo localhost\r\n");
  61.         $this->sessionlog .= "Sent: helo localhost";
  62.         $line = fgets($smtp, 1024);
  63.         $this->sessionlog .= "Rcvd: $line";
  64.  
  65.         $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] = substr($line, 0, 1);
  66.         $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULTTXT'] = substr($line, 0, 1024);
  67.  
  68.         if ($SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] !=  '2')
  69.             return (false);
  70.  
  71.         return (true);
  72.     }
  73.  
  74.     function smtp_ehlo($smtp)
  75.     {
  76.         global $SMTP_GLOBAL_STATUS;
  77.  
  78.         /* Well, let's use "helo" for now.. Until we need the
  79.           extra func's   [Unk]
  80.         */
  81.         fputs($smtp, "ehlo localhost\r\n");
  82.         $this->sessionlog .= "Sent: ehlo localhost";
  83.         $line = fgets($smtp, 1024);
  84.         $this->sessionlog .= "Rcvd: $line";
  85.         $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] = substr($line, 0, 1);
  86.         $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULTTXT'] = substr($line, 0, 1024);
  87.  
  88.         if ($SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] <>  '2')
  89.             return (false);
  90.  
  91.         return (true);
  92.     }
  93.  
  94.  
  95.     function smtp_mail_from($smtp)
  96.     {
  97.         global $SMTP_GLOBAL_STATUS;
  98.         fputs($smtp, "MAIL FROM:$this->from\r\n");
  99.         $this->sessionlog .= "Sent: MAIL FROM:$this->from";
  100.         $line = fgets($smtp, 1024);
  101.         $this->sessionlog .= "Rcvd: $line";
  102.  
  103.         $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] = substr($line, 0, 1);
  104.         $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULTTXT'] = substr($line, 0, 1024);
  105.  
  106.         if ($SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] <>  '2')
  107.             return (false);
  108.  
  109.         return (true);
  110.     }
  111.  
  112.     function smtp_rcpt_to($smtp)
  113.     {
  114.         global $SMTP_GLOBAL_STATUS;
  115.  
  116.         // Modified by nicocha to use to, cc and bcc field
  117.         while ($tmp = array_shift($this->to))
  118.         {
  119.             if($tmp == '' || $tmp == '<>')
  120.                 continue;
  121.             fputs($smtp, "RCPT TO:$tmp\r\n");
  122.             $this->sessionlog .= "Sent: RCPT TO:$tmp";
  123.             $line = fgets($smtp, 1024);
  124.             $this->sessionlog .= "Rcvd: $line";
  125.  
  126.             $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] = substr($line, 0, 1);
  127.             $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULTTXT'] = substr($line, 0, 1024);
  128.             if ($SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] <>  '2')
  129.                 return (false);
  130.         }
  131.         while ($tmp = array_shift($this->cc))
  132.         {
  133.             if($tmp == '' || $tmp == '<>')
  134.                 continue;
  135.             fputs($smtp, "RCPT TO:$tmp\r\n");
  136.             $this->sessionlog .= "Sent: RCPT TO:$tmp";
  137.             $line = fgets($smtp, 1024);
  138.             $this->sessionlog .= "Rcvd: $line";
  139.             $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] = substr($line, 0, 1);
  140.             $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULTTXT'] = substr($line, 0, 1024);
  141.  
  142.             if ($SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] <>  '2')
  143.                 return (false);
  144.         }
  145.         while ($tmp = array_shift($this->bcc))
  146.         {
  147.             if($tmp == '' || $tmp == '<>')
  148.                 continue;
  149.             fputs($smtp, "RCPT TO:$tmp\r\n");
  150.             $this->sessionlog .= "Sent: RCPT TO:$tmp";
  151.             $line = fgets($smtp, 1024);
  152.             $this->sessionlog .= "Rcvd: $line";
  153.             $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] = substr($line, 0, 1);
  154.             $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULTTXT'] = substr($line, 0, 1024);
  155.  
  156.             if ($SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] <>  '2')
  157.                 return (false);
  158.         }
  159.         return (true);
  160.     }
  161.  
  162.     function smtp_data($smtp)
  163.     {
  164.         global $SMTP_GLOBAL_STATUS;
  165.  
  166.         fputs($smtp, "DATA\r\n");
  167.         $this->sessionlog .= "Sent: DATA";
  168.         $line = fgets($smtp, 1024);
  169.         $this->sessionlog .= "Rcvd: $line";
  170.  
  171.         $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] = substr($line, 0, 1);
  172.         $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULTTXT'] = substr($line, 0, 1024);
  173.  
  174.         if ($SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] <>  '3')
  175.             return (false);
  176.  
  177.         fputs($smtp, "$this->data");
  178.         fputs($smtp, "\r\n.\r\n");
  179.         $line = fgets($smtp, 1024);
  180.         $this->sessionlog .= "Rcvd: $line";
  181.         if (substr($line, 0, 1) !=  '2')
  182.             return (false);
  183.  
  184.         return (true);
  185.     }
  186.  
  187.     function smtp_quit($smtp)
  188.     {
  189.         global $SMTP_GLOBAL_STATUS;
  190.  
  191.         fputs($smtp,  "QUIT\r\n");
  192.         $this->sessionlog .= "Sent: QUIT";
  193.         $line = fgets($smtp, 1024);
  194.         $this->sessionlog .= "Rcvd: $line";
  195.  
  196.         $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] = substr($line, 0, 1);
  197.         $SMTP_GLOBAL_STATUS[$smtp]['LASTRESULTTXT'] = substr($line, 0, 1024);
  198.  
  199.         if ($SMTP_GLOBAL_STATUS[$smtp]['LASTRESULT'] !=  '2')
  200.             return (false);
  201.  
  202.         return (true);
  203.     }
  204.  
  205.     function send()
  206.     {
  207.         $smtp = $this->smtp_open();
  208.         if(!$smtp)
  209.             return (false);
  210.         if(!$this->smtp_helo($smtp))
  211.             return (false);
  212.         if(!$this->smtp_mail_from($smtp))
  213.             return (false);
  214.         if(!$this->smtp_rcpt_to($smtp))
  215.             return (false);
  216.         if(!$this->smtp_data($smtp))
  217.             return (false);
  218.         if(!$this->smtp_quit($smtp))
  219.             return (false);
  220.  
  221.         return (true);
  222.     }
  223. }
  224. ?>
  225.