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