Fix IMAP PARSE reponse matching. This should get rid of retrival errors
[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 */
dd7766e0 35function isMultipart ($session) {
e25c2bd3 36 global $attachments;
7622a204 37
38 foreach ($attachments as $info) {
39 if ($info['session'] == $session) {
40 return true;
dd7766e0 41 }
a1e937bb 42 }
7622a204 43 return false;
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 */
68444892 58 $abook = addressbook_init(false, true);
e25c2bd3 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 */
68444892 92 $abook = addressbook_init(false, true);
e25c2bd3 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 */
dd7766e0 113function attachFiles ($fp, $session) {
e25c2bd3 114 global $attachments, $attachment_dir, $username;
77b88425 115
e25c2bd3 116 $length = 0;
77b88425 117
e25c2bd3 118 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
dd7766e0 119 if (isMultipart($session)) {
e25c2bd3 120 foreach ($attachments as $info) {
dd7766e0 121 if ($info['session'] == $session) {
a1e937bb 122 if (isset($info['type'])) {
e25c2bd3 123 $filetype = $info['type'];
a1e937bb 124 }
125 else {
e25c2bd3 126 $filetype = 'application/octet-stream';
a1e937bb 127 }
77b88425 128
129 $header = '--' . mimeBoundary() . "\r\n";
130 if ( isset($info['remotefilename']) && $info['remotefilename'] != '') {
131 $header .= "Content-Type: $filetype; name=\"" .
132 $info['remotefilename'] . "\"\r\n";
133 $header .= "Content-Disposition: attachment; filename=\"" .
134 $info['remotefilename'] . "\"\r\n";
135 } else {
136 $header .= "Content-Type: $filetype;\r\n";
137 }
138
139
140 /* Use 'rb' for NT systems -- read binary
141 * Unix doesn't care -- everything's binary! :-)
a1e937bb 142 */
77b88425 143
3392dc86 144 $filename = $hashed_attachment_dir . '/' . $info['localfilename'];
145 $file = fopen ($filename, 'rb');
e25c2bd3 146 if (substr($filetype, 0, 5) == 'text/' ||
604e2c03 147 substr($filetype, 0, 8) == 'message/' ) {
e25c2bd3 148 $header .= "\r\n";
149 fputs ($fp, $header);
150 $length += strlen($header);
151 while ($tmp = fgets($file, 4096)) {
152 $tmp = str_replace("\r\n", "\n", $tmp);
153 $tmp = str_replace("\r", "\n", $tmp);
154 $tmp = str_replace("\n", "\r\n", $tmp);
a1e937bb 155 if (feof($fp) && substr($tmp, -2) != "\r\n") {
e25c2bd3 156 $tmp .= "\r\n";
a1e937bb 157 }
e25c2bd3 158 fputs($fp, $tmp);
159 $length += strlen($tmp);
160 }
161 } else {
162 $header .= "Content-Transfer-Encoding: base64\r\n\r\n";
163 fputs ($fp, $header);
164 $length += strlen($header);
165 while ($tmp = fread($file, 570)) {
166 $encoded = chunk_split(base64_encode($tmp));
167 $length += strlen($encoded);
168 fputs ($fp, $encoded);
169 }
170 }
a7d75834 171 fclose ($file);
dd7766e0 172 }
e25c2bd3 173 }
174 }
175 return $length;
176}
177
a1e937bb 178/* Delete files that are uploaded for attaching
179 */
dd7766e0 180function deleteAttachments($session) {
181 global $username, $attachments, $attachment_dir;
e25c2bd3 182 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
dd7766e0 183
184 $rem_attachments = array();
185 foreach ($attachments as $info) {
186 if ($info['session'] == $session) {
187 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
188 if (file_exists($attached_file)) {
189 unlink($attached_file);
190 }
191 } else {
192 $rem_attachments[] = $info;
193 }
194 }
195 $attachments = $rem_attachments;
e25c2bd3 196}
a7d75834 197
a1e937bb 198/* Return a nice MIME-boundary
199 */
e25c2bd3 200function mimeBoundary () {
201 static $mimeBoundaryString;
77b88425 202
203 if ( !isset( $mimeBoundaryString ) ||
204 $mimeBoundaryString == '') {
205 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
206 mt_rand( 10000, 99999 );
e25c2bd3 207 }
77b88425 208
e25c2bd3 209 return $mimeBoundaryString;
210}
7b67334e 211
e25c2bd3 212/* Time offset for correct timezone */
213function timezone () {
214 global $invert_time;
215
216 $diff_second = date('Z');
a1e937bb 217 if ($invert_time) {
e25c2bd3 218 $diff_second = - $diff_second;
a1e937bb 219 }
220 if ($diff_second > 0) {
e25c2bd3 221 $sign = '+';
a1e937bb 222 }
223 else {
e25c2bd3 224 $sign = '-';
a1e937bb 225 }
226
e25c2bd3 227 $diff_second = abs($diff_second);
228
229 $diff_hour = floor ($diff_second / 3600);
230 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
231
232 $zonename = '('.strftime('%Z').')';
233 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename);
234 return ($result);
235}
236
237/* Print all the needed RFC822 headers */
dd7766e0 238function write822Header ($fp, $t, $c, $b, $subject, $more_headers, $session) {
e25c2bd3 239 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
240 global $data_dir, $username, $popuser, $domain, $version, $useSendmail;
241 global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
242 global $REMOTE_HOST, $identity;
243
a1e937bb 244 /* Storing the header to make sure the header is the same
245 * everytime the header is printed.
246 */
e25c2bd3 247 static $header, $headerlength;
248
249 if ($header == '') {
250 $to = expandAddrs(parseAddrs($t));
251 $cc = expandAddrs(parseAddrs($c));
252 $bcc = expandAddrs(parseAddrs($b));
253 if (isset($identity) && $identity != 'default') {
254 $reply_to = getPref($data_dir, $username, 'reply_to' . $identity);
255 $from = getPref($data_dir, $username, 'full_name' . $identity);
256 $from_addr = getPref($data_dir, $username, 'email_address' . $identity);
257 } else {
aaf9abef 258 $reply_to = getPref($data_dir, $username, 'reply_to');
259 $from = getPref($data_dir, $username, 'full_name');
260 $from_addr = getPref($data_dir, $username, 'email_address');
e25c2bd3 261 }
262
a1e937bb 263 if ($from_addr == '') {
ec9f1c41 264 $from_addr = $popuser.'@'.$domain;
a1e937bb 265 }
e25c2bd3 266
267 $to_list = getLineOfAddrs($to);
268 $cc_list = getLineOfAddrs($cc);
269 $bcc_list = getLineOfAddrs($bcc);
270
271 /* Encoding 8-bit characters and making from line */
272 $subject = encodeHeader($subject);
a1e937bb 273 if ($from == '') {
7b67334e 274 $from = "<$from_addr>";
a1e937bb 275 }
276 else {
ec9f1c41 277 $from = '"' . encodeHeader($from) . "\" <$from_addr>";
a1e937bb 278 }
e25c2bd3 279
280 /* This creates an RFC 822 date */
281 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
282
283 /* Create a message-id */
284 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
285 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
286
287 /* Make an RFC822 Received: line */
a1e937bb 288 if (isset($REMOTE_HOST)) {
8a2848f0 289 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
a1e937bb 290 }
291 else {
8a2848f0 292 $received_from = $REMOTE_ADDR;
a1e937bb 293 }
77b88425 294
e25c2bd3 295 if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) {
a1e937bb 296 if ($HTTP_X_FORWARDED_FOR == '') {
e25c2bd3 297 $HTTP_X_FORWARDED_FOR = 'unknown';
a1e937bb 298 }
9949206d 299 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
7e235a1a 300 }
301
fcb2237d 302 $header = "Received: from $received_from\r\n";
303 $header .= " (SquirrelMail authenticated user $username)\r\n";
304 $header .= " by $SERVER_NAME with HTTP;\r\n";
e25c2bd3 305 $header .= " $date\r\n";
7e235a1a 306
e25c2bd3 307 /* Insert the rest of the header fields */
308 $header .= "Message-ID: $message_id\r\n";
309 $header .= "Date: $date\r\n";
310 $header .= "Subject: $subject\r\n";
311 $header .= "From: $from\r\n";
312 $header .= "To: $to_list\r\n"; // Who it's TO
7e235a1a 313
604e2c03 314 if (isset($more_headers["Content-Type"])) {
7e235a1a 315 $contentType = $more_headers["Content-Type"];
316 unset($more_headers["Content-Type"]);
317 }
604e2c03 318 else {
dd7766e0 319 if (isMultipart($session)) {
7e235a1a 320 $contentType = "multipart/mixed;";
604e2c03 321 }
322 else {
7e235a1a 323 if ($default_charset != '') {
324 $contentType = 'text/plain; charset='.$default_charset;
325 }
326 else {
327 $contentType = 'text/plain;';
328 }
329 }
77b88425 330 }
7e235a1a 331
332 /* Insert headers from the $more_headers array */
333 if(is_array($more_headers)) {
e25c2bd3 334 reset($more_headers);
335 while(list($h_name, $h_val) = each($more_headers)) {
336 $header .= sprintf("%s: %s\r\n", $h_name, $h_val);
337 }
338 }
7e235a1a 339
e25c2bd3 340 if ($cc_list) {
7b67334e 341 $header .= "Cc: $cc_list\r\n"; // Who the CCs are
e25c2bd3 342 }
7e235a1a 343
a1e937bb 344 if ($reply_to != '') {
7b67334e 345 $header .= "Reply-To: $reply_to\r\n";
a1e937bb 346 }
7e235a1a 347
e25c2bd3 348 if ($useSendmail) {
7b67334e 349 if ($bcc_list) {
e25c2bd3 350 // BCCs is removed from header by sendmail
7e235a1a 351 $header .= "Bcc: $bcc_list\r\n";
7b67334e 352 }
e25c2bd3 353 }
7e235a1a 354
a1e937bb 355 $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; /* Identify SquirrelMail */
77b88425 356
a1e937bb 357 /* Do the MIME-stuff */
e25c2bd3 358 $header .= "MIME-Version: 1.0\r\n";
7e235a1a 359
dd7766e0 360 if (isMultipart($session)) {
604e2c03 361 $header .= 'Content-Type: '.$contentType.' boundary="';
362 $header .= mimeBoundary();
363 $header .= "\"\r\n";
e25c2bd3 364 } else {
7e235a1a 365 $header .= 'Content-Type: '.$contentType."\r\n";
7b67334e 366 $header .= "Content-Transfer-Encoding: 8bit\r\n";
e25c2bd3 367 }
368 $header .= "\r\n"; // One blank line to separate header and body
369
370 $headerlength = strlen($header);
371 }
372
a1e937bb 373 /* Write the header */
e25c2bd3 374 fputs ($fp, $header);
375
376 return $headerlength;
377}
17ce8467 378
a1e937bb 379/* Send the body
380 */
dd7766e0 381function writeBody ($fp, $passedBody, $session) {
e25c2bd3 382 global $default_charset;
383
384 $attachmentlength = 0;
385
dd7766e0 386 if (isMultipart($session)) {
e25c2bd3 387 $body = '--'.mimeBoundary()."\r\n";
388
a1e937bb 389 if ($default_charset != "") {
17ce8467 390 $body .= "Content-Type: text/plain; charset=$default_charset\r\n";
a1e937bb 391 }
392 else {
17ce8467 393 $body .= "Content-Type: text/plain\r\n";
a1e937bb 394 }
e25c2bd3 395
396 $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
397 $body .= $passedBody . "\r\n\r\n";
398 fputs ($fp, $body);
399
dd7766e0 400 $attachmentlength = attachFiles($fp, $session);
e25c2bd3 401
a1e937bb 402 if (!isset($postbody)) {
403 $postbody = "";
404 }
e25c2bd3 405 $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n";
406 fputs ($fp, $postbody);
407 } else {
408 $body = $passedBody . "\r\n";
409 fputs ($fp, $body);
410 $postbody = "\r\n";
411 fputs ($fp, $postbody);
412 }
7e235a1a 413
e25c2bd3 414 return (strlen($body) + strlen($postbody) + $attachmentlength);
415}
17ce8467 416
a1e937bb 417/* Send mail using the sendmail command
418 */
dd7766e0 419function sendSendmail($t, $c, $b, $subject, $body, $more_headers, $session) {
e25c2bd3 420 global $sendmail_path, $popuser, $username, $domain;
421
a1e937bb 422 /* Build envelope sender address. Make sure it doesn't contain
423 * spaces or other "weird" chars that would allow a user to
424 * exploit the shell/pipe it is used in.
425 */
e25c2bd3 426 $envelopefrom = "$popuser@$domain";
427 $envelopefrom = ereg_replace("[[:blank:]]",'', $envelopefrom);
428 $envelopefrom = ereg_replace("[[:space:]]",'', $envelopefrom);
429 $envelopefrom = ereg_replace("[[:cntrl:]]",'', $envelopefrom);
430
a1e937bb 431 /* open pipe to sendmail or qmail-inject (qmail-inject doesn't accept -t param) */
e25c2bd3 432 if (strstr($sendmail_path, "qmail-inject")) {
433 $fp = popen (escapeshellcmd("$sendmail_path -f$envelopefrom"), "w");
434 } else {
435 $fp = popen (escapeshellcmd("$sendmail_path -t -f$envelopefrom"), "w");
436 }
437
dd7766e0 438 $headerlength = write822Header ($fp, $t, $c, $b, $subject, $more_headers, $session);
439 $bodylength = writeBody($fp, $body, $session);
e25c2bd3 440
441 pclose($fp);
442
443 return ($headerlength + $bodylength);
444}
445
446function smtpReadData($smtpConnection) {
447 $read = fgets($smtpConnection, 1024);
448 $counter = 0;
449 while ($read) {
450 echo $read . '<BR>';
451 $data[$counter] = $read;
452 $read = fgets($smtpConnection, 1024);
453 $counter++;
454 }
455}
456
dd7766e0 457function sendSMTP($t, $c, $b, $subject, $body, $more_headers, $session) {
e25c2bd3 458 global $username, $popuser, $domain, $version, $smtpServerAddress,
459 $smtpPort, $data_dir, $color, $use_authenticated_smtp, $identity,
460 $key, $onetimepad;
461
462 $to = expandRcptAddrs(parseAddrs($t));
463 $cc = expandRcptAddrs(parseAddrs($c));
464 $bcc = expandRcptAddrs(parseAddrs($b));
a1e937bb 465 if (isset($identity) && $identity != 'default') {
e25c2bd3 466 $from_addr = getPref($data_dir, $username, 'email_address' . $identity);
a1e937bb 467 }
468 else {
e25c2bd3 469 $from_addr = getPref($data_dir, $username, 'email_address');
a1e937bb 470 }
e25c2bd3 471
a1e937bb 472 if (!$from_addr) {
e25c2bd3 473 $from_addr = "$popuser@$domain";
a1e937bb 474 }
e25c2bd3 475
476 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
477 if (!$smtpConnection) {
478 echo 'Error connecting to SMTP Server.<br>';
479 echo "$errorNumber : $errorString<br>";
480 exit;
481 }
482 $tmp = fgets($smtpConnection, 1024);
14c62c12 483 if (errorCheck($tmp, $smtpConnection)!=5) {
484 return(0);
485 }
e25c2bd3 486
487 $to_list = getLineOfAddrs($to);
488 $cc_list = getLineOfAddrs($cc);
489
a1e937bb 490 /* Lets introduce ourselves */
e25c2bd3 491 if (! isset ($use_authenticated_smtp) || $use_authenticated_smtp == false) {
492 fputs($smtpConnection, "HELO $domain\r\n");
493 $tmp = fgets($smtpConnection, 1024);
494 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
495 } else {
496 fputs($smtpConnection, "EHLO $domain\r\n");
497 $tmp = fgets($smtpConnection, 1024);
498 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
499
500 fputs($smtpConnection, "AUTH LOGIN\r\n");
501 $tmp = fgets($smtpConnection, 1024);
a1e937bb 502 if (errorCheck($tmp, $smtpConnection)!=5) {
503 return(0);
504 }
77b88425 505
e25c2bd3 506 fputs($smtpConnection, base64_encode ($username) . "\r\n");
507 $tmp = fgets($smtpConnection, 1024);
a1e937bb 508 if (errorCheck($tmp, $smtpConnection)!=5) {
509 return(0);
510 }
e25c2bd3 511
512 fputs($smtpConnection, base64_encode (OneTimePadDecrypt($key, $onetimepad)) . "\r\n");
513 $tmp = fgets($smtpConnection, 1024);
a1e937bb 514 if (errorCheck($tmp, $smtpConnection)!=5) {
515 return(0);
516 }
e25c2bd3 517 }
518
a1e937bb 519 /* Ok, who is sending the message? */
e25c2bd3 520 fputs($smtpConnection, "MAIL FROM: <$from_addr>\r\n");
521 $tmp = fgets($smtpConnection, 1024);
a1e937bb 522 if (errorCheck($tmp, $smtpConnection)!=5) {
523 return(0);
524 }
e25c2bd3 525
a1e937bb 526 /* send who the recipients are */
e25c2bd3 527 for ($i = 0; $i < count($to); $i++) {
528 fputs($smtpConnection, "RCPT TO: $to[$i]\r\n");
529 $tmp = fgets($smtpConnection, 1024);
a1e937bb 530 if (errorCheck($tmp, $smtpConnection)!=5) {
531 return(0);
532 }
e25c2bd3 533 }
534 for ($i = 0; $i < count($cc); $i++) {
535 fputs($smtpConnection, "RCPT TO: $cc[$i]\r\n");
536 $tmp = fgets($smtpConnection, 1024);
a1e937bb 537 if (errorCheck($tmp, $smtpConnection)!=5) {
538 return(0);
539 }
e25c2bd3 540 }
541 for ($i = 0; $i < count($bcc); $i++) {
542 fputs($smtpConnection, "RCPT TO: $bcc[$i]\r\n");
543 $tmp = fgets($smtpConnection, 1024);
77b88425 544 if (errorCheck($tmp, $smtpConnection)!=5) {
a1e937bb 545 return(0);
546 }
e25c2bd3 547 }
77b88425 548
a1e937bb 549 /* Lets start sending the actual message */
e25c2bd3 550 fputs($smtpConnection, "DATA\r\n");
551 $tmp = fgets($smtpConnection, 1024);
a1e937bb 552 if (errorCheck($tmp, $smtpConnection)!=5) {
553 return(0);
554 }
77b88425 555
a1e937bb 556 /* Send the message */
dd7766e0 557 $headerlength = write822Header ($smtpConnection, $t, $c, $b, $subject, $more_headers, $session);
558 $bodylength = writeBody($smtpConnection, $body, $session);
e25c2bd3 559
a1e937bb 560 fputs($smtpConnection, ".\r\n"); /* end the DATA part */
e25c2bd3 561 $tmp = fgets($smtpConnection, 1024);
562 $num = errorCheck($tmp, $smtpConnection, true);
563 if ($num != 250) {
e25c2bd3 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) {
8f8f3a4e 576 global $color, $compose_new_win;
e25c2bd3 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 }
7e235a1a 665
e25c2bd3 666 if ($status == 0) {
667 include_once('../functions/page_header.php');
8f8f3a4e 668 if ($compose_new_win == '1') {
669 compose_Header($color, 'None');
670 }
7e235a1a 671 else {
8f8f3a4e 672 displayPageHeader($color, 'None');
673 }
e25c2bd3 674 include_once('../functions/display_messages.php');
675 $lines = nl2br(htmlspecialchars($lines));
676 $msg = $message . "<br>\nServer replied: $lines";
677 plain_error_message($msg, $color);
678 }
679 if (! $verbose) return $status;
680 return $err_num;
681}
682
dd7766e0 683function sendMessage($t, $c, $b, $subject, $body, $reply_id, $MDN, $prio = 3, $session) {
77b88425 684 global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad,
604e2c03 685 $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress,
686 $imapPort, $default_use_priority, $more_headers, $request_mdn, $request_dr;
77b88425 687
e25c2bd3 688 $more_headers = Array();
689
77b88425 690 do_hook('smtp_send');
691
e25c2bd3 692 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1);
77b88425 693
e25c2bd3 694 if (isset($reply_id) && $reply_id) {
695 sqimap_mailbox_select ($imap_stream, $mailbox);
696 sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, 'Answered');
77b88425 697
698 /* Insert In-Reply-To and References headers if the
a1e937bb 699 * message-id of the message we reply to is set (longer than "<>")
700 * The References header should really be the old Referenced header
701 * with the message ID appended, but it can be only the message ID too.
702 */
e25c2bd3 703 $hdr = sqimap_get_small_header ($imap_stream, $reply_id, false);
704 if(strlen($hdr->message_id) > 2) {
ec9f1c41 705 $more_headers['In-Reply-To'] = $hdr->message_id;
706 $more_headers['References'] = $hdr->message_id;
e25c2bd3 707 }
708 }
709 if ($default_use_priority) {
710 $more_headers = array_merge($more_headers, createPriorityHeaders($prio));
711 }
77b88425 712
604e2c03 713 $requestRecipt = 0;
714 if (isset($request_dr)) {
715 $requestRecipt += 1;
716 }
717 if (isset($request_mdn)) {
718 $requestRecipt += 2;
719 }
720 if ( $requestRecipt > 0) {
721 $more_headers = array_merge($more_headers, createReceiptHeaders($requestRecipt));
722 }
723
a1e937bb 724 /* In order to remove the problem of users not able to create
725 * messages with "." on a blank line, RFC821 has made provision
726 * in section 4.5.2 (Transparency).
727 */
e25c2bd3 728 $body = ereg_replace("\n\\.", "\n..", $body);
729 $body = ereg_replace("^\\.", "..", $body);
77b88425 730
a1e937bb 731 /* this is to catch all plain \n instances and
732 * replace them with \r\n. All newlines were converted
733 * into just \n inside the compose.php file.
734 */
e25c2bd3 735 $body = ereg_replace("\n", "\r\n", $body);
604e2c03 736
737 if ($MDN) {
738 $more_headers["Content-Type"] = "multipart/report; ".
739 "report-type=disposition-notification;";
740 }
77b88425 741
e25c2bd3 742 if ($useSendmail) {
dd7766e0 743 $length = sendSendmail($t, $c, $b, $subject, $body, $more_headers, $session);
e25c2bd3 744 } else {
dd7766e0 745 $length = sendSMTP($t, $c, $b, $subject, $body, $more_headers, $session);
e25c2bd3 746 }
e25c2bd3 747 if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
748 sqimap_append ($imap_stream, $sent_folder, $length);
dd7766e0 749 write822Header ($imap_stream, $t, $c, $b, $subject, $more_headers, $session);
750 writeBody ($imap_stream, $body, $session);
e25c2bd3 751 sqimap_append_done ($imap_stream);
752 }
753 sqimap_logout($imap_stream);
a1e937bb 754 /* Delete the files uploaded for attaching (if any).
755 * only if $length != 0 (if there was no error)
756 */
77b88425 757 if ($length) {
dd7766e0 758 ClearAttachments($session);
77b88425 759 }
760
e25c2bd3 761 return $length;
762}
d1b8b679 763
e25c2bd3 764function createPriorityHeaders($prio) {
765 $prio_headers = Array();
77b88425 766 $prio_headers['X-Priority'] = $prio;
767
e25c2bd3 768 switch($prio) {
77b88425 769 case 1: $prio_headers['Importance'] = 'High';
770 $prio_headers['X-MSMail-Priority'] = 'High';
e25c2bd3 771 break;
77b88425 772
773 case 3: $prio_headers['Importance'] = 'Normal';
774 $prio_headers['X-MSMail-Priority'] = 'Normal';
e25c2bd3 775 break;
77b88425 776
e25c2bd3 777 case 5:
77b88425 778 $prio_headers['Importance'] = 'Low';
779 $prio_headers['X-MSMail-Priority'] = 'Low';
e25c2bd3 780 break;
781 }
782 return $prio_headers;
783}
020abcf3 784
604e2c03 785function createReceiptHeaders($receipt) {
0804c276 786
787 GLOBAL $data_dir, $username;
788
789 $receipt_headers = Array();
790 $from_addr = getPref($data_dir, $username, 'email_address');
791 $from = getPref($data_dir, $username, 'full_name');
792
793 if ($from == '') {
794 $from = "<$from_addr>";
795 }
796 else {
797 $from = '"' . encodeHeader($from) . "\" <$from_addr>";
798 }
799
800 /* On Delivery */
801 if ( $receipt == 1
802 || $receipt == 3 ) {
803 $receipt_headers["Return-Receipt-To"] = $from;
804 }
805 /* On Read */
806 if ($receipt == 2
807 || $receipt == 3 ) {
808 /* Pegasus Mail */
809 $receipt_headers["X-Confirm-Reading-To"] = $from;
810 /* RFC 2298 */
811 $receipt_headers["Disposition-Notification-To"] = $from;
812 }
813 return $receipt_headers;
814}
604e2c03 815
816
020abcf3 817?>