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