2 /***************************************************************************
5 * begin : Wed May 09 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : support@phpbb.com
9 * $Id: smtp.php,v 1.16.2.11 2005/09/27 20:24:35 grahamje Exp $
11 ***************************************************************************/
13 /***************************************************************************
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.
20 ***************************************************************************/
22 define('SMTP_INCLUDED', 1);
25 // This function has been modified as provided
26 // by SirSir to allow multiline responses when
27 // using SMTP Extensions
29 function server_parse($socket, $response, $line = __LINE__)
31 $server_response = '';
32 while (substr($server_response, 3, 1) != ' ')
34 if (!($server_response = fgets($socket, 256)))
36 message_die(GENERAL_ERROR, "Couldn't get mail server response codes", "", $line, __FILE__);
40 if (!(substr($server_response, 0, 3) == $response))
42 message_die(GENERAL_ERROR, "Ran into problems sending Mail. Response: $server_response", "", $line, __FILE__);
46 // Replacement or substitute for PHP's mail command
47 function smtpmail($mail_to, $subject, $message, $headers = '')
51 // Fix any bare linefeeds in the message to make it RFC821 Compliant.
52 $message = preg_replace("#(?<!\r)\n#si", "\r\n", $message);
56 if (is_array($headers))
58 if (sizeof($headers) > 1)
60 $headers = join("\n", $headers);
64 $headers = $headers[0];
67 $headers = chop($headers);
69 // Make sure there are no bare linefeeds in the headers
70 $headers = preg_replace('#(?<!\r)\n#si', "\r\n", $headers);
72 // Ok this is rather confusing all things considered,
73 // but we have to grab bcc and cc headers and treat them differently
74 // Something we really didn't take into consideration originally
75 $header_array = explode("\r\n", $headers);
76 @reset($header_array);
79 while(list(, $header) = each($header_array))
81 if (preg_match('#^cc:#si', $header))
83 $cc = preg_replace('#^cc:(.*)#si', '\1', $header);
85 else if (preg_match('#^bcc:#si', $header))
87 $bcc = preg_replace('#^bcc:(.*)#si', '\1', $header);
90 $headers .= ($header != '') ? $header . "\r\n" : '';
93 $headers = chop($headers);
94 $cc = explode(', ', $cc);
95 $bcc = explode(', ', $bcc);
98 if (trim($subject) == '')
100 message_die(GENERAL_ERROR, "No email Subject specified", "", __LINE__, __FILE__);
103 if (trim($message) == '')
105 message_die(GENERAL_ERROR, "Email message was blank", "", __LINE__, __FILE__);
108 // Ok we have error checked as much as we can to this point let's get on
110 if( !$socket = @fsockopen($board_config['smtp_host'], 25, $errno, $errstr, 20) )
112 message_die(GENERAL_ERROR, "Could not connect to smtp host : $errno : $errstr", "", __LINE__, __FILE__);
116 server_parse($socket, "220", __LINE__);
118 // Do we want to use AUTH?, send RFC2554 EHLO, else send RFC821 HELO
119 // This improved as provided by SirSir to accomodate
120 if( !empty($board_config['smtp_username']) && !empty($board_config['smtp_password']) )
122 fputs($socket, "EHLO " . $board_config['smtp_host'] . "\r\n");
123 server_parse($socket, "250", __LINE__);
125 fputs($socket, "AUTH LOGIN\r\n");
126 server_parse($socket, "334", __LINE__);
128 fputs($socket, base64_encode($board_config['smtp_username']) . "\r\n");
129 server_parse($socket, "334", __LINE__);
131 fputs($socket, base64_encode($board_config['smtp_password']) . "\r\n");
132 server_parse($socket, "235", __LINE__);
136 fputs($socket, "HELO " . $board_config['smtp_host'] . "\r\n");
137 server_parse($socket, "250", __LINE__);
140 // From this point onward most server response codes should be 250
141 // Specify who the mail is from....
142 fputs($socket, "MAIL FROM: <" . $board_config['board_email'] . ">\r\n");
143 server_parse($socket, "250", __LINE__);
145 // Specify each user to send to and build to header.
148 // Add an additional bit of error checking to the To field.
149 $mail_to = (trim($mail_to) == '') ? 'Undisclosed-recipients:;' : trim($mail_to);
150 if (preg_match('#[^ ]+\@[^ ]+#', $mail_to))
152 fputs($socket, "RCPT TO: <$mail_to>\r\n");
153 server_parse($socket, "250", __LINE__);
156 // Ok now do the CC and BCC fields...
158 while(list(, $bcc_address) = each($bcc))
160 // Add an additional bit of error checking to bcc header...
161 $bcc_address = trim($bcc_address);
162 if (preg_match('#[^ ]+\@[^ ]+#', $bcc_address))
164 fputs($socket, "RCPT TO: <$bcc_address>\r\n");
165 server_parse($socket, "250", __LINE__);
170 while(list(, $cc_address) = each($cc))
172 // Add an additional bit of error checking to cc header
173 $cc_address = trim($cc_address);
174 if (preg_match('#[^ ]+\@[^ ]+#', $cc_address))
176 fputs($socket, "RCPT TO: <$cc_address>\r\n");
177 server_parse($socket, "250", __LINE__);
181 // Ok now we tell the server we are ready to start sending data
182 fputs($socket, "DATA\r\n");
184 // This is the last response code we look for until the end of the message.
185 server_parse($socket, "354", __LINE__);
187 // Send the Subject Line...
188 fputs($socket, "Subject: $subject\r\n");
190 // Now the To Header.
191 fputs($socket, "To: $mail_to\r\n");
193 // Now any custom headers....
194 fputs($socket, "$headers\r\n\r\n");
196 // Ok now we are ready for the message...
197 fputs($socket, "$message\r\n");
199 // Ok the all the ingredients are mixed in let's cook this puppy...
200 fputs($socket, ".\r\n");
201 server_parse($socket, "250", __LINE__);
203 // Now tell the server we are done and close the socket...
204 fputs($socket, "QUIT\r\n");