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.9 2003/07/18 16:34:01 acydburn 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 while (substr($server_response, 3, 1) != ' ')
33 if (!($server_response = fgets($socket, 256)))
35 message_die(GENERAL_ERROR, "Couldn't get mail server response codes", "", $line, __FILE__);
39 if (!(substr($server_response, 0, 3) == $response))
41 message_die(GENERAL_ERROR, "Ran into problems sending Mail. Response: $server_response", "", $line, __FILE__);
45 // Replacement or substitute for PHP's mail command
46 function smtpmail($mail_to, $subject, $message, $headers = '')
50 // Fix any bare linefeeds in the message to make it RFC821 Compliant.
51 $message = preg_replace("#(?<!\r)\n#si", "\r\n", $message);
55 if (is_array($headers))
57 if (sizeof($headers) > 1)
59 $headers = join("\n", $headers);
63 $headers = $headers[0];
66 $headers = chop($headers);
68 // Make sure there are no bare linefeeds in the headers
69 $headers = preg_replace('#(?<!\r)\n#si', "\r\n", $headers);
71 // Ok this is rather confusing all things considered,
72 // but we have to grab bcc and cc headers and treat them differently
73 // Something we really didn't take into consideration originally
74 $header_array = explode("\r\n", $headers);
75 @reset($header_array);
78 while(list(, $header) = each($header_array))
80 if (preg_match('#^cc:#si', $header))
82 $cc = preg_replace('#^cc:(.*)#si', '\1', $header);
84 else if (preg_match('#^bcc:#si', $header))
86 $bcc = preg_replace('#^bcc:(.*)#si', '\1', $header);
89 $headers .= ($header != '') ? $header . "\r\n" : '';
92 $headers = chop($headers);
93 $cc = explode(', ', $cc);
94 $bcc = explode(', ', $bcc);
97 if (trim($subject) == '')
99 message_die(GENERAL_ERROR, "No email Subject specified", "", __LINE__, __FILE__);
102 if (trim($message) == '')
104 message_die(GENERAL_ERROR, "Email message was blank", "", __LINE__, __FILE__);
107 // Ok we have error checked as much as we can to this point let's get on
109 if( !$socket = fsockopen($board_config['smtp_host'], 25, $errno, $errstr, 20) )
111 message_die(GENERAL_ERROR, "Could not connect to smtp host : $errno : $errstr", "", __LINE__, __FILE__);
115 server_parse($socket, "220", __LINE__);
117 // Do we want to use AUTH?, send RFC2554 EHLO, else send RFC821 HELO
118 // This improved as provided by SirSir to accomodate
119 if( !empty($board_config['smtp_username']) && !empty($board_config['smtp_password']) )
121 fputs($socket, "EHLO " . $board_config['smtp_host'] . "\r\n");
122 server_parse($socket, "250", __LINE__);
124 fputs($socket, "AUTH LOGIN\r\n");
125 server_parse($socket, "334", __LINE__);
127 fputs($socket, base64_encode($board_config['smtp_username']) . "\r\n");
128 server_parse($socket, "334", __LINE__);
130 fputs($socket, base64_encode($board_config['smtp_password']) . "\r\n");
131 server_parse($socket, "235", __LINE__);
135 fputs($socket, "HELO " . $board_config['smtp_host'] . "\r\n");
136 server_parse($socket, "250", __LINE__);
139 // From this point onward most server response codes should be 250
140 // Specify who the mail is from....
141 fputs($socket, "MAIL FROM: <" . $board_config['board_email'] . ">\r\n");
142 server_parse($socket, "250", __LINE__);
144 // Specify each user to send to and build to header.
147 // Add an additional bit of error checking to the To field.
148 $mail_to = (trim($mail_to) == '') ? 'Undisclosed-recipients:;' : trim($mail_to);
149 if (preg_match('#[^ ]+\@[^ ]+#', $mail_to))
151 fputs($socket, "RCPT TO: <$mail_to>\r\n");
152 server_parse($socket, "250", __LINE__);
155 // Ok now do the CC and BCC fields...
157 while(list(, $bcc_address) = each($bcc))
159 // Add an additional bit of error checking to bcc header...
160 $bcc_address = trim($bcc_address);
161 if (preg_match('#[^ ]+\@[^ ]+#', $bcc_address))
163 fputs($socket, "RCPT TO: <$bcc_address>\r\n");
164 server_parse($socket, "250", __LINE__);
169 while(list(, $cc_address) = each($cc))
171 // Add an additional bit of error checking to cc header
172 $cc_address = trim($cc_address);
173 if (preg_match('#[^ ]+\@[^ ]+#', $cc_address))
175 fputs($socket, "RCPT TO: <$cc_address>\r\n");
176 server_parse($socket, "250", __LINE__);
180 // Ok now we tell the server we are ready to start sending data
181 fputs($socket, "DATA\r\n");
183 // This is the last response code we look for until the end of the message.
184 server_parse($socket, "354", __LINE__);
186 // Send the Subject Line...
187 fputs($socket, "Subject: $subject\r\n");
189 // Now the To Header.
190 fputs($socket, "To: $mail_to\r\n");
192 // Now any custom headers....
193 fputs($socket, "$headers\r\n\r\n");
195 // Ok now we are ready for the message...
196 fputs($socket, "$message\r\n");
198 // Ok the all the ingredients are mixed in let's cook this puppy...
199 fputs($socket, ".\r\n");
200 server_parse($socket, "250", __LINE__);
202 // Now tell the server we are done and close the socket...
203 fputs($socket, "QUIT\r\n");