Rev Author Line No. Line
130 kaklik 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.11 2005/09/27 20:24:35 grahamje 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 {
31 $server_response = '';
32 while (substr($server_response, 3, 1) != ' ')
33 {
34 if (!($server_response = fgets($socket, 256)))
35 {
36 message_die(GENERAL_ERROR, "Couldn't get mail server response codes", "", $line, __FILE__);
37 }
38 }
39  
40 if (!(substr($server_response, 0, 3) == $response))
41 {
42 message_die(GENERAL_ERROR, "Ran into problems sending Mail. Response: $server_response", "", $line, __FILE__);
43 }
44 }
45  
46 // Replacement or substitute for PHP's mail command
47 function smtpmail($mail_to, $subject, $message, $headers = '')
48 {
49 global $board_config;
50  
51 // Fix any bare linefeeds in the message to make it RFC821 Compliant.
52 $message = preg_replace("#(?<!\r)\n#si", "\r\n", $message);
53  
54 if ($headers != '')
55 {
56 if (is_array($headers))
57 {
58 if (sizeof($headers) > 1)
59 {
60 $headers = join("\n", $headers);
61 }
62 else
63 {
64 $headers = $headers[0];
65 }
66 }
67 $headers = chop($headers);
68  
69 // Make sure there are no bare linefeeds in the headers
70 $headers = preg_replace('#(?<!\r)\n#si', "\r\n", $headers);
71  
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);
77  
78 $headers = '';
79 while(list(, $header) = each($header_array))
80 {
81 if (preg_match('#^cc:#si', $header))
82 {
83 $cc = preg_replace('#^cc:(.*)#si', '\1', $header);
84 }
85 else if (preg_match('#^bcc:#si', $header))
86 {
87 $bcc = preg_replace('#^bcc:(.*)#si', '\1', $header);
88 $header = '';
89 }
90 $headers .= ($header != '') ? $header . "\r\n" : '';
91 }
92  
93 $headers = chop($headers);
94 $cc = explode(', ', $cc);
95 $bcc = explode(', ', $bcc);
96 }
97  
98 if (trim($subject) == '')
99 {
100 message_die(GENERAL_ERROR, "No email Subject specified", "", __LINE__, __FILE__);
101 }
102  
103 if (trim($message) == '')
104 {
105 message_die(GENERAL_ERROR, "Email message was blank", "", __LINE__, __FILE__);
106 }
107  
108 // Ok we have error checked as much as we can to this point let's get on
109 // it already.
110 if( !$socket = @fsockopen($board_config['smtp_host'], 25, $errno, $errstr, 20) )
111 {
112 message_die(GENERAL_ERROR, "Could not connect to smtp host : $errno : $errstr", "", __LINE__, __FILE__);
113 }
114  
115 // Wait for reply
116 server_parse($socket, "220", __LINE__);
117  
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']) )
121 {
122 fputs($socket, "EHLO " . $board_config['smtp_host'] . "\r\n");
123 server_parse($socket, "250", __LINE__);
124  
125 fputs($socket, "AUTH LOGIN\r\n");
126 server_parse($socket, "334", __LINE__);
127  
128 fputs($socket, base64_encode($board_config['smtp_username']) . "\r\n");
129 server_parse($socket, "334", __LINE__);
130  
131 fputs($socket, base64_encode($board_config['smtp_password']) . "\r\n");
132 server_parse($socket, "235", __LINE__);
133 }
134 else
135 {
136 fputs($socket, "HELO " . $board_config['smtp_host'] . "\r\n");
137 server_parse($socket, "250", __LINE__);
138 }
139  
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__);
144  
145 // Specify each user to send to and build to header.
146 $to_header = '';
147  
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))
151 {
152 fputs($socket, "RCPT TO: <$mail_to>\r\n");
153 server_parse($socket, "250", __LINE__);
154 }
155  
156 // Ok now do the CC and BCC fields...
157 @reset($bcc);
158 while(list(, $bcc_address) = each($bcc))
159 {
160 // Add an additional bit of error checking to bcc header...
161 $bcc_address = trim($bcc_address);
162 if (preg_match('#[^ ]+\@[^ ]+#', $bcc_address))
163 {
164 fputs($socket, "RCPT TO: <$bcc_address>\r\n");
165 server_parse($socket, "250", __LINE__);
166 }
167 }
168  
169 @reset($cc);
170 while(list(, $cc_address) = each($cc))
171 {
172 // Add an additional bit of error checking to cc header
173 $cc_address = trim($cc_address);
174 if (preg_match('#[^ ]+\@[^ ]+#', $cc_address))
175 {
176 fputs($socket, "RCPT TO: <$cc_address>\r\n");
177 server_parse($socket, "250", __LINE__);
178 }
179 }
180  
181 // Ok now we tell the server we are ready to start sending data
182 fputs($socket, "DATA\r\n");
183  
184 // This is the last response code we look for until the end of the message.
185 server_parse($socket, "354", __LINE__);
186  
187 // Send the Subject Line...
188 fputs($socket, "Subject: $subject\r\n");
189  
190 // Now the To Header.
191 fputs($socket, "To: $mail_to\r\n");
192  
193 // Now any custom headers....
194 fputs($socket, "$headers\r\n\r\n");
195  
196 // Ok now we are ready for the message...
197 fputs($socket, "$message\r\n");
198  
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__);
202  
203 // Now tell the server we are done and close the socket...
204 fputs($socket, "QUIT\r\n");
205 fclose($socket);
206  
207 return TRUE;
208 }
209  
210 ?>