59177427 |
1 | <?php |
2ba13803 |
2 | |
35586184 |
3 | /** |
4 | * smtp.php |
5 | * |
15e6162e |
6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
35586184 |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * This contains all the functions needed to send messages through |
10 | * an smtp server or sendmail. |
11 | * |
12 | * $Id$ |
13 | */ |
b8ea4ed6 |
14 | |
35586184 |
15 | require_once('../functions/addressbook.php'); |
16 | require_once('../functions/plugin.php'); |
3392dc86 |
17 | require_once('../functions/prefs.php'); |
35586184 |
18 | |
19 | global $username, $popuser, $domain; |
465db5d7 |
20 | |
a1e937bb |
21 | /* This should most probably go to some initialization... */ |
e25c2bd3 |
22 | if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) { |
23 | $popuser = $usernamedata[1]; |
24 | $domain = $usernamedata[2]; |
25 | unset($usernamedata); |
26 | } else { |
27 | $popuser = $username; |
28 | } |
a1e937bb |
29 | /* We need domain for smtp */ |
30 | if (!$domain) { |
e25c2bd3 |
31 | $domain = getenv('HOSTNAME'); |
a1e937bb |
32 | } |
e25c2bd3 |
33 | |
a3fbf5a4 |
34 | /** |
35 | * Return which separator we should be using. |
36 | * \r\n for SMTP delivery, just \n for Sendmail. |
37 | */ |
38 | function sqm_nrn(){ |
39 | global $useSendmail; |
40 | if ($useSendmail){ |
41 | return "\n"; |
42 | } else { |
43 | return "\r\n"; |
44 | } |
45 | } |
46 | |
47 | |
a1e937bb |
48 | /* Returns true only if this message is multipart */ |
dd7766e0 |
49 | function isMultipart ($session) { |
e25c2bd3 |
50 | global $attachments; |
7622a204 |
51 | |
52 | foreach ($attachments as $info) { |
a3fbf5a4 |
53 | if ($info['session'] == $session) { |
54 | return true; |
55 | } |
a1e937bb |
56 | } |
7622a204 |
57 | return false; |
e25c2bd3 |
58 | } |
59 | |
a1e937bb |
60 | /* looks up aliases in the addressbook and expands them to |
61 | * the full address. |
62 | * |
63 | * Adds @$domain if it wasn't in the address book and if it |
64 | * doesn't have an @ symbol in it |
65 | */ |
e25c2bd3 |
66 | function expandAddrs ($array) { |
67 | global $domain; |
68 | |
a1e937bb |
69 | /* don't show errors -- kinda critical that we don't see |
70 | * them here since the redirect won't work if we do show them |
71 | */ |
68444892 |
72 | $abook = addressbook_init(false, true); |
e25c2bd3 |
73 | for ($i=0; $i < count($array); $i++) { |
74 | $result = $abook->lookup($array[$i]); |
75 | $ret = ""; |
76 | if (isset($result['email'])) { |
a4102fd7 |
77 | if (isset($result['name'])) { |
e25c2bd3 |
78 | $ret = '"'.$result['name'].'" '; |
a4102fd7 |
79 | } |
80 | $ret .= '<'.$result['email'].'>'; |
81 | $array[$i] = $ret; |
e25c2bd3 |
82 | } |
83 | else |
a1e937bb |
84 | { |
85 | if (strpos($array[$i], '@') === false) { |
86 | $array[$i] .= '@' . $domain; |
e25c2bd3 |
87 | } |
a1e937bb |
88 | $array[$i] = '<' . $array[$i] . '>'; |
89 | } |
e25c2bd3 |
90 | } |
91 | return $array; |
92 | } |
93555af8 |
93 | |
93555af8 |
94 | |
a1e937bb |
95 | /* looks up aliases in the addressbook and expands them to |
96 | * the RFC 821 valid RCPT address. ie <user@example.com> |
97 | * Adds @$domain if it wasn't in the address book and if it |
98 | * doesn't have an @ symbol in it |
99 | */ |
e25c2bd3 |
100 | function expandRcptAddrs ($array) { |
101 | global $domain; |
102 | |
a1e937bb |
103 | /* don't show errors -- kinda critical that we don't see |
104 | * them here since the redirect won't work if we do show them |
105 | */ |
68444892 |
106 | $abook = addressbook_init(false, true); |
e25c2bd3 |
107 | for ($i=0; $i < count($array); $i++) { |
108 | $result = $abook->lookup($array[$i]); |
109 | $ret = ""; |
110 | if (isset($result['email'])) { |
93555af8 |
111 | $ret = '<'.$result['email'].'>'; |
112 | $array[$i] = $ret; |
e25c2bd3 |
113 | } |
114 | else { |
a1e937bb |
115 | if (strpos($array[$i], '@') === false) { |
e25c2bd3 |
116 | $array[$i] .= '@' . $domain; |
a1e937bb |
117 | } |
93555af8 |
118 | $array[$i] = '<' . $array[$i] . '>'; |
e25c2bd3 |
119 | } |
120 | } |
121 | return $array; |
122 | } |
93555af8 |
123 | |
4ba45d11 |
124 | |
77b88425 |
125 | /* Attach the files that are due to be attached |
a1e937bb |
126 | */ |
dd7766e0 |
127 | function attachFiles ($fp, $session) { |
e25c2bd3 |
128 | global $attachments, $attachment_dir, $username; |
77b88425 |
129 | |
e25c2bd3 |
130 | $length = 0; |
a3fbf5a4 |
131 | $rn = sqm_nrn(); |
77b88425 |
132 | |
e25c2bd3 |
133 | $hashed_attachment_dir = getHashedDir($username, $attachment_dir); |
dd7766e0 |
134 | if (isMultipart($session)) { |
e25c2bd3 |
135 | foreach ($attachments as $info) { |
a3fbf5a4 |
136 | if ($info['session'] == $session) { |
137 | if (isset($info['type'])) { |
138 | $filetype = $info['type']; |
139 | } |
140 | else { |
141 | $filetype = 'application/octet-stream'; |
142 | } |
143 | |
144 | $header = '--' . mimeBoundary() . "$rn"; |
145 | if ( isset($info['remotefilename']) |
146 | && $info['remotefilename'] != '') { |
147 | $header .= "Content-Type: $filetype; name=\"" . |
148 | $info['remotefilename'] . "\"$rn"; |
149 | $header .= "Content-Disposition: attachment; filename=\"" |
150 | . $info['remotefilename'] . "\"$rn"; |
151 | } else { |
152 | $header .= "Content-Type: $filetype;$rn"; |
153 | } |
77b88425 |
154 | |
a3fbf5a4 |
155 | |
156 | /* Use 'rb' for NT systems -- read binary |
157 | * Unix doesn't care -- everything's binary! :-) |
158 | */ |
159 | |
160 | $filename = $hashed_attachment_dir . '/' |
161 | . $info['localfilename']; |
162 | $file = fopen ($filename, 'rb'); |
163 | if (substr($filetype, 0, 5) == 'text/' || |
164 | substr($filetype, 0, 8) == 'message/' ) { |
165 | $header .= "$rn"; |
166 | fputs ($fp, $header); |
167 | $length += strlen($header); |
168 | while ($tmp = fgets($file, 4096)) { |
169 | $tmp = str_replace("\r\n", "\n", $tmp); |
170 | $tmp = str_replace("\r", "\n", $tmp); |
171 | if ($rn == "\r\n"){ |
172 | $tmp = str_replace("\n", "\r\n", $tmp); |
173 | } |
174 | /** |
175 | * Check if the last line has newline ($rn) in it |
176 | * and append if it doesn't. |
177 | */ |
178 | if (feof($fp) && !strstr($tmp, "$rn")){ |
179 | $tmp .= "$rn"; |
180 | } |
181 | fputs($fp, $tmp); |
182 | $length += strlen($tmp); |
183 | } |
184 | } else { |
185 | $header .= "Content-Transfer-Encoding: base64" |
186 | . "$rn" . "$rn"; |
187 | fputs ($fp, $header); |
188 | $length += strlen($header); |
189 | while ($tmp = fread($file, 570)) { |
190 | $encoded = chunk_split(base64_encode($tmp)); |
191 | $length += strlen($encoded); |
192 | fputs ($fp, $encoded); |
a1e937bb |
193 | } |
e25c2bd3 |
194 | } |
a3fbf5a4 |
195 | fclose ($file); |
e25c2bd3 |
196 | } |
e25c2bd3 |
197 | } |
198 | } |
199 | return $length; |
200 | } |
201 | |
a1e937bb |
202 | /* Delete files that are uploaded for attaching |
203 | */ |
dd7766e0 |
204 | function deleteAttachments($session) { |
205 | global $username, $attachments, $attachment_dir; |
e25c2bd3 |
206 | $hashed_attachment_dir = getHashedDir($username, $attachment_dir); |
dd7766e0 |
207 | |
208 | $rem_attachments = array(); |
209 | foreach ($attachments as $info) { |
a3fbf5a4 |
210 | if ($info['session'] == $session) { |
dd7766e0 |
211 | $attached_file = "$hashed_attachment_dir/$info[localfilename]"; |
212 | if (file_exists($attached_file)) { |
a3fbf5a4 |
213 | unlink($attached_file); |
dd7766e0 |
214 | } |
a3fbf5a4 |
215 | } else { |
216 | $rem_attachments[] = $info; |
217 | } |
dd7766e0 |
218 | } |
219 | $attachments = $rem_attachments; |
e25c2bd3 |
220 | } |
a7d75834 |
221 | |
a1e937bb |
222 | /* Return a nice MIME-boundary |
223 | */ |
e25c2bd3 |
224 | function mimeBoundary () { |
225 | static $mimeBoundaryString; |
77b88425 |
226 | |
227 | if ( !isset( $mimeBoundaryString ) || |
228 | $mimeBoundaryString == '') { |
229 | $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' . |
a3fbf5a4 |
230 | mt_rand( 10000, 99999 ); |
e25c2bd3 |
231 | } |
77b88425 |
232 | |
e25c2bd3 |
233 | return $mimeBoundaryString; |
234 | } |
7b67334e |
235 | |
e25c2bd3 |
236 | /* Time offset for correct timezone */ |
237 | function timezone () { |
238 | global $invert_time; |
239 | |
240 | $diff_second = date('Z'); |
a1e937bb |
241 | if ($invert_time) { |
e25c2bd3 |
242 | $diff_second = - $diff_second; |
a1e937bb |
243 | } |
244 | if ($diff_second > 0) { |
e25c2bd3 |
245 | $sign = '+'; |
a1e937bb |
246 | } |
247 | else { |
e25c2bd3 |
248 | $sign = '-'; |
a1e937bb |
249 | } |
250 | |
e25c2bd3 |
251 | $diff_second = abs($diff_second); |
252 | |
253 | $diff_hour = floor ($diff_second / 3600); |
254 | $diff_minute = floor (($diff_second-3600*$diff_hour) / 60); |
255 | |
256 | $zonename = '('.strftime('%Z').')'; |
a3fbf5a4 |
257 | $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, |
258 | $zonename); |
e25c2bd3 |
259 | return ($result); |
260 | } |
261 | |
262 | /* Print all the needed RFC822 headers */ |
dd7766e0 |
263 | function write822Header ($fp, $t, $c, $b, $subject, $more_headers, $session) { |
e25c2bd3 |
264 | global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT; |
265 | global $data_dir, $username, $popuser, $domain, $version, $useSendmail; |
266 | global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR; |
267 | global $REMOTE_HOST, $identity; |
a3fbf5a4 |
268 | /** |
269 | * Get which delimiter we are going to use. |
270 | */ |
271 | $rn = sqm_nrn(); |
a1e937bb |
272 | /* Storing the header to make sure the header is the same |
273 | * everytime the header is printed. |
274 | */ |
e25c2bd3 |
275 | static $header, $headerlength; |
276 | |
277 | if ($header == '') { |
278 | $to = expandAddrs(parseAddrs($t)); |
279 | $cc = expandAddrs(parseAddrs($c)); |
280 | $bcc = expandAddrs(parseAddrs($b)); |
281 | if (isset($identity) && $identity != 'default') { |
282 | $reply_to = getPref($data_dir, $username, 'reply_to' . $identity); |
283 | $from = getPref($data_dir, $username, 'full_name' . $identity); |
a3fbf5a4 |
284 | $from_addr = getPref($data_dir, $username, |
285 | 'email_address' . $identity); |
e25c2bd3 |
286 | } else { |
aaf9abef |
287 | $reply_to = getPref($data_dir, $username, 'reply_to'); |
288 | $from = getPref($data_dir, $username, 'full_name'); |
289 | $from_addr = getPref($data_dir, $username, 'email_address'); |
e25c2bd3 |
290 | } |
291 | |
a1e937bb |
292 | if ($from_addr == '') { |
ec9f1c41 |
293 | $from_addr = $popuser.'@'.$domain; |
a1e937bb |
294 | } |
e25c2bd3 |
295 | |
296 | $to_list = getLineOfAddrs($to); |
297 | $cc_list = getLineOfAddrs($cc); |
298 | $bcc_list = getLineOfAddrs($bcc); |
299 | |
300 | /* Encoding 8-bit characters and making from line */ |
301 | $subject = encodeHeader($subject); |
a1e937bb |
302 | if ($from == '') { |
7b67334e |
303 | $from = "<$from_addr>"; |
a1e937bb |
304 | } |
305 | else { |
ec9f1c41 |
306 | $from = '"' . encodeHeader($from) . "\" <$from_addr>"; |
a1e937bb |
307 | } |
e25c2bd3 |
308 | |
309 | /* This creates an RFC 822 date */ |
310 | $date = date("D, j M Y H:i:s ", mktime()) . timezone(); |
311 | |
312 | /* Create a message-id */ |
313 | $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.'; |
314 | $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>'; |
315 | |
316 | /* Make an RFC822 Received: line */ |
a1e937bb |
317 | if (isset($REMOTE_HOST)) { |
8a2848f0 |
318 | $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])"; |
a1e937bb |
319 | } |
320 | else { |
8a2848f0 |
321 | $received_from = $REMOTE_ADDR; |
a1e937bb |
322 | } |
77b88425 |
323 | |
e25c2bd3 |
324 | if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) { |
a1e937bb |
325 | if ($HTTP_X_FORWARDED_FOR == '') { |
e25c2bd3 |
326 | $HTTP_X_FORWARDED_FOR = 'unknown'; |
a1e937bb |
327 | } |
9949206d |
328 | $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)"; |
7e235a1a |
329 | } |
330 | |
a3fbf5a4 |
331 | $header = "Received: from $received_from" . $rn; |
332 | $header .= " (SquirrelMail authenticated user $username)" . $rn; |
333 | $header .= " by $SERVER_NAME with HTTP;" . $rn; |
334 | $header .= " $date" . $rn; |
7e235a1a |
335 | |
e25c2bd3 |
336 | /* Insert the rest of the header fields */ |
a3fbf5a4 |
337 | $header .= "Message-ID: $message_id" . $rn; |
338 | $header .= "Date: $date" . $rn; |
339 | $header .= "Subject: $subject" . $rn; |
340 | $header .= "From: $from" . $rn; |
341 | $header .= "To: $to_list" . $rn; // Who it's TO |
7e235a1a |
342 | |
604e2c03 |
343 | if (isset($more_headers["Content-Type"])) { |
7e235a1a |
344 | $contentType = $more_headers["Content-Type"]; |
345 | unset($more_headers["Content-Type"]); |
346 | } |
604e2c03 |
347 | else { |
dd7766e0 |
348 | if (isMultipart($session)) { |
7e235a1a |
349 | $contentType = "multipart/mixed;"; |
604e2c03 |
350 | } |
351 | else { |
7e235a1a |
352 | if ($default_charset != '') { |
353 | $contentType = 'text/plain; charset='.$default_charset; |
354 | } |
355 | else { |
356 | $contentType = 'text/plain;'; |
357 | } |
358 | } |
77b88425 |
359 | } |
7e235a1a |
360 | |
361 | /* Insert headers from the $more_headers array */ |
362 | if(is_array($more_headers)) { |
e25c2bd3 |
363 | reset($more_headers); |
364 | while(list($h_name, $h_val) = each($more_headers)) { |
a3fbf5a4 |
365 | $header .= sprintf("%s: %s%s", $h_name, $h_val, $rn); |
e25c2bd3 |
366 | } |
367 | } |
7e235a1a |
368 | |
e25c2bd3 |
369 | if ($cc_list) { |
a3fbf5a4 |
370 | $header .= "Cc: $cc_list" . $rn; // Who the CCs are |
e25c2bd3 |
371 | } |
7e235a1a |
372 | |
a1e937bb |
373 | if ($reply_to != '') { |
a3fbf5a4 |
374 | $header .= "Reply-To: $reply_to" . $rn; |
a1e937bb |
375 | } |
7e235a1a |
376 | |
e25c2bd3 |
377 | if ($useSendmail) { |
7b67334e |
378 | if ($bcc_list) { |
e25c2bd3 |
379 | // BCCs is removed from header by sendmail |
a3fbf5a4 |
380 | $header .= "Bcc: $bcc_list" . $rn; |
7b67334e |
381 | } |
e25c2bd3 |
382 | } |
7e235a1a |
383 | |
a3fbf5a4 |
384 | /* Identify SquirrelMail */ |
385 | $header .= "X-Mailer: SquirrelMail (version $version)" . $rn; |
77b88425 |
386 | |
a1e937bb |
387 | /* Do the MIME-stuff */ |
a3fbf5a4 |
388 | $header .= "MIME-Version: 1.0" . $rn; |
7e235a1a |
389 | |
dd7766e0 |
390 | if (isMultipart($session)) { |
604e2c03 |
391 | $header .= 'Content-Type: '.$contentType.' boundary="'; |
392 | $header .= mimeBoundary(); |
a3fbf5a4 |
393 | $header .= "\"$rn"; |
e25c2bd3 |
394 | } else { |
a3fbf5a4 |
395 | $header .= 'Content-Type: ' . $contentType . $rn; |
396 | $header .= "Content-Transfer-Encoding: 8bit" . $rn; |
e25c2bd3 |
397 | } |
a3fbf5a4 |
398 | $header .= $rn; // One blank line to separate header and body |
e25c2bd3 |
399 | |
400 | $headerlength = strlen($header); |
401 | } |
402 | |
a1e937bb |
403 | /* Write the header */ |
e25c2bd3 |
404 | fputs ($fp, $header); |
405 | |
406 | return $headerlength; |
407 | } |
17ce8467 |
408 | |
a1e937bb |
409 | /* Send the body |
410 | */ |
dd7766e0 |
411 | function writeBody ($fp, $passedBody, $session) { |
e25c2bd3 |
412 | global $default_charset; |
a3fbf5a4 |
413 | /** |
414 | * Get delimiter. |
415 | */ |
416 | $rn = sqm_nrn(); |
417 | |
e25c2bd3 |
418 | $attachmentlength = 0; |
419 | |
dd7766e0 |
420 | if (isMultipart($session)) { |
a3fbf5a4 |
421 | $body = '--'.mimeBoundary() . $rn; |
e25c2bd3 |
422 | |
a1e937bb |
423 | if ($default_charset != "") { |
a3fbf5a4 |
424 | $body .= "Content-Type: text/plain; charset=$default_charset".$rn; |
a1e937bb |
425 | } |
426 | else { |
a3fbf5a4 |
427 | $body .= "Content-Type: text/plain" . $rn; |
a1e937bb |
428 | } |
e25c2bd3 |
429 | |
a3fbf5a4 |
430 | $body .= "Content-Transfer-Encoding: 8bit" . $rn . $rn; |
431 | $body .= $passedBody . $rn . $rn; |
e25c2bd3 |
432 | fputs ($fp, $body); |
433 | |
dd7766e0 |
434 | $attachmentlength = attachFiles($fp, $session); |
e25c2bd3 |
435 | |
a1e937bb |
436 | if (!isset($postbody)) { |
437 | $postbody = ""; |
438 | } |
a3fbf5a4 |
439 | $postbody .= $rn . "--" . mimeBoundary() . "--" . $rn . $rn; |
e25c2bd3 |
440 | fputs ($fp, $postbody); |
441 | } else { |
a3fbf5a4 |
442 | $body = $passedBody . $rn; |
e25c2bd3 |
443 | fputs ($fp, $body); |
a3fbf5a4 |
444 | $postbody = $rn; |
e25c2bd3 |
445 | fputs ($fp, $postbody); |
446 | } |
7e235a1a |
447 | |
e25c2bd3 |
448 | return (strlen($body) + strlen($postbody) + $attachmentlength); |
449 | } |
17ce8467 |
450 | |
a1e937bb |
451 | /* Send mail using the sendmail command |
452 | */ |
dd7766e0 |
453 | function sendSendmail($t, $c, $b, $subject, $body, $more_headers, $session) { |
e25c2bd3 |
454 | global $sendmail_path, $popuser, $username, $domain; |
455 | |
a1e937bb |
456 | /* Build envelope sender address. Make sure it doesn't contain |
457 | * spaces or other "weird" chars that would allow a user to |
458 | * exploit the shell/pipe it is used in. |
459 | */ |
e25c2bd3 |
460 | $envelopefrom = "$popuser@$domain"; |
461 | $envelopefrom = ereg_replace("[[:blank:]]",'', $envelopefrom); |
462 | $envelopefrom = ereg_replace("[[:space:]]",'', $envelopefrom); |
463 | $envelopefrom = ereg_replace("[[:cntrl:]]",'', $envelopefrom); |
464 | |
a3fbf5a4 |
465 | /** |
466 | * open pipe to sendmail or qmail-inject |
467 | * (qmail-inject doesn't accept -t param) |
468 | */ |
e25c2bd3 |
469 | if (strstr($sendmail_path, "qmail-inject")) { |
470 | $fp = popen (escapeshellcmd("$sendmail_path -f$envelopefrom"), "w"); |
471 | } else { |
472 | $fp = popen (escapeshellcmd("$sendmail_path -t -f$envelopefrom"), "w"); |
473 | } |
474 | |
a3fbf5a4 |
475 | $headerlength = write822Header ($fp, $t, $c, $b, $subject, |
476 | $more_headers, $session); |
dd7766e0 |
477 | $bodylength = writeBody($fp, $body, $session); |
e25c2bd3 |
478 | |
479 | pclose($fp); |
480 | |
481 | return ($headerlength + $bodylength); |
482 | } |
483 | |
484 | function smtpReadData($smtpConnection) { |
485 | $read = fgets($smtpConnection, 1024); |
486 | $counter = 0; |
487 | while ($read) { |
488 | echo $read . '<BR>'; |
489 | $data[$counter] = $read; |
490 | $read = fgets($smtpConnection, 1024); |
491 | $counter++; |
492 | } |
493 | } |
494 | |
dd7766e0 |
495 | function sendSMTP($t, $c, $b, $subject, $body, $more_headers, $session) { |
e25c2bd3 |
496 | global $username, $popuser, $domain, $version, $smtpServerAddress, |
497 | $smtpPort, $data_dir, $color, $use_authenticated_smtp, $identity, |
498 | $key, $onetimepad; |
499 | |
500 | $to = expandRcptAddrs(parseAddrs($t)); |
501 | $cc = expandRcptAddrs(parseAddrs($c)); |
502 | $bcc = expandRcptAddrs(parseAddrs($b)); |
a1e937bb |
503 | if (isset($identity) && $identity != 'default') { |
a3fbf5a4 |
504 | $from_addr = getPref($data_dir, $username, |
505 | 'email_address' . $identity); |
a1e937bb |
506 | } |
507 | else { |
e25c2bd3 |
508 | $from_addr = getPref($data_dir, $username, 'email_address'); |
a1e937bb |
509 | } |
e25c2bd3 |
510 | |
a1e937bb |
511 | if (!$from_addr) { |
e25c2bd3 |
512 | $from_addr = "$popuser@$domain"; |
a1e937bb |
513 | } |
2044f95a |
514 | |
515 | /* POP3 BEFORE SMTP CODE HERE */ |
516 | global $pop_before_smtp; |
517 | if (isset($pop_before_smtp) && $pop_before_smtp === true) { |
518 | if (!isset($pop_port)) { |
519 | $pop_port = 110; |
520 | } |
521 | if (!isset($pop_server)) { |
522 | $pop_server = $smtpServerAddress; /* usually the same host! */ |
523 | } |
524 | $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str); |
525 | if (!$popConnection) { |
526 | error_log("Error connecting to POP Server ($pop_server:$pop_port)" |
527 | . " $err_no : $err_str"); |
528 | } else { |
529 | $tmp = fgets($popConnection, 1024); /* banner */ |
530 | if (!eregi("^\+OK", $tmp, $regs)) { |
531 | return(0); |
532 | } |
533 | fputs($popConnection, "USER $username\r\n"); |
534 | $tmp = fgets($popConnection, 1024); |
535 | if (!eregi("^\+OK", $tmp, $regs)) { |
536 | return(0); |
537 | } |
538 | fputs($popConnection, 'PASS ' . OneTimePadDecrypt($key, $onetimepad) . "\r\n"); |
539 | $tmp = fgets($popConnection, 1024); |
540 | if (!eregi("^\+OK", $tmp, $regs)) { |
541 | return(0); |
542 | } |
543 | fputs($popConnection, "QUIT\r\n"); /* log off */ |
544 | fclose($popConnection); |
545 | } |
546 | } |
e25c2bd3 |
547 | |
a3fbf5a4 |
548 | $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, |
549 | $errorNumber, $errorString); |
e25c2bd3 |
550 | if (!$smtpConnection) { |
551 | echo 'Error connecting to SMTP Server.<br>'; |
552 | echo "$errorNumber : $errorString<br>"; |
553 | exit; |
554 | } |
555 | $tmp = fgets($smtpConnection, 1024); |
14c62c12 |
556 | if (errorCheck($tmp, $smtpConnection)!=5) { |
557 | return(0); |
558 | } |
e25c2bd3 |
559 | |
560 | $to_list = getLineOfAddrs($to); |
561 | $cc_list = getLineOfAddrs($cc); |
562 | |
a1e937bb |
563 | /* Lets introduce ourselves */ |
a3fbf5a4 |
564 | if (! isset ($use_authenticated_smtp) |
565 | || $use_authenticated_smtp == false) { |
e25c2bd3 |
566 | fputs($smtpConnection, "HELO $domain\r\n"); |
567 | $tmp = fgets($smtpConnection, 1024); |
568 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
569 | } else { |
570 | fputs($smtpConnection, "EHLO $domain\r\n"); |
571 | $tmp = fgets($smtpConnection, 1024); |
572 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
573 | |
574 | fputs($smtpConnection, "AUTH LOGIN\r\n"); |
575 | $tmp = fgets($smtpConnection, 1024); |
a1e937bb |
576 | if (errorCheck($tmp, $smtpConnection)!=5) { |
577 | return(0); |
578 | } |
77b88425 |
579 | |
e25c2bd3 |
580 | fputs($smtpConnection, base64_encode ($username) . "\r\n"); |
581 | $tmp = fgets($smtpConnection, 1024); |
a1e937bb |
582 | if (errorCheck($tmp, $smtpConnection)!=5) { |
583 | return(0); |
584 | } |
e25c2bd3 |
585 | |
a3fbf5a4 |
586 | fputs($smtpConnection, base64_encode |
587 | (OneTimePadDecrypt($key, $onetimepad)) . "\r\n"); |
e25c2bd3 |
588 | $tmp = fgets($smtpConnection, 1024); |
a1e937bb |
589 | if (errorCheck($tmp, $smtpConnection)!=5) { |
590 | return(0); |
591 | } |
e25c2bd3 |
592 | } |
593 | |
a1e937bb |
594 | /* Ok, who is sending the message? */ |
e25c2bd3 |
595 | fputs($smtpConnection, "MAIL FROM: <$from_addr>\r\n"); |
596 | $tmp = fgets($smtpConnection, 1024); |
a1e937bb |
597 | if (errorCheck($tmp, $smtpConnection)!=5) { |
598 | return(0); |
599 | } |
e25c2bd3 |
600 | |
a1e937bb |
601 | /* send who the recipients are */ |
e25c2bd3 |
602 | for ($i = 0; $i < count($to); $i++) { |
603 | fputs($smtpConnection, "RCPT TO: $to[$i]\r\n"); |
604 | $tmp = fgets($smtpConnection, 1024); |
a1e937bb |
605 | if (errorCheck($tmp, $smtpConnection)!=5) { |
606 | return(0); |
607 | } |
e25c2bd3 |
608 | } |
609 | for ($i = 0; $i < count($cc); $i++) { |
610 | fputs($smtpConnection, "RCPT TO: $cc[$i]\r\n"); |
611 | $tmp = fgets($smtpConnection, 1024); |
a1e937bb |
612 | if (errorCheck($tmp, $smtpConnection)!=5) { |
613 | return(0); |
614 | } |
e25c2bd3 |
615 | } |
616 | for ($i = 0; $i < count($bcc); $i++) { |
617 | fputs($smtpConnection, "RCPT TO: $bcc[$i]\r\n"); |
618 | $tmp = fgets($smtpConnection, 1024); |
77b88425 |
619 | if (errorCheck($tmp, $smtpConnection)!=5) { |
a1e937bb |
620 | return(0); |
621 | } |
e25c2bd3 |
622 | } |
77b88425 |
623 | |
a1e937bb |
624 | /* Lets start sending the actual message */ |
e25c2bd3 |
625 | fputs($smtpConnection, "DATA\r\n"); |
626 | $tmp = fgets($smtpConnection, 1024); |
a1e937bb |
627 | if (errorCheck($tmp, $smtpConnection)!=5) { |
628 | return(0); |
629 | } |
77b88425 |
630 | |
a1e937bb |
631 | /* Send the message */ |
a3fbf5a4 |
632 | $headerlength = write822Header ($smtpConnection, $t, $c, $b, |
633 | $subject, $more_headers, $session); |
dd7766e0 |
634 | $bodylength = writeBody($smtpConnection, $body, $session); |
e25c2bd3 |
635 | |
a1e937bb |
636 | fputs($smtpConnection, ".\r\n"); /* end the DATA part */ |
e25c2bd3 |
637 | $tmp = fgets($smtpConnection, 1024); |
638 | $num = errorCheck($tmp, $smtpConnection, true); |
639 | if ($num != 250) { |
e25c2bd3 |
640 | return(0); |
641 | } |
642 | |
a1e937bb |
643 | fputs($smtpConnection, "QUIT\r\n"); /* log off */ |
e25c2bd3 |
644 | |
645 | fclose($smtpConnection); |
646 | |
647 | return ($headerlength + $bodylength); |
648 | } |
60994e13 |
649 | |
60994e13 |
650 | |
e25c2bd3 |
651 | function errorCheck($line, $smtpConnection, $verbose = false) { |
8f8f3a4e |
652 | global $color, $compose_new_win; |
e25c2bd3 |
653 | |
a1e937bb |
654 | /* Read new lines on a multiline response */ |
e25c2bd3 |
655 | $lines = $line; |
656 | while(ereg("^[0-9]+-", $line)) { |
657 | $line = fgets($smtpConnection, 1024); |
658 | $lines .= $line; |
659 | } |
660 | |
a1e937bb |
661 | /* Status: 0 = fatal |
662 | * 5 = ok |
663 | */ |
e25c2bd3 |
664 | $err_num = substr($line, 0, strpos($line, " ")); |
665 | switch ($err_num) { |
666 | case 500: $message = 'Syntax error; command not recognized'; |
667 | $status = 0; |
668 | break; |
669 | case 501: $message = 'Syntax error in parameters or arguments'; |
670 | $status = 0; |
671 | break; |
672 | case 502: $message = 'Command not implemented'; |
673 | $status = 0; |
674 | break; |
675 | case 503: $message = 'Bad sequence of commands'; |
676 | $status = 0; |
677 | break; |
678 | case 504: $message = 'Command parameter not implemented'; |
679 | $status = 0; |
a1e937bb |
680 | break; |
e25c2bd3 |
681 | |
682 | case 211: $message = 'System status, or system help reply'; |
683 | $status = 5; |
684 | break; |
685 | case 214: $message = 'Help message'; |
686 | $status = 5; |
687 | break; |
688 | |
e25c2bd3 |
689 | case 220: $message = 'Service ready'; |
690 | $status = 5; |
691 | break; |
692 | case 221: $message = 'Service closing transmission channel'; |
693 | $status = 5; |
694 | break; |
a1e937bb |
695 | |
e25c2bd3 |
696 | case 421: $message = 'Service not available, closing chanel'; |
697 | $status = 0; |
698 | break; |
699 | |
a1e937bb |
700 | case 235: return(5); |
701 | break; |
e25c2bd3 |
702 | case 250: $message = 'Requested mail action okay, completed'; |
703 | $status = 5; |
704 | break; |
705 | case 251: $message = 'User not local; will forward'; |
706 | $status = 5; |
707 | break; |
708 | case 334: return(5); break; |
709 | case 450: $message = 'Requested mail action not taken: mailbox unavailable'; |
710 | $status = 0; |
711 | break; |
712 | case 550: $message = 'Requested action not taken: mailbox unavailable'; |
713 | $status = 0; |
714 | break; |
715 | case 451: $message = 'Requested action aborted: error in processing'; |
716 | $status = 0; |
717 | break; |
718 | case 551: $message = 'User not local; please try forwarding'; |
719 | $status = 0; |
720 | break; |
721 | case 452: $message = 'Requested action not taken: insufficient system storage'; |
722 | $status = 0; |
723 | break; |
724 | case 552: $message = 'Requested mail action aborted: exceeding storage allocation'; |
725 | $status = 0; |
726 | break; |
727 | case 553: $message = 'Requested action not taken: mailbox name not allowed'; |
728 | $status = 0; |
729 | break; |
730 | case 354: $message = 'Start mail input; end with .'; |
731 | $status = 5; |
732 | break; |
733 | case 554: $message = 'Transaction failed'; |
734 | $status = 0; |
735 | break; |
736 | default: $message = 'Unknown response: '. nl2br(htmlspecialchars($lines)); |
737 | $status = 0; |
738 | $error_num = '001'; |
739 | break; |
740 | } |
7e235a1a |
741 | |
e25c2bd3 |
742 | if ($status == 0) { |
743 | include_once('../functions/page_header.php'); |
8f8f3a4e |
744 | if ($compose_new_win == '1') { |
745 | compose_Header($color, 'None'); |
746 | } |
7e235a1a |
747 | else { |
8f8f3a4e |
748 | displayPageHeader($color, 'None'); |
749 | } |
e25c2bd3 |
750 | include_once('../functions/display_messages.php'); |
751 | $lines = nl2br(htmlspecialchars($lines)); |
752 | $msg = $message . "<br>\nServer replied: $lines"; |
753 | plain_error_message($msg, $color); |
754 | } |
755 | if (! $verbose) return $status; |
756 | return $err_num; |
757 | } |
758 | |
67c2f5ea |
759 | /* create new reference header per rfc2822 */ |
760 | |
761 | function calculate_references($refs, $inreplyto, $old_reply_to) { |
a3fbf5a4 |
762 | /** |
763 | * Get the delimiter. |
764 | */ |
765 | $rn = sqm_nrn(); |
67c2f5ea |
766 | $refer = ""; |
767 | for ($i=1;$i<count($refs[0]);$i++) { |
768 | if (!empty($refs[0][$i])) { |
769 | if (preg_match("/^References:(.+)$/", $refs[0][$i], $regs)) { |
770 | $refer = trim($regs[1]); |
771 | } |
772 | else { |
773 | $refer .= ' ' . trim($refs[0][$i]); |
774 | } |
775 | } |
776 | } |
777 | $refer = trim($refer); |
778 | if (strlen($refer) > 2) { |
779 | $refer .= ' ' . $inreplyto; |
780 | } |
781 | else { |
782 | if (!empty($old_reply_to)) { |
783 | $refer .= $old_reply_to . ' ' . $inreplyto; |
784 | } |
785 | else { |
786 | $refer .= $inreplyto; |
2044f95a |
787 | } |
67c2f5ea |
788 | } |
789 | trim($refer); |
a3fbf5a4 |
790 | $refer = str_replace(' ', "$rn ", $refer); |
67c2f5ea |
791 | return $refer; |
792 | } |
793 | |
a3fbf5a4 |
794 | function sendMessage($t, $c, $b, $subject, $body, $reply_id, $MDN, |
795 | $prio = 3, $session) { |
77b88425 |
796 | global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad, |
a3fbf5a4 |
797 | $data_dir, $username, $domain, $key, $version, $sent_folder, |
798 | $imapServerAddress, $imapPort, $default_use_priority, $more_headers, |
799 | $request_mdn, $request_dr; |
77b88425 |
800 | |
a3fbf5a4 |
801 | /** |
802 | * Get the delimiter. |
803 | */ |
804 | $rn = sqm_nrn(); |
e25c2bd3 |
805 | $more_headers = Array(); |
806 | |
77b88425 |
807 | do_hook('smtp_send'); |
808 | |
a3fbf5a4 |
809 | $imap_stream = sqimap_login($username, $key, $imapServerAddress, |
810 | $imapPort, 1); |
77b88425 |
811 | |
e25c2bd3 |
812 | if (isset($reply_id) && $reply_id) { |
813 | sqimap_mailbox_select ($imap_stream, $mailbox); |
814 | sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, 'Answered'); |
77b88425 |
815 | |
816 | /* Insert In-Reply-To and References headers if the |
a1e937bb |
817 | * message-id of the message we reply to is set (longer than "<>") |
818 | * The References header should really be the old Referenced header |
67c2f5ea |
819 | * with the message ID appended, and now it is (jmunro) |
a1e937bb |
820 | */ |
e25c2bd3 |
821 | $hdr = sqimap_get_small_header ($imap_stream, $reply_id, false); |
822 | if(strlen($hdr->message_id) > 2) { |
67c2f5ea |
823 | $refs = get_reference_header ($imap_stream, $reply_id); |
824 | $inreplyto = $hdr->message_id; |
2044f95a |
825 | $old_reply_to = $hdr->inrepto; |
67c2f5ea |
826 | $refer = calculate_references ($refs, $inreplyto, $old_reply_to); |
827 | $more_headers['In-Reply-To'] = $inreplyto; |
828 | $more_headers['References'] = $refer; |
e25c2bd3 |
829 | } |
830 | } |
831 | if ($default_use_priority) { |
832 | $more_headers = array_merge($more_headers, createPriorityHeaders($prio)); |
833 | } |
77b88425 |
834 | |
604e2c03 |
835 | $requestRecipt = 0; |
836 | if (isset($request_dr)) { |
837 | $requestRecipt += 1; |
838 | } |
839 | if (isset($request_mdn)) { |
840 | $requestRecipt += 2; |
841 | } |
842 | if ( $requestRecipt > 0) { |
843 | $more_headers = array_merge($more_headers, createReceiptHeaders($requestRecipt)); |
844 | } |
845 | |
a1e937bb |
846 | /* In order to remove the problem of users not able to create |
847 | * messages with "." on a blank line, RFC821 has made provision |
848 | * in section 4.5.2 (Transparency). |
849 | */ |
e25c2bd3 |
850 | $body = ereg_replace("\n\\.", "\n..", $body); |
851 | $body = ereg_replace("^\\.", "..", $body); |
77b88425 |
852 | |
a1e937bb |
853 | /* this is to catch all plain \n instances and |
854 | * replace them with \r\n. All newlines were converted |
855 | * into just \n inside the compose.php file. |
a3fbf5a4 |
856 | * But only if delimiter is, in fact, \r\n. |
a1e937bb |
857 | */ |
a3fbf5a4 |
858 | if ($rn == "\r\n"){ |
859 | $body = ereg_replace("\n", "\r\n", $body); |
860 | } |
604e2c03 |
861 | |
862 | if ($MDN) { |
863 | $more_headers["Content-Type"] = "multipart/report; ". |
864 | "report-type=disposition-notification;"; |
865 | } |
77b88425 |
866 | |
e25c2bd3 |
867 | if ($useSendmail) { |
a3fbf5a4 |
868 | $length = sendSendmail($t, $c, $b, $subject, $body, $more_headers, |
869 | $session); |
e25c2bd3 |
870 | } else { |
a3fbf5a4 |
871 | $length = sendSMTP($t, $c, $b, $subject, $body, $more_headers, |
872 | $session); |
e25c2bd3 |
873 | } |
e25c2bd3 |
874 | if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) { |
875 | sqimap_append ($imap_stream, $sent_folder, $length); |
a3fbf5a4 |
876 | write822Header ($imap_stream, $t, $c, $b, $subject, $more_headers, |
877 | $session); |
dd7766e0 |
878 | writeBody ($imap_stream, $body, $session); |
e25c2bd3 |
879 | sqimap_append_done ($imap_stream); |
880 | } |
881 | sqimap_logout($imap_stream); |
a1e937bb |
882 | /* Delete the files uploaded for attaching (if any). |
883 | * only if $length != 0 (if there was no error) |
884 | */ |
77b88425 |
885 | if ($length) { |
dd7766e0 |
886 | ClearAttachments($session); |
77b88425 |
887 | } |
888 | |
e25c2bd3 |
889 | return $length; |
890 | } |
d1b8b679 |
891 | |
e25c2bd3 |
892 | function createPriorityHeaders($prio) { |
893 | $prio_headers = Array(); |
77b88425 |
894 | $prio_headers['X-Priority'] = $prio; |
895 | |
e25c2bd3 |
896 | switch($prio) { |
77b88425 |
897 | case 1: $prio_headers['Importance'] = 'High'; |
898 | $prio_headers['X-MSMail-Priority'] = 'High'; |
e25c2bd3 |
899 | break; |
77b88425 |
900 | |
901 | case 3: $prio_headers['Importance'] = 'Normal'; |
902 | $prio_headers['X-MSMail-Priority'] = 'Normal'; |
e25c2bd3 |
903 | break; |
77b88425 |
904 | |
e25c2bd3 |
905 | case 5: |
77b88425 |
906 | $prio_headers['Importance'] = 'Low'; |
907 | $prio_headers['X-MSMail-Priority'] = 'Low'; |
e25c2bd3 |
908 | break; |
909 | } |
910 | return $prio_headers; |
911 | } |
020abcf3 |
912 | |
604e2c03 |
913 | function createReceiptHeaders($receipt) { |
0804c276 |
914 | |
915 | GLOBAL $data_dir, $username; |
916 | |
917 | $receipt_headers = Array(); |
918 | $from_addr = getPref($data_dir, $username, 'email_address'); |
919 | $from = getPref($data_dir, $username, 'full_name'); |
920 | |
921 | if ($from == '') { |
922 | $from = "<$from_addr>"; |
923 | } |
924 | else { |
925 | $from = '"' . encodeHeader($from) . "\" <$from_addr>"; |
926 | } |
927 | |
928 | /* On Delivery */ |
929 | if ( $receipt == 1 |
930 | || $receipt == 3 ) { |
931 | $receipt_headers["Return-Receipt-To"] = $from; |
932 | } |
933 | /* On Read */ |
934 | if ($receipt == 2 |
935 | || $receipt == 3 ) { |
936 | /* Pegasus Mail */ |
937 | $receipt_headers["X-Confirm-Reading-To"] = $from; |
938 | /* RFC 2298 */ |
939 | $receipt_headers["Disposition-Notification-To"] = $from; |
940 | } |
941 | return $receipt_headers; |
942 | } |
604e2c03 |
943 | |
944 | |
020abcf3 |
945 | ?> |