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