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