1 rizwank 1.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.2 2002/12/22 15:09:17 psotfx 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 rizwank 1.1 define('SMTP_INCLUDED', 1);
23 //
24 // This function has been modified as provided
25 // by SirSir to allow multiline responses when
26 // using SMTP Extensions
27 //
28 function server_parse($socket, $response)
29 {
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 rizwank 1.1
44 /****************************************************************************
45 * Function: smtpmail
46 * Description: This is a functional replacement for php's builtin mail
47 * function, that uses smtp.
48 * Usage: The usage for this function is identical to that of php's
49 * built in mail function.
50 ****************************************************************************/
51 function smtpmail($mail_to, $subject, $message, $headers = "")
52 {
53 // For now I'm using an array based $smtp_vars to hold the smtp server
54 // info, but it should probably change to $board_config...
55 // then the relevant info would be $board_config['smtp_host'] and
56 // $board_config['smtp_port'].
57 global $board_config;
58
59 //
60 // Fix any bare linefeeds in the message to make it RFC821 Compliant.
61 //
62 $message = preg_replace("/(?<!\r)\n/si", "\r\n", $message);
63
64 rizwank 1.1 if ($headers != "")
65 {
66 if(is_array($headers))
67 {
68 if(sizeof($headers) > 1)
69 {
70 $headers = join("\r\n", $headers);
71 }
72 else
73 {
74 $headers = $headers[0];
75 }
76 }
77 $headers = chop($headers);
78
79 //
80 // Make sure there are no bare linefeeds in the headers
81 //
82 $headers = preg_replace("/(?<!\r)\n/si", "\r\n", $headers);
83 //
84 // Ok this is rather confusing all things considered,
85 rizwank 1.1 // but we have to grab bcc and cc headers and treat them differently
86 // Something we really didn't take into consideration originally
87 //
88 $header_array = explode("\r\n", $headers);
89 @reset($header_array);
90 $headers = "";
91 while( list(, $header) = each($header_array) )
92 {
93 if( preg_match("/^cc:/si", $header) )
94 {
95 $cc = preg_replace("/^cc:(.*)/si", "\\1", $header);
96 }
97 else if( preg_match("/^bcc:/si", $header ))
98 {
99 $bcc = preg_replace("/^bcc:(.*)/si", "\\1", $header);
100 $header = "";
101 }
102 $headers .= $header . "\r\n";
103 }
104 $headers = chop($headers);
105 $cc = explode(",", $cc);
106 rizwank 1.1 $bcc = explode(",", $bcc);
107 }
108 if($mail_to == "")
109 {
110 message_die(GENERAL_ERROR, "No email address specified", "", __LINE__, __FILE__);
111 }
112 if(trim($subject) == "")
113 {
114 message_die(GENERAL_ERROR, "No email Subject specified", "", __LINE__, __FILE__);
115 }
116 if(trim($message) == "")
117 {
118 message_die(GENERAL_ERROR, "Email message was blank", "", __LINE__, __FILE__);
119 }
120 $mail_to_array = explode(",", $mail_to);
121
122 //
123 // Ok we have error checked as much as we can to this point let's get on
124 // it already.
125 //
126 if( !$socket = fsockopen($board_config['smtp_host'], 25, $errno, $errstr, 20) )
127 rizwank 1.1 {
128 message_die(GENERAL_ERROR, "Could not connect to smtp host : $errno : $errstr", "", __LINE__, __FILE__);
129 }
130 server_parse($socket, "220");
131
132 if( !empty($board_config['smtp_username']) && !empty($board_config['smtp_password']) )
133 {
134 // Send the RFC2554 specified EHLO.
135 // This improved as provided by SirSir to accomodate
136 // both SMTP AND ESMTP capable servers
137 fputs($socket, "EHLO " . $board_config['smtp_host'] . "\r\n");
138 server_parse($socket, "250");
139
140 fputs($socket, "AUTH LOGIN\r\n");
141 server_parse($socket, "334");
142 fputs($socket, base64_encode($board_config['smtp_username']) . "\r\n");
143 server_parse($socket, "334");
144 fputs($socket, base64_encode($board_config['smtp_password']) . "\r\n");
145 server_parse($socket, "235");
146 }
147 else
148 rizwank 1.1 {
149 // Send the RFC821 specified HELO.
150 fputs($socket, "HELO " . $board_config['smtp_host'] . "\r\n");
151 server_parse($socket, "250");
152 }
153
154 // From this point onward most server response codes should be 250
155 // Specify who the mail is from....
156 fputs($socket, "MAIL FROM: <" . $board_config['board_email'] . ">\r\n");
157 server_parse($socket, "250");
158
159 // Specify each user to send to and build to header.
160 $to_header = "To: ";
161 @reset( $mail_to_array );
162 while( list( , $mail_to_address ) = each( $mail_to_array ))
163 {
164 //
165 // Add an additional bit of error checking to the To field.
166 //
167 $mail_to_address = trim($mail_to_address);
168 if ( preg_match('/[^ ]+\@[^ ]+/', $mail_to_address) )
169 rizwank 1.1 {
170 fputs( $socket, "RCPT TO: <$mail_to_address>\r\n" );
171 server_parse( $socket, "250" );
172 }
173 $to_header .= ( ( $mail_to_address != '' ) ? ', ' : '' ) . "<$mail_to_address>";
174 }
175 // Ok now do the CC and BCC fields...
176 @reset( $bcc );
177 while( list( , $bcc_address ) = each( $bcc ))
178 {
179 //
180 // Add an additional bit of error checking to bcc header...
181 //
182 $bcc_address = trim( $bcc_address );
183 if ( preg_match('/[^ ]+\@[^ ]+/', $bcc_address) )
184 {
185 fputs( $socket, "RCPT TO: <$bcc_address>\r\n" );
186 server_parse( $socket, "250" );
187 }
188 }
189 @reset( $cc );
190 rizwank 1.1 while( list( , $cc_address ) = each( $cc ))
191 {
192 //
193 // Add an additional bit of error checking to cc header
194 //
195 $cc_address = trim( $cc_address );
196 if ( preg_match('/[^ ]+\@[^ ]+/', $cc_address) )
197 {
198 fputs($socket, "RCPT TO: <$cc_address>\r\n");
199 server_parse($socket, "250");
200 }
201 }
202 // Ok now we tell the server we are ready to start sending data
203 fputs($socket, "DATA\r\n");
204
205 // This is the last response code we look for until the end of the message.
206 server_parse($socket, "354");
207
208 // Send the Subject Line...
209 fputs($socket, "Subject: $subject\r\n");
210
211 rizwank 1.1 // Now the To Header.
212 fputs($socket, "$to_header\r\n");
213
214 // Now any custom headers....
215 fputs($socket, "$headers\r\n\r\n");
216
217 // Ok now we are ready for the message...
218 fputs($socket, "$message\r\n");
219
220 // Ok the all the ingredients are mixed in let's cook this puppy...
221 fputs($socket, ".\r\n");
222 server_parse($socket, "250");
223
224 // Now tell the server we are done and close the socket...
225 fputs($socket, "QUIT\r\n");
226 fclose($socket);
227
228 return TRUE;
229 }
230
231 ?>
|