e1ee60fe |
1 | <?php |
5b8fd093 |
2 | /** |
eeb17be5 |
3 | * Deliver_SMTP.class.php |
4 | * |
5 | * Copyright (c) 1999-2004 The SquirrelMail Project Team |
6 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
7 | * |
8 | * Delivery backend for the Deliver class. |
9 | * |
883d9cd3 |
10 | * @version $Id$ |
eeb17be5 |
11 | * @package squirrelmail |
12 | */ |
e1ee60fe |
13 | |
2b646597 |
14 | /** This of course depends upon Deliver */ |
0f85ddf9 |
15 | require_once(SM_PATH . 'class/deliver/Deliver.class.php'); |
e1ee60fe |
16 | |
2b646597 |
17 | /** |
eeb17be5 |
18 | * Deliver messages using SMTP |
19 | * @package squirrelmail |
20 | */ |
e1ee60fe |
21 | class Deliver_SMTP extends Deliver { |
22 | |
5fe73b9f |
23 | function preWriteToStream(&$s) { |
eeb17be5 |
24 | if ($s) { |
25 | if ($s{0} == '.') $s = '.' . $s; |
26 | $s = str_replace("\n.","\n..",$s); |
27 | } |
5fe73b9f |
28 | } |
29 | |
30 | function initStream($message, $domain, $length=0, $host='', $port='', $user='', $pass='', $authpop=false) { |
eeb17be5 |
31 | global $use_smtp_tls,$smtp_auth_mech; |
32 | |
33 | if ($authpop) { |
34 | $this->authPop($host, '', $user, $pass); |
22bd1baf |
35 | } |
eeb17be5 |
36 | |
37 | $rfc822_header = $message->rfc822_header; |
38 | |
39 | $from = $rfc822_header->from[0]; |
40 | $to = $rfc822_header->to; |
41 | $cc = $rfc822_header->cc; |
42 | $bcc = $rfc822_header->bcc; |
43 | $content_type = $rfc822_header->content_type; |
33feaaec |
44 | |
eeb17be5 |
45 | // MAIL FROM: <from address> MUST be empty in cae of MDN (RFC2298) |
46 | if ($content_type->type0 == 'multipart' && |
47 | $content_type->type1 == 'report' && |
48 | isset($content_type->properties['report-type']) && |
49 | $content_type->properties['report-type']=='disposition-notification') { |
50 | $from->host = ''; |
51 | $from->mailbox = ''; |
52 | } |
53 | |
54 | if (($use_smtp_tls == true) and (check_php_version(4,3)) and (extension_loaded('openssl'))) { |
55 | $stream = fsockopen('tls://' . $host, $port, $errorNumber, $errorString); |
56 | } else { |
57 | $stream = fsockopen($host, $port, $errorNumber, $errorString); |
58 | } |
59 | |
60 | if (!$stream) { |
61 | $this->dlv_msg = $errorString; |
62 | $this->dlv_ret_nr = $errorNumber; |
63 | return(0); |
64 | } |
65 | $tmp = fgets($stream, 1024); |
66 | if ($this->errorCheck($tmp, $stream)) { |
67 | return(0); |
68 | } |
69 | |
70 | /* |
71 | * If $_SERVER['HTTP_HOST'] is set, use that in our HELO to the SMTP |
72 | * server. This should fix the DNS issues some people have had |
73 | */ |
74 | if (sqgetGlobalVar('HTTP_HOST', $HTTP_HOST, SQ_SERVER)) { // HTTP_HOST is set |
75 | // optionally trim off port number |
76 | if($p = strrpos($HTTP_HOST, ':')) { |
77 | $HTTP_HOST = substr($HTTP_HOST, 0, $p); |
78 | } |
79 | $helohost = $HTTP_HOST; |
80 | } else { // For some reason, HTTP_HOST is not set - revert to old behavior |
81 | $helohost = $domain; |
82 | } |
83 | |
84 | /* Lets introduce ourselves */ |
85 | fputs($stream, "EHLO $helohost\r\n"); |
86 | $tmp = fgets($stream,1024); |
87 | if ($this->errorCheck($tmp,$stream)) { |
df3a8577 |
88 | // fall back to HELO if EHLO is not supported |
06709b31 |
89 | if ($this->dlv_ret_nr == '500') { |
df3a8577 |
90 | fputs($stream, "HELO $helohost\r\n"); |
91 | $tmp = fgets($stream,1024); |
92 | if ($this->errorCheck($tmp,$stream)) { |
93 | return(0); |
94 | } |
95 | } else { |
96 | return(0); |
97 | } |
eeb17be5 |
98 | } |
99 | |
100 | if (( $smtp_auth_mech == 'cram-md5') or ( $smtp_auth_mech == 'digest-md5' )) { |
101 | // Doing some form of non-plain auth |
102 | if ($smtp_auth_mech == 'cram-md5') { |
103 | fputs($stream, "AUTH CRAM-MD5\r\n"); |
104 | } elseif ($smtp_auth_mech == 'digest-md5') { |
105 | fputs($stream, "AUTH DIGEST-MD5\r\n"); |
106 | } |
107 | |
108 | $tmp = fgets($stream,1024); |
109 | |
110 | if ($this->errorCheck($tmp,$stream)) { |
111 | return(0); |
112 | } |
113 | |
114 | // At this point, $tmp should hold "334 <challenge string>" |
115 | $chall = substr($tmp,4); |
116 | // Depending on mechanism, generate response string |
117 | if ($smtp_auth_mech == 'cram-md5') { |
118 | $response = cram_md5_response($user,$pass,$chall); |
119 | } elseif ($smtp_auth_mech == 'digest-md5') { |
120 | $response = digest_md5_response($user,$pass,$chall,'smtp',$host); |
121 | } |
122 | fputs($stream, $response); |
123 | |
124 | // Let's see what the server had to say about that |
125 | $tmp = fgets($stream,1024); |
126 | if ($this->errorCheck($tmp,$stream)) { |
127 | return(0); |
128 | } |
129 | |
130 | // CRAM-MD5 is done at this point. If DIGEST-MD5, there's a bit more to go |
131 | if ($smtp_auth_mech == 'digest-md5') { |
132 | // $tmp contains rspauth, but I don't store that yet. (No need yet) |
133 | fputs($stream,"\r\n"); |
134 | $tmp = fgets($stream,1024); |
33feaaec |
135 | |
eeb17be5 |
136 | if ($this->errorCheck($tmp,$stream)) { |
137 | return(0); |
138 | } |
139 | } |
140 | // CRAM-MD5 and DIGEST-MD5 code ends here |
141 | } elseif ($smtp_auth_mech == 'none') { |
142 | // No auth at all, just send helo and then send the mail |
143 | // We already said hi earlier, nothing more is needed. |
144 | } elseif ($smtp_auth_mech == 'login') { |
145 | // The LOGIN method |
146 | fputs($stream, "AUTH LOGIN\r\n"); |
147 | $tmp = fgets($stream, 1024); |
148 | |
149 | if ($this->errorCheck($tmp, $stream)) { |
150 | return(0); |
151 | } |
152 | fputs($stream, base64_encode ($user) . "\r\n"); |
153 | $tmp = fgets($stream, 1024); |
154 | if ($this->errorCheck($tmp, $stream)) { |
155 | return(0); |
156 | } |
157 | |
158 | fputs($stream, base64_encode($pass) . "\r\n"); |
159 | $tmp = fgets($stream, 1024); |
160 | if ($this->errorCheck($tmp, $stream)) { |
161 | return(0); |
162 | } |
163 | } elseif ($smtp_auth_mech == "plain") { |
164 | /* SASL Plain */ |
165 | $auth = base64_encode("$user\0$user\0$pass"); |
166 | |
167 | $query = "AUTH PLAIN\r\n"; |
168 | fputs($stream, $query); |
169 | $read=fgets($stream, 1024); |
170 | |
171 | if (substr($read,0,3) == '334') { // OK so far.. |
172 | fputs($stream, "$auth\r\n"); |
173 | $read = fgets($stream, 1024); |
174 | } |
175 | |
176 | $results=explode(" ",$read,3); |
177 | $response=$results[1]; |
178 | $message=$results[2]; |
179 | } else { |
180 | /* Right here, they've reached an unsupported auth mechanism. |
181 | This is the ugliest hack I've ever done, but it'll do till I can fix |
182 | things up better tomorrow. So tired... */ |
183 | if ($this->errorCheck("535 Unable to use this auth type",$stream)) { |
184 | return(0); |
185 | } |
186 | } |
187 | |
188 | /* Ok, who is sending the message? */ |
189 | $fromaddress = ($from->mailbox && $from->host) ? |
190 | $from->mailbox.'@'.$from->host : ''; |
191 | fputs($stream, 'MAIL FROM:<'.$fromaddress.">\r\n"); |
192 | $tmp = fgets($stream, 1024); |
193 | if ($this->errorCheck($tmp, $stream)) { |
194 | return(0); |
195 | } |
196 | |
197 | /* send who the recipients are */ |
198 | for ($i = 0, $cnt = count($to); $i < $cnt; $i++) { |
199 | if (!$to[$i]->host) $to[$i]->host = $domain; |
200 | if ($to[$i]->mailbox) { |
201 | fputs($stream, 'RCPT TO:<'.$to[$i]->mailbox.'@'.$to[$i]->host.">\r\n"); |
202 | $tmp = fgets($stream, 1024); |
203 | if ($this->errorCheck($tmp, $stream)) { |
204 | return(0); |
205 | } |
206 | } |
207 | } |
208 | |
209 | for ($i = 0, $cnt = count($cc); $i < $cnt; $i++) { |
210 | if (!$cc[$i]->host) $cc[$i]->host = $domain; |
211 | if ($cc[$i]->mailbox) { |
212 | fputs($stream, 'RCPT TO:<'.$cc[$i]->mailbox.'@'.$cc[$i]->host.">\r\n"); |
213 | $tmp = fgets($stream, 1024); |
214 | if ($this->errorCheck($tmp, $stream)) { |
215 | return(0); |
216 | } |
217 | } |
218 | } |
5fe73b9f |
219 | |
eeb17be5 |
220 | for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++) { |
221 | if (!$bcc[$i]->host) $bcc[$i]->host = $domain; |
222 | if ($bcc[$i]->mailbox) { |
223 | fputs($stream, 'RCPT TO:<'.$bcc[$i]->mailbox.'@'.$bcc[$i]->host.">\r\n"); |
224 | $tmp = fgets($stream, 1024); |
225 | if ($this->errorCheck($tmp, $stream)) { |
226 | return(0); |
227 | } |
228 | } |
229 | } |
230 | /* Lets start sending the actual message */ |
231 | fputs($stream, "DATA\r\n"); |
5fe73b9f |
232 | $tmp = fgets($stream, 1024); |
eeb17be5 |
233 | if ($this->errorCheck($tmp, $stream)) { |
234 | return(0); |
5fe73b9f |
235 | } |
eeb17be5 |
236 | return $stream; |
5fe73b9f |
237 | } |
238 | |
239 | function finalizeStream($stream) { |
eeb17be5 |
240 | fputs($stream, "\r\n.\r\n"); /* end the DATA part */ |
241 | $tmp = fgets($stream, 1024); |
242 | $this->errorCheck($tmp, $stream); |
243 | if ($this->dlv_ret_nr != 250) { |
244 | return(0); |
245 | } |
246 | fputs($stream, "QUIT\r\n"); /* log off */ |
247 | fclose($stream); |
248 | return true; |
5fe73b9f |
249 | } |
250 | |
ca71b2db |
251 | /* check if an SMTP reply is an error and set an error message) */ |
5fe73b9f |
252 | function errorCheck($line, $smtpConnection) { |
ca71b2db |
253 | |
254 | $err_num = substr($line, 0, 3); |
255 | $this->dlv_ret_nr = $err_num; |
256 | $server_msg = substr($line, 4); |
257 | |
258 | while(substr($line, 0, 4) == ($err_num.'-')) { |
259 | $line = fgets($smtpConnection, 1024); |
260 | $server_msg .= substr($line, 4); |
261 | } |
262 | |
eeb17be5 |
263 | if ( ((int) $err_num{0}) < 4) { |
264 | return false; |
ca71b2db |
265 | } |
266 | |
267 | switch ($err_num) { |
268 | case '421': $message = _("Service not available, closing channel"); |
269 | break; |
270 | case '432': $message = _("A password transition is needed"); |
271 | break; |
272 | case '450': $message = _("Requested mail action not taken: mailbox unavailable"); |
273 | break; |
274 | case '451': $message = _("Requested action aborted: error in processing"); |
275 | break; |
276 | case '452': $message = _("Requested action not taken: insufficient system storage"); |
277 | break; |
278 | case '454': $message = _("Temporary authentication failure"); |
279 | break; |
280 | case '500': $message = _("Syntax error; command not recognized"); |
281 | break; |
282 | case '501': $message = _("Syntax error in parameters or arguments"); |
283 | break; |
284 | case '502': $message = _("Command not implemented"); |
285 | break; |
286 | case '503': $message = _("Bad sequence of commands"); |
287 | break; |
288 | case '504': $message = _("Command parameter not implemented"); |
289 | break; |
290 | case '530': $message = _("Authentication required"); |
291 | break; |
292 | case '534': $message = _("Authentication mechanism is too weak"); |
293 | break; |
294 | case '535': $message = _("Authentication failed"); |
295 | break; |
296 | case '538': $message = _("Encryption required for requested authentication mechanism"); |
297 | break; |
298 | case '550': $message = _("Requested action not taken: mailbox unavailable"); |
299 | break; |
300 | case '551': $message = _("User not local; please try forwarding"); |
301 | break; |
302 | case '552': $message = _("Requested mail action aborted: exceeding storage allocation"); |
303 | break; |
304 | case '553': $message = _("Requested action not taken: mailbox name not allowed"); |
305 | break; |
306 | case '554': $message = _("Transaction failed"); |
307 | break; |
308 | default: $message = _("Unknown response"); |
309 | break; |
310 | } |
311 | |
312 | $this->dlv_msg = $message; |
313 | $this->dlv_server_msg = nl2br(htmlspecialchars($server_msg)); |
314 | |
5fe73b9f |
315 | return true; |
316 | } |
317 | |
318 | function authPop($pop_server='', $pop_port='', $user, $pass) { |
319 | if (!$pop_port) { |
320 | $pop_port = 110; |
321 | } |
322 | if (!$pop_server) { |
323 | $pop_server = 'localhost'; |
324 | } |
325 | $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str); |
326 | if (!$popConnection) { |
327 | error_log("Error connecting to POP Server ($pop_server:$pop_port)" |
eeb17be5 |
328 | . " $err_no : $err_str"); |
5fe73b9f |
329 | } else { |
330 | $tmp = fgets($popConnection, 1024); /* banner */ |
22bd1baf |
331 | if (substr($tmp, 0, 3) != '+OK') { |
5fe73b9f |
332 | return(0); |
333 | } |
334 | fputs($popConnection, "USER $user\r\n"); |
335 | $tmp = fgets($popConnection, 1024); |
22bd1baf |
336 | if (substr($tmp, 0, 3) != '+OK') { |
5fe73b9f |
337 | return(0); |
338 | } |
339 | fputs($popConnection, 'PASS ' . $pass . "\r\n"); |
340 | $tmp = fgets($popConnection, 1024); |
22bd1baf |
341 | if (substr($tmp, 0, 3) != '+OK') { |
5fe73b9f |
342 | return(0); |
343 | } |
344 | fputs($popConnection, "QUIT\r\n"); /* log off */ |
345 | fclose($popConnection); |
346 | } |
347 | } |
e1ee60fe |
348 | } |
5fe73b9f |
349 | |
e1ee60fe |
350 | ?> |