Updated version for 1.2.0 release...
[squirrelmail.git] / functions / smtp.php
CommitLineData
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/*****************************************************************/
16/*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
17/*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
18/*** + Base level indent should begin at left margin, as ***/
19/*** the require_once and global lines below. ***/
20/*** + All identation should consist of four space blocks ***/
21/*** + Tab characters are evil. ***/
22/*** + all comments should use "slash-star ... star-slash" ***/
23/*** style -- no pound characters, no slash-slash style ***/
24/*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
25/*** ALWAYS USE { AND } CHARACTERS!!! ***/
26/*** + Please use ' instead of ", when possible. Note " ***/
27/*** should always be used in _( ) function calls. ***/
28/*** Thank you for your help making the SM code more readable. ***/
29/*****************************************************************/
f435778e 30
35586184 31require_once('../functions/addressbook.php');
32require_once('../functions/plugin.php');
3392dc86 33require_once('../functions/prefs.php');
35586184 34
35global $username, $popuser, $domain;
465db5d7 36
31afdefb 37 // This should most probably go to some initialization...
38 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
39 $popuser = $usernamedata[1];
40 $domain = $usernamedata[2];
7350889b 41 unset($usernamedata);
31afdefb 42 } else {
43 $popuser = $username;
44 }
45 // We need domain for smtp
46 if (!$domain)
ec9f1c41 47 $domain = getenv('HOSTNAME');
31afdefb 48
df15de21 49 // Returns true only if this message is multipart
50 function isMultipart () {
4ba45d11 51 global $attachments;
52
53 if (count($attachments)>0)
54 return true;
55 else
56 return false;
df15de21 57 }
58
a4102fd7 59 // looks up aliases in the addressbook and expands them to
60 // the full address.
cb13511f 61 // Adds @$domain if it wasn't in the address book and if it
62 // doesn't have an @ symbol in it
a4102fd7 63 function expandAddrs ($array) {
cb13511f 64 global $domain;
65
44bace96 66 // don't show errors -- kinda critical that we don't see
67 // them here since the redirect won't work if we do show them
68 $abook = addressbook_init(false);
a4102fd7 69 for ($i=0; $i < count($array); $i++) {
70 $result = $abook->lookup($array[$i]);
71 $ret = "";
72 if (isset($result['email'])) {
73 if (isset($result['name'])) {
74 $ret = '"'.$result['name'].'" ';
75 }
76 $ret .= '<'.$result['email'].'>';
77 $array[$i] = $ret;
78 }
18e206dd 79 else
80 {
912c949d 81 if (strpos($array[$i], '@') === false)
82 $array[$i] .= '@' . $domain;
18e206dd 83 $array[$i] = '<' . $array[$i] . '>';
84 }
a4102fd7 85 }
86 return $array;
87 }
88
93555af8 89
90 // looks up aliases in the addressbook and expands them to
91 // the RFC 821 valid RCPT address. ie <user@example.com>
92 // Adds @$domain if it wasn't in the address book and if it
93 // doesn't have an @ symbol in it
94 function expandRcptAddrs ($array) {
95 global $domain;
96
97 // don't show errors -- kinda critical that we don't see
98 // them here since the redirect won't work if we do show them
99 $abook = addressbook_init(false);
100 for ($i=0; $i < count($array); $i++) {
101 $result = $abook->lookup($array[$i]);
102 $ret = "";
103 if (isset($result['email'])) {
104 $ret = '<'.$result['email'].'>';
105 $array[$i] = $ret;
106 }
107 else
108 {
109 if (strpos($array[$i], '@') === false)
110 $array[$i] .= '@' . $domain;
111 $array[$i] = '<' . $array[$i] . '>';
112 }
113 }
114 return $array;
115 }
116
117
df15de21 118 // Attach the files that are due to be attached
4ba45d11 119 function attachFiles ($fp) {
3392dc86 120 global $attachments, $attachment_dir, $username;
4ba45d11 121
7b67334e 122 $length = 0;
123
3392dc86 124 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
a7d75834 125 if (isMultipart()) {
f972eb46 126 foreach ($attachments as $info)
127 {
4f601b14 128 if (isset($info['type']))
7d50f13c 129 $filetype = $info['type'];
130 else
ec9f1c41 131 $filetype = 'application/octet-stream';
a7d75834 132
ec9f1c41 133 $header = '--'.mimeBoundary()."\r\n";
f972eb46 134 $header .= "Content-Type: $filetype; name=\"" .
135 $info['remotefilename'] . "\"\r\n";
136 $header .= "Content-Disposition: attachment; filename=\"" .
137 $info['remotefilename'] . "\"\r\n";
a7d75834 138
f61de966 139 // Use 'rb' for NT systems -- read binary
140 // Unix doesn't care -- everything's binary! :-)
3392dc86 141
142 $filename = $hashed_attachment_dir . '/' . $info['localfilename'];
143 $file = fopen ($filename, 'rb');
aaf9abef 144 if (substr($filetype, 0, 5) == 'text/' ||
145 $filetype == 'message/rfc822') {
146 $header .= "\r\n";
147 fputs ($fp, $header);
148 $length += strlen($header);
149 while ($tmp = fgets($file, 4096)) {
150 $tmp = str_replace("\r\n", "\n", $tmp);
151 $tmp = str_replace("\r", "\n", $tmp);
152 $tmp = str_replace("\n", "\r\n", $tmp);
e9eca7fe 153 if (feof($fp) && substr($tmp, -2) != "\r\n")
154 $tmp .= "\r\n";
aaf9abef 155 fputs($fp, $tmp);
156 $length += strlen($tmp);
157 }
158 } else {
159 $header .= "Content-Transfer-Encoding: base64\r\n\r\n";
160 fputs ($fp, $header);
161 $length += strlen($header);
162 while ($tmp = fread($file, 570)) {
163 $encoded = chunk_split(base64_encode($tmp));
164 $length += strlen($encoded);
165 fputs ($fp, $encoded);
166 }
167 }
a7d75834 168 fclose ($file);
7b67334e 169 }
4ba45d11 170 }
7b67334e 171
172 return $length;
df15de21 173 }
174
a7d75834 175 // Delete files that are uploaded for attaching
176 function deleteAttachments() {
177 global $attachments, $attachment_dir;
178
3392dc86 179 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
a7d75834 180 if (isMultipart()) {
181 reset($attachments);
182 while (list($localname, $remotename) = each($attachments)) {
183 if (!ereg ("\\/", $localname)) {
3392dc86 184 $filename = $hashed_attachment_dir . '/' . $localname;
185 unlink ($filename);
186 unlink ("$filename.info");
a7d75834 187 }
188 }
189 }
190 }
191
df15de21 192 // Return a nice MIME-boundary
193 function mimeBoundary () {
7b67334e 194 static $mimeBoundaryString;
df15de21 195
196 if ($mimeBoundaryString == "") {
ed865adf 197 $mimeBoundaryString = "----=_" .
fe53c3b9 198 GenerateRandomString(60, '\'()+,-./:=?_', 7);
465db5d7 199 }
df15de21 200
201 return $mimeBoundaryString;
465db5d7 202 }
203
24397423 204 /* Time offset for correct timezone */
205 function timezone () {
b0d1b51e 206 global $invert_time;
207
ec9f1c41 208 $diff_second = date('Z');
d47b2518 209 if ($invert_time)
210 $diff_second = - $diff_second;
24397423 211 if ($diff_second > 0)
ec9f1c41 212 $sign = '+';
24397423 213 else
ec9f1c41 214 $sign = '-';
24397423 215
216 $diff_second = abs($diff_second);
217
218 $diff_hour = floor ($diff_second / 3600);
219 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
220
ec9f1c41 221 $zonename = '('.strftime('%Z').')';
24397423 222 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename);
223 return ($result);
224 }
225
df15de21 226 /* Print all the needed RFC822 headers */
60994e13 227 function write822Header ($fp, $t, $c, $b, $subject, $more_headers) {
a7d75834 228 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
31afdefb 229 global $data_dir, $username, $popuser, $domain, $version, $useSendmail;
9949206d 230 global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
aaf9abef 231 global $REMOTE_HOST, $identity;
465db5d7 232
7b67334e 233 // Storing the header to make sure the header is the same
234 // everytime the header is printed.
235 static $header, $headerlength;
236
ec9f1c41 237 if ($header == '') {
a4102fd7 238 $to = expandAddrs(parseAddrs($t));
239 $cc = expandAddrs(parseAddrs($c));
240 $bcc = expandAddrs(parseAddrs($b));
aaf9abef 241 if (isset($identity) && $identity != 'default')
242 {
243 $reply_to = getPref($data_dir, $username, 'reply_to' . $identity);
244 $from = getPref($data_dir, $username, 'full_name' . $identity);
245 $from_addr = getPref($data_dir, $username, 'email_address' . $identity);
246 }
247 else
248 {
249 $reply_to = getPref($data_dir, $username, 'reply_to');
250 $from = getPref($data_dir, $username, 'full_name');
251 $from_addr = getPref($data_dir, $username, 'email_address');
252 }
31afdefb 253
ec9f1c41 254 if ($from_addr == '')
255 $from_addr = $popuser.'@'.$domain;
7b67334e 256
257 $to_list = getLineOfAddrs($to);
258 $cc_list = getLineOfAddrs($cc);
259 $bcc_list = getLineOfAddrs($bcc);
8ae331c2 260
78e230b7 261 /* Encoding 8-bit characters and making from line */
d51894be 262 $subject = encodeHeader($subject);
ec9f1c41 263 if ($from == '')
7b67334e 264 $from = "<$from_addr>";
265 else
ec9f1c41 266 $from = '"' . encodeHeader($from) . "\" <$from_addr>";
7b67334e 267
a7d75834 268 /* This creates an RFC 822 date */
7b67334e 269 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
a7d75834 270
271 /* Create a message-id */
ec9f1c41 272 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
273 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
7b67334e 274
275 /* Make an RFC822 Received: line */
a5363806 276 if (isset($REMOTE_HOST))
8a2848f0 277 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
a5363806 278 else
8a2848f0 279 $received_from = $REMOTE_ADDR;
a5363806 280
9949206d 281 if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) {
ec9f1c41 282 if ($HTTP_X_FORWARDED_FOR == '')
283 $HTTP_X_FORWARDED_FOR = 'unknown';
9949206d 284 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
285 }
a5363806 286
8a2848f0 287 $header = "Received: from $received_from\r\n";
288 $header .= " (SquirrelMail authenticated user $username)\r\n";
289 $header .= " by $SERVER_NAME with HTTP;\r\n";
290 $header .= " $date\r\n";
7b67334e 291
a7d75834 292 /* Insert the rest of the header fields */
293 $header .= "Message-ID: $message_id\r\n";
7b67334e 294 $header .= "Date: $date\r\n";
295 $header .= "Subject: $subject\r\n";
296 $header .= "From: $from\r\n";
18e206dd 297 $header .= "To: $to_list\r\n"; // Who it's TO
60994e13 298
299 /* Insert headers from the $more_headers array */
300 if(is_array($more_headers)) {
301 reset($more_headers);
302 while(list($h_name, $h_val) = each($more_headers)) {
303 $header .= sprintf("%s: %s\r\n", $h_name, $h_val);
304 }
305 }
306
7b67334e 307 if ($cc_list) {
308 $header .= "Cc: $cc_list\r\n"; // Who the CCs are
df15de21 309 }
7b67334e 310
ec9f1c41 311 if ($reply_to != '')
7b67334e 312 $header .= "Reply-To: $reply_to\r\n";
313
314 if ($useSendmail) {
315 if ($bcc_list) {
316 // BCCs is removed from header by sendmail
317 $header .= "Bcc: $bcc_list\r\n";
318 }
319 }
320
321 $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail
322
323 // Do the MIME-stuff
f8308f16 324 $header .= "MIME-Version: 1.0\r\n";
7b67334e 325
326 if (isMultipart()) {
ec9f1c41 327 $header .= 'Content-Type: multipart/mixed; boundary="';
7b67334e 328 $header .= mimeBoundary();
329 $header .= "\"\r\n";
330 } else {
ec9f1c41 331 if ($default_charset != '')
17ce8467 332 $header .= "Content-Type: text/plain; charset=$default_charset\r\n";
333 else
334 $header .= "Content-Type: text/plain;\r\n";
7b67334e 335 $header .= "Content-Transfer-Encoding: 8bit\r\n";
336 }
337 $header .= "\r\n"; // One blank line to separate header and body
df15de21 338
7b67334e 339 $headerlength = strlen($header);
340 }
341
342 // Write the header
343 fputs ($fp, $header);
df15de21 344
7b67334e 345 return $headerlength;
346 }
df15de21 347
7b67334e 348 // Send the body
349 function writeBody ($fp, $passedBody) {
17ce8467 350 global $default_charset;
351
7b67334e 352 $attachmentlength = 0;
353
df15de21 354 if (isMultipart()) {
ec9f1c41 355 $body = '--'.mimeBoundary()."\r\n";
17ce8467 356
357 if ($default_charset != "")
358 $body .= "Content-Type: text/plain; charset=$default_charset\r\n";
359 else
360 $body .= "Content-Type: text/plain\r\n";
361
7b67334e 362 $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
d51894be 363 $body .= $passedBody . "\r\n\r\n";
7b67334e 364 fputs ($fp, $body);
365
6441f7c6 366 $attachmentlength = attachFiles($fp);
7b67334e 367
f8cfeb5c 368 if (!isset($postbody)) $postbody = "";
7b67334e 369 $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n";
370 fputs ($fp, $postbody);
df15de21 371 } else {
d51894be 372 $body = $passedBody . "\r\n";
7b67334e 373 fputs ($fp, $body);
374 $postbody = "\r\n";
375 fputs ($fp, $postbody);
df15de21 376 }
df15de21 377
7b67334e 378 return (strlen($body) + strlen($postbody) + $attachmentlength);
c99c5b31 379 }
465db5d7 380
c99c5b31 381 // Send mail using the sendmail command
60994e13 382 function sendSendmail($t, $c, $b, $subject, $body, $more_headers) {
31afdefb 383 global $sendmail_path, $popuser, $username, $domain;
7b67334e 384
6ebf8b30 385 // Build envelope sender address. Make sure it doesn't contain
386 // spaces or other "weird" chars that would allow a user to
387 // exploit the shell/pipe it is used in.
31afdefb 388 $envelopefrom = "$popuser@$domain";
ec9f1c41 389 $envelopefrom = ereg_replace("[[:blank:]]",'', $envelopefrom);
390 $envelopefrom = ereg_replace("[[:space:]]",'', $envelopefrom);
391 $envelopefrom = ereg_replace("[[:cntrl:]]",'', $envelopefrom);
6ebf8b30 392
e957cd4a 393 // open pipe to sendmail or qmail-inject (qmail-inject doesn't accept -t param)
394 if (strstr($sendmail_path, "qmail-inject")) {
395 $fp = popen (escapeshellcmd("$sendmail_path -f$envelopefrom"), "w");
396 } else {
397 $fp = popen (escapeshellcmd("$sendmail_path -t -f$envelopefrom"), "w");
398 }
399
60994e13 400 $headerlength = write822Header ($fp, $t, $c, $b, $subject, $more_headers);
7b67334e 401 $bodylength = writeBody($fp, $body);
465db5d7 402
403 pclose($fp);
7b67334e 404
0775b17f 405 return ($headerlength + $bodylength);
465db5d7 406 }
407
b8ea4ed6 408 function smtpReadData($smtpConnection) {
409 $read = fgets($smtpConnection, 1024);
410 $counter = 0;
411 while ($read) {
ec9f1c41 412 echo $read . '<BR>';
b8ea4ed6 413 $data[$counter] = $read;
414 $read = fgets($smtpConnection, 1024);
415 $counter++;
416 }
417 }
418
60994e13 419 function sendSMTP($t, $c, $b, $subject, $body, $more_headers) {
8de3c34d 420 global $username, $popuser, $domain, $version, $smtpServerAddress,
421 $smtpPort, $data_dir, $color, $use_authenticated_smtp, $identity,
422 $key, $onetimepad;
7831268e 423
93555af8 424 $to = expandRcptAddrs(parseAddrs($t));
425 $cc = expandRcptAddrs(parseAddrs($c));
426 $bcc = expandRcptAddrs(parseAddrs($b));
aaf9abef 427 if (isset($identity) && $identity != 'default')
428 $from_addr = getPref($data_dir, $username, 'email_address' . $identity);
429 else
430 $from_addr = getPref($data_dir, $username, 'email_address');
356d7825 431
31afdefb 432 if (!$from_addr)
433 $from_addr = "$popuser@$domain";
d3cdb279 434
c175e2e4 435 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
b8ea4ed6 436 if (!$smtpConnection) {
ec9f1c41 437 echo 'Error connecting to SMTP Server.<br>';
b8ea4ed6 438 echo "$errorNumber : $errorString<br>";
439 exit;
440 }
f8a9394d 441 $tmp = fgets($smtpConnection, 1024);
d5f34863 442 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
b8ea4ed6 443
40ee9452 444 $to_list = getLineOfAddrs($to);
445 $cc_list = getLineOfAddrs($cc);
446
447 /** Lets introduce ourselves */
d402e33e 448 if (! isset ($use_authenticated_smtp) || $use_authenticated_smtp == false) {
d2f4c914 449 fputs($smtpConnection, "HELO $domain\r\n");
450 $tmp = fgets($smtpConnection, 1024);
d5f34863 451 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
d2f4c914 452 } else {
453 fputs($smtpConnection, "EHLO $domain\r\n");
454 $tmp = fgets($smtpConnection, 1024);
d5f34863 455 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
d2f4c914 456
457 fputs($smtpConnection, "AUTH LOGIN\r\n");
458 $tmp = fgets($smtpConnection, 1024);
d5f34863 459 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
d2f4c914 460
461 fputs($smtpConnection, base64_encode ($username) . "\r\n");
462 $tmp = fgets($smtpConnection, 1024);
d5f34863 463 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
d2f4c914 464
16cad51a 465 fputs($smtpConnection, base64_encode (OneTimePadDecrypt($key, $onetimepad)) . "\r\n");
d2f4c914 466 $tmp = fgets($smtpConnection, 1024);
d5f34863 467 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
d2f4c914 468 }
7831268e 469
40ee9452 470 /** Ok, who is sending the message? */
f89627f3 471 fputs($smtpConnection, "MAIL FROM: <$from_addr>\r\n");
f8a9394d 472 $tmp = fgets($smtpConnection, 1024);
d5f34863 473 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
b8ea4ed6 474
40ee9452 475 /** send who the recipients are */
476 for ($i = 0; $i < count($to); $i++) {
18e206dd 477 fputs($smtpConnection, "RCPT TO: $to[$i]\r\n");
f8a9394d 478 $tmp = fgets($smtpConnection, 1024);
d5f34863 479 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
40ee9452 480 }
481 for ($i = 0; $i < count($cc); $i++) {
b4338e67 482 fputs($smtpConnection, "RCPT TO: $cc[$i]\r\n");
f8a9394d 483 $tmp = fgets($smtpConnection, 1024);
d5f34863 484 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
40ee9452 485 }
486 for ($i = 0; $i < count($bcc); $i++) {
b4338e67 487 fputs($smtpConnection, "RCPT TO: $bcc[$i]\r\n");
f8a9394d 488 $tmp = fgets($smtpConnection, 1024);
d5f34863 489 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
40ee9452 490 }
b8ea4ed6 491
40ee9452 492 /** Lets start sending the actual message */
3379d740 493 fputs($smtpConnection, "DATA\r\n");
f8a9394d 494 $tmp = fgets($smtpConnection, 1024);
d5f34863 495 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
7831268e 496
7b67334e 497 // Send the message
60994e13 498 $headerlength = write822Header ($smtpConnection, $t, $c, $b, $subject, $more_headers);
7b67334e 499 $bodylength = writeBody($smtpConnection, $body);
7831268e 500
3379d740 501 fputs($smtpConnection, ".\r\n"); // end the DATA part
f8a9394d 502 $tmp = fgets($smtpConnection, 1024);
354eb441 503 $num = errorCheck($tmp, $smtpConnection, true);
3021b626 504 if ($num != 250) {
d5f34863 505 $tmp = nl2br(htmlspecialchars($tmp));
506 displayPageHeader($color, 'None');
0fc2aca0 507 include_once('../functions/display_messages.php');
d5f34863 508 $msg = "Message not sent!<br>\nReason given: $tmp";
509 plain_error_message($msg, $color);
510 return(0);
3021b626 511 }
d3cdb279 512
3379d740 513 fputs($smtpConnection, "QUIT\r\n"); // log off
78509c54 514
515 fclose($smtpConnection);
7b67334e 516
517 return ($headerlength + $bodylength);
b8ea4ed6 518 }
3021b626 519
520
354eb441 521 function errorCheck($line, $smtpConnection, $verbose = false) {
28010579 522 global $color;
776c7431 523
f8a9394d 524 // Read new lines on a multiline response
525 $lines = $line;
526 while(ereg("^[0-9]+-", $line)) {
527 $line = fgets($smtpConnection, 1024);
528 $lines .= $line;
529 }
530
3021b626 531 // Status: 0 = fatal
532 // 5 = ok
533
534 $err_num = substr($line, 0, strpos($line, " "));
535 switch ($err_num) {
ec9f1c41 536 case 500: $message = 'Syntax error; command not recognized';
3021b626 537 $status = 0;
538 break;
ec9f1c41 539 case 501: $message = 'Syntax error in parameters or arguments';
3021b626 540 $status = 0;
541 break;
ec9f1c41 542 case 502: $message = 'Command not implemented';
3021b626 543 $status = 0;
544 break;
ec9f1c41 545 case 503: $message = 'Bad sequence of commands';
3021b626 546 $status = 0;
547 break;
ec9f1c41 548 case 504: $message = 'Command parameter not implemented';
3021b626 549 $status = 0;
550 break;
551
552
ec9f1c41 553 case 211: $message = 'System status, or system help reply';
3021b626 554 $status = 5;
555 break;
ec9f1c41 556 case 214: $message = 'Help message';
3021b626 557 $status = 5;
558 break;
559
560
ec9f1c41 561 case 220: $message = 'Service ready';
3021b626 562 $status = 5;
563 break;
ec9f1c41 564 case 221: $message = 'Service closing transmission channel';
3021b626 565 $status = 5;
566 break;
ec9f1c41 567 case 421: $message = 'Service not available, closing chanel';
3021b626 568 $status = 0;
569 break;
570
d5f34863 571 case 235: return(5); break;
ec9f1c41 572 case 250: $message = 'Requested mail action okay, completed';
3021b626 573 $status = 5;
574 break;
ec9f1c41 575 case 251: $message = 'User not local; will forward';
3021b626 576 $status = 5;
577 break;
d5f34863 578 case 334: return(5); break;
ec9f1c41 579 case 450: $message = 'Requested mail action not taken: mailbox unavailable';
3021b626 580 $status = 0;
581 break;
ec9f1c41 582 case 550: $message = 'Requested action not taken: mailbox unavailable';
3021b626 583 $status = 0;
584 break;
ec9f1c41 585 case 451: $message = 'Requested action aborted: error in processing';
3021b626 586 $status = 0;
587 break;
ec9f1c41 588 case 551: $message = 'User not local; please try forwarding';
3021b626 589 $status = 0;
590 break;
ec9f1c41 591 case 452: $message = 'Requested action not taken: insufficient system storage';
3021b626 592 $status = 0;
593 break;
ec9f1c41 594 case 552: $message = 'Requested mail action aborted: exceeding storage allocation';
3021b626 595 $status = 0;
596 break;
ec9f1c41 597 case 553: $message = 'Requested action not taken: mailbox name not allowed';
3021b626 598 $status = 0;
599 break;
ec9f1c41 600 case 354: $message = 'Start mail input; end with .';
3021b626 601 $status = 5;
602 break;
ec9f1c41 603 case 554: $message = 'Transaction failed';
3021b626 604 $status = 0;
605 break;
434666c7 606 default: $message = 'Unknown response: '. nl2br(htmlspecialchars($lines));
3021b626 607 $status = 0;
ec9f1c41 608 $error_num = '001';
3021b626 609 break;
610 }
611
612 if ($status == 0) {
0fc2aca0 613 include_once('../functions/page_header.php');
ec9f1c41 614 displayPageHeader($color, 'None');
0fc2aca0 615 include_once('../functions/display_messages.php');
f8a9394d 616 $lines = nl2br(htmlspecialchars($lines));
d5f34863 617 $msg = $message . "<br>\nServer replied: $lines";
618 plain_error_message($msg, $color);
3021b626 619 }
354eb441 620 if (! $verbose) return $status;
3021b626 621 return $err_num;
622 }
df15de21 623
020abcf3 624 function sendMessage($t, $c, $b, $subject, $body, $reply_id, $prio = 3) {
d2f4c914 625 global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad;
6441f7c6 626 global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort;
020abcf3 627 global $default_use_priority;
8fa48477 628 global $more_headers;
60994e13 629 $more_headers = Array();
d1b8b679 630
8fa48477 631 do_hook("smtp_send");
632
966286ae 633 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1);
60994e13 634
214e1343 635 if (isset($reply_id) && $reply_id) {
966286ae 636 sqimap_mailbox_select ($imap_stream, $mailbox);
ec9f1c41 637 sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, 'Answered');
60994e13 638
c95df380 639 // Insert In-Reply-To and References headers if the
640 // message-id of the message we reply to is set (longer than "<>")
641 // The References header should really be the old Referenced header
642 // with the message ID appended, but it can be only the message ID too.
643 $hdr = sqimap_get_small_header ($imap_stream, $reply_id, false);
644 if(strlen($hdr->message_id) > 2) {
ec9f1c41 645 $more_headers['In-Reply-To'] = $hdr->message_id;
646 $more_headers['References'] = $hdr->message_id;
c95df380 647 }
966286ae 648 }
020abcf3 649 if ($default_use_priority) {
650 $more_headers = array_merge($more_headers, createPriorityHeaders($prio));
651 }
8ae331c2 652
af8eba43 653 // In order to remove the problem of users not able to create
eedcdd97 654 // messages with "." on a blank line, RFC821 has made provision
655 // in section 4.5.2 (Transparency).
656 $body = ereg_replace("\n\\.", "\n..", $body);
657 $body = ereg_replace("^\\.", "..", $body);
af8eba43 658
8ae331c2 659 // this is to catch all plain \n instances and
eedcdd97 660 // replace them with \r\n. All newlines were converted
661 // into just \n inside the compose.php file.
8ae331c2 662 $body = ereg_replace("\n", "\r\n", $body);
3a8c414d 663
8ae331c2 664 if ($useSendmail) {
60994e13 665 $length = sendSendmail($t, $c, $b, $subject, $body, $more_headers);
df15de21 666 } else {
60994e13 667 $length = sendSMTP($t, $c, $b, $subject, $body, $more_headers);
df15de21 668 }
d1b8b679 669
2966f172 670 if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
671 sqimap_append ($imap_stream, $sent_folder, $length);
60994e13 672 write822Header ($imap_stream, $t, $c, $b, $subject, $more_headers);
8ae331c2 673 writeBody ($imap_stream, $body);
2966f172 674 sqimap_append_done ($imap_stream);
8ae331c2 675 }
676 sqimap_logout($imap_stream);
a7d75834 677 // Delete the files uploaded for attaching (if any).
f95d9e7b 678 // only if $length != 0 (if there was no error)
679 if ($length)
680 ClearAttachments();
681
682 return $length;
df15de21 683 }
020abcf3 684
685 function createPriorityHeaders($prio) {
686 $prio_headers = Array();
687 $prio_headers["X-Priority"] = $prio;
688
689 switch($prio) {
690 case 1: $prio_headers["Importance"] = "High";
691 $prio_headers["X-MSMail-Priority"] = "High";
692 break;
693
694 case 3: $prio_headers["Importance"] = "Normal";
695 $prio_headers["X-MSMail-Priority"] = "Normal";
696 break;
697
698 case 5:
699 $prio_headers["Importance"] = "Low";
700 $prio_headers["X-MSMail-Priority"] = "Low";
701 break;
702 }
703 return $prio_headers;
704 }
705?>