home *** CD-ROM | disk | FTP | other *** search
/ PC Advisor 2006 January / PCA126_DVD.iso / ADVISORS / phpBB-2.0.17 / phpBB2 / includes / smtp.php < prev    next >
Encoding:
PHP Script  |  2005-07-19  |  6.0 KB  |  209 lines

  1. <?php
  2. /***************************************************************************
  3.  *                              smtp.php
  4.  *                       -------------------
  5.  *   begin                : Wed May 09 2001
  6.  *   copyright            : (C) 2001 The phpBB Group
  7.  *   email                : support@phpbb.com
  8.  *
  9.  *   $Id: smtp.php,v 1.16.2.10 2005/05/06 20:50:11 acydburn Exp $
  10.  *
  11.  ***************************************************************************/
  12.  
  13. /***************************************************************************
  14.  *
  15.  *   This program is free software; you can redistribute it and/or modify
  16.  *   it under the terms of the GNU General Public License as published by
  17.  *   the Free Software Foundation; either version 2 of the License, or
  18.  *   (at your option) any later version.
  19.  *
  20.  ***************************************************************************/
  21.  
  22. define('SMTP_INCLUDED', 1);
  23.  
  24. //
  25. // This function has been modified as provided
  26. // by SirSir to allow multiline responses when 
  27. // using SMTP Extensions
  28. //
  29. function server_parse($socket, $response, $line = __LINE__) 
  30.     while (substr($server_response, 3, 1) != ' ') 
  31.     {
  32.         if (!($server_response = fgets($socket, 256))) 
  33.         { 
  34.             message_die(GENERAL_ERROR, "Couldn't get mail server response codes", "", $line, __FILE__); 
  35.         } 
  36.     } 
  37.  
  38.     if (!(substr($server_response, 0, 3) == $response)) 
  39.     { 
  40.         message_die(GENERAL_ERROR, "Ran into problems sending Mail. Response: $server_response", "", $line, __FILE__); 
  41.     } 
  42. }
  43.  
  44. // Replacement or substitute for PHP's mail command
  45. function smtpmail($mail_to, $subject, $message, $headers = '')
  46. {
  47.     global $board_config;
  48.  
  49.     // Fix any bare linefeeds in the message to make it RFC821 Compliant.
  50.     $message = preg_replace("#(?<!\r)\n#si", "\r\n", $message);
  51.  
  52.     if ($headers != '')
  53.     {
  54.         if (is_array($headers))
  55.         {
  56.             if (sizeof($headers) > 1)
  57.             {
  58.                 $headers = join("\n", $headers);
  59.             }
  60.             else
  61.             {
  62.                 $headers = $headers[0];
  63.             }
  64.         }
  65.         $headers = chop($headers);
  66.  
  67.         // Make sure there are no bare linefeeds in the headers
  68.         $headers = preg_replace('#(?<!\r)\n#si', "\r\n", $headers);
  69.  
  70.         // Ok this is rather confusing all things considered,
  71.         // but we have to grab bcc and cc headers and treat them differently
  72.         // Something we really didn't take into consideration originally
  73.         $header_array = explode("\r\n", $headers);
  74.         @reset($header_array);
  75.  
  76.         $headers = '';
  77.         while(list(, $header) = each($header_array))
  78.         {
  79.             if (preg_match('#^cc:#si', $header))
  80.             {
  81.                 $cc = preg_replace('#^cc:(.*)#si', '\1', $header);
  82.             }
  83.             else if (preg_match('#^bcc:#si', $header))
  84.             {
  85.                 $bcc = preg_replace('#^bcc:(.*)#si', '\1', $header);
  86.                 $header = '';
  87.             }
  88.             $headers .= ($header != '') ? $header . "\r\n" : '';
  89.         }
  90.  
  91.         $headers = chop($headers);
  92.         $cc = explode(', ', $cc);
  93.         $bcc = explode(', ', $bcc);
  94.     }
  95.  
  96.     if (trim($subject) == '')
  97.     {
  98.         message_die(GENERAL_ERROR, "No email Subject specified", "", __LINE__, __FILE__);
  99.     }
  100.  
  101.     if (trim($message) == '')
  102.     {
  103.         message_die(GENERAL_ERROR, "Email message was blank", "", __LINE__, __FILE__);
  104.     }
  105.  
  106.     // Ok we have error checked as much as we can to this point let's get on
  107.     // it already.
  108.     if( !$socket = @fsockopen($board_config['smtp_host'], 25, $errno, $errstr, 20) )
  109.     {
  110.         message_die(GENERAL_ERROR, "Could not connect to smtp host : $errno : $errstr", "", __LINE__, __FILE__);
  111.     }
  112.  
  113.     // Wait for reply
  114.     server_parse($socket, "220", __LINE__);
  115.  
  116.     // Do we want to use AUTH?, send RFC2554 EHLO, else send RFC821 HELO
  117.     // This improved as provided by SirSir to accomodate
  118.     if( !empty($board_config['smtp_username']) && !empty($board_config['smtp_password']) )
  119.     { 
  120.         fputs($socket, "EHLO " . $board_config['smtp_host'] . "\r\n");
  121.         server_parse($socket, "250", __LINE__);
  122.  
  123.         fputs($socket, "AUTH LOGIN\r\n");
  124.         server_parse($socket, "334", __LINE__);
  125.  
  126.         fputs($socket, base64_encode($board_config['smtp_username']) . "\r\n");
  127.         server_parse($socket, "334", __LINE__);
  128.  
  129.         fputs($socket, base64_encode($board_config['smtp_password']) . "\r\n");
  130.         server_parse($socket, "235", __LINE__);
  131.     }
  132.     else
  133.     {
  134.         fputs($socket, "HELO " . $board_config['smtp_host'] . "\r\n");
  135.         server_parse($socket, "250", __LINE__);
  136.     }
  137.  
  138.     // From this point onward most server response codes should be 250
  139.     // Specify who the mail is from....
  140.     fputs($socket, "MAIL FROM: <" . $board_config['board_email'] . ">\r\n");
  141.     server_parse($socket, "250", __LINE__);
  142.  
  143.     // Specify each user to send to and build to header.
  144.     $to_header = '';
  145.  
  146.     // Add an additional bit of error checking to the To field.
  147.     $mail_to = (trim($mail_to) == '') ? 'Undisclosed-recipients:;' : trim($mail_to);
  148.     if (preg_match('#[^ ]+\@[^ ]+#', $mail_to))
  149.     {
  150.         fputs($socket, "RCPT TO: <$mail_to>\r\n");
  151.         server_parse($socket, "250", __LINE__);
  152.     }
  153.  
  154.     // Ok now do the CC and BCC fields...
  155.     @reset($bcc);
  156.     while(list(, $bcc_address) = each($bcc))
  157.     {
  158.         // Add an additional bit of error checking to bcc header...
  159.         $bcc_address = trim($bcc_address);
  160.         if (preg_match('#[^ ]+\@[^ ]+#', $bcc_address))
  161.         {
  162.             fputs($socket, "RCPT TO: <$bcc_address>\r\n");
  163.             server_parse($socket, "250", __LINE__);
  164.         }
  165.     }
  166.  
  167.     @reset($cc);
  168.     while(list(, $cc_address) = each($cc))
  169.     {
  170.         // Add an additional bit of error checking to cc header
  171.         $cc_address = trim($cc_address);
  172.         if (preg_match('#[^ ]+\@[^ ]+#', $cc_address))
  173.         {
  174.             fputs($socket, "RCPT TO: <$cc_address>\r\n");
  175.             server_parse($socket, "250", __LINE__);
  176.         }
  177.     }
  178.  
  179.     // Ok now we tell the server we are ready to start sending data
  180.     fputs($socket, "DATA\r\n");
  181.  
  182.     // This is the last response code we look for until the end of the message.
  183.     server_parse($socket, "354", __LINE__);
  184.  
  185.     // Send the Subject Line...
  186.     fputs($socket, "Subject: $subject\r\n");
  187.  
  188.     // Now the To Header.
  189.     fputs($socket, "To: $mail_to\r\n");
  190.  
  191.     // Now any custom headers....
  192.     fputs($socket, "$headers\r\n\r\n");
  193.  
  194.     // Ok now we are ready for the message...
  195.     fputs($socket, "$message\r\n");
  196.  
  197.     // Ok the all the ingredients are mixed in let's cook this puppy...
  198.     fputs($socket, ".\r\n");
  199.     server_parse($socket, "250", __LINE__);
  200.  
  201.     // Now tell the server we are done and close the socket...
  202.     fputs($socket, "QUIT\r\n");
  203.     fclose($socket);
  204.  
  205.     return TRUE;
  206. }
  207.  
  208. ?>