$allow_charset_search var needs a default value, otherwise config.php gets a parse...
[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) {
a3fbf5a4 39 if ($info['session'] == $session) {
40 return true;
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 */
721a6cac 113function attachFiles ($fp, $session, $rn="\r\n") {
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) {
a3fbf5a4 121 if ($info['session'] == $session) {
122 if (isset($info['type'])) {
123 $filetype = $info['type'];
124 }
125 else {
126 $filetype = 'application/octet-stream';
127 }
128
129 $header = '--' . mimeBoundary() . "$rn";
130 if ( isset($info['remotefilename'])
131 && $info['remotefilename'] != '') {
132 $header .= "Content-Type: $filetype; name=\"" .
133 $info['remotefilename'] . "\"$rn";
134 $header .= "Content-Disposition: attachment; filename=\""
135 . $info['remotefilename'] . "\"$rn";
136 } else {
30e08236 137 $header .= "Content-Type: $filetype$rn";
a3fbf5a4 138 }
77b88425 139
a3fbf5a4 140
141 /* Use 'rb' for NT systems -- read binary
142 * Unix doesn't care -- everything's binary! :-)
143 */
144
145 $filename = $hashed_attachment_dir . '/'
146 . $info['localfilename'];
147 $file = fopen ($filename, 'rb');
148 if (substr($filetype, 0, 5) == 'text/' ||
149 substr($filetype, 0, 8) == 'message/' ) {
150 $header .= "$rn";
151 fputs ($fp, $header);
152 $length += strlen($header);
153 while ($tmp = fgets($file, 4096)) {
154 $tmp = str_replace("\r\n", "\n", $tmp);
155 $tmp = str_replace("\r", "\n", $tmp);
156 if ($rn == "\r\n"){
157 $tmp = str_replace("\n", "\r\n", $tmp);
158 }
159 /**
160 * Check if the last line has newline ($rn) in it
161 * and append if it doesn't.
162 */
163 if (feof($fp) && !strstr($tmp, "$rn")){
164 $tmp .= "$rn";
165 }
166 fputs($fp, $tmp);
167 $length += strlen($tmp);
168 }
169 } else {
170 $header .= "Content-Transfer-Encoding: base64"
171 . "$rn" . "$rn";
172 fputs ($fp, $header);
173 $length += strlen($header);
174 while ($tmp = fread($file, 570)) {
175 $encoded = chunk_split(base64_encode($tmp));
176 $length += strlen($encoded);
177 fputs ($fp, $encoded);
a1e937bb 178 }
e25c2bd3 179 }
a3fbf5a4 180 fclose ($file);
e25c2bd3 181 }
e25c2bd3 182 }
183 }
184 return $length;
185}
186
a1e937bb 187/* Delete files that are uploaded for attaching
188 */
dd7766e0 189function deleteAttachments($session) {
190 global $username, $attachments, $attachment_dir;
e25c2bd3 191 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
dd7766e0 192
193 $rem_attachments = array();
194 foreach ($attachments as $info) {
a3fbf5a4 195 if ($info['session'] == $session) {
dd7766e0 196 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
197 if (file_exists($attached_file)) {
a3fbf5a4 198 unlink($attached_file);
dd7766e0 199 }
a3fbf5a4 200 } else {
201 $rem_attachments[] = $info;
202 }
dd7766e0 203 }
204 $attachments = $rem_attachments;
e25c2bd3 205}
a7d75834 206
a1e937bb 207/* Return a nice MIME-boundary
208 */
e25c2bd3 209function mimeBoundary () {
210 static $mimeBoundaryString;
77b88425 211
212 if ( !isset( $mimeBoundaryString ) ||
213 $mimeBoundaryString == '') {
214 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
a3fbf5a4 215 mt_rand( 10000, 99999 );
e25c2bd3 216 }
77b88425 217
e25c2bd3 218 return $mimeBoundaryString;
219}
7b67334e 220
e25c2bd3 221/* Time offset for correct timezone */
222function timezone () {
223 global $invert_time;
224
225 $diff_second = date('Z');
a1e937bb 226 if ($invert_time) {
e25c2bd3 227 $diff_second = - $diff_second;
a1e937bb 228 }
229 if ($diff_second > 0) {
e25c2bd3 230 $sign = '+';
a1e937bb 231 }
232 else {
e25c2bd3 233 $sign = '-';
a1e937bb 234 }
235
e25c2bd3 236 $diff_second = abs($diff_second);
237
238 $diff_hour = floor ($diff_second / 3600);
239 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
240
241 $zonename = '('.strftime('%Z').')';
a3fbf5a4 242 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute,
243 $zonename);
e25c2bd3 244 return ($result);
245}
246
247/* Print all the needed RFC822 headers */
721a6cac 248function write822Header ($fp, $t, $c, $b, $subject, $more_headers, $session, $rn="\r\n") {
e25c2bd3 249 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
250 global $data_dir, $username, $popuser, $domain, $version, $useSendmail;
251 global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
252 global $REMOTE_HOST, $identity;
721a6cac 253
a1e937bb 254 /* Storing the header to make sure the header is the same
255 * everytime the header is printed.
256 */
e25c2bd3 257 static $header, $headerlength;
258
259 if ($header == '') {
260 $to = expandAddrs(parseAddrs($t));
261 $cc = expandAddrs(parseAddrs($c));
262 $bcc = expandAddrs(parseAddrs($b));
263 if (isset($identity) && $identity != 'default') {
264 $reply_to = getPref($data_dir, $username, 'reply_to' . $identity);
265 $from = getPref($data_dir, $username, 'full_name' . $identity);
a3fbf5a4 266 $from_addr = getPref($data_dir, $username,
267 'email_address' . $identity);
e25c2bd3 268 } else {
aaf9abef 269 $reply_to = getPref($data_dir, $username, 'reply_to');
270 $from = getPref($data_dir, $username, 'full_name');
271 $from_addr = getPref($data_dir, $username, 'email_address');
e25c2bd3 272 }
273
a1e937bb 274 if ($from_addr == '') {
ec9f1c41 275 $from_addr = $popuser.'@'.$domain;
a1e937bb 276 }
e25c2bd3 277
278 $to_list = getLineOfAddrs($to);
279 $cc_list = getLineOfAddrs($cc);
280 $bcc_list = getLineOfAddrs($bcc);
281
282 /* Encoding 8-bit characters and making from line */
283 $subject = encodeHeader($subject);
a1e937bb 284 if ($from == '') {
7b67334e 285 $from = "<$from_addr>";
a1e937bb 286 }
287 else {
ec9f1c41 288 $from = '"' . encodeHeader($from) . "\" <$from_addr>";
a1e937bb 289 }
e25c2bd3 290
291 /* This creates an RFC 822 date */
292 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
293
294 /* Create a message-id */
295 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
296 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
297
298 /* Make an RFC822 Received: line */
a1e937bb 299 if (isset($REMOTE_HOST)) {
8a2848f0 300 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
a1e937bb 301 }
302 else {
8a2848f0 303 $received_from = $REMOTE_ADDR;
a1e937bb 304 }
77b88425 305
e25c2bd3 306 if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) {
a1e937bb 307 if ($HTTP_X_FORWARDED_FOR == '') {
e25c2bd3 308 $HTTP_X_FORWARDED_FOR = 'unknown';
a1e937bb 309 }
9949206d 310 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
7e235a1a 311 }
312
a3fbf5a4 313 $header = "Received: from $received_from" . $rn;
314 $header .= " (SquirrelMail authenticated user $username)" . $rn;
315 $header .= " by $SERVER_NAME with HTTP;" . $rn;
316 $header .= " $date" . $rn;
7e235a1a 317
e25c2bd3 318 /* Insert the rest of the header fields */
a3fbf5a4 319 $header .= "Message-ID: $message_id" . $rn;
320 $header .= "Date: $date" . $rn;
321 $header .= "Subject: $subject" . $rn;
322 $header .= "From: $from" . $rn;
323 $header .= "To: $to_list" . $rn; // Who it's TO
7e235a1a 324
604e2c03 325 if (isset($more_headers["Content-Type"])) {
7e235a1a 326 $contentType = $more_headers["Content-Type"];
327 unset($more_headers["Content-Type"]);
328 }
604e2c03 329 else {
dd7766e0 330 if (isMultipart($session)) {
7e235a1a 331 $contentType = "multipart/mixed;";
604e2c03 332 }
333 else {
7e235a1a 334 if ($default_charset != '') {
335 $contentType = 'text/plain; charset='.$default_charset;
336 }
337 else {
338 $contentType = 'text/plain;';
339 }
340 }
77b88425 341 }
7e235a1a 342
343 /* Insert headers from the $more_headers array */
344 if(is_array($more_headers)) {
e25c2bd3 345 reset($more_headers);
346 while(list($h_name, $h_val) = each($more_headers)) {
721a6cac 347 if ($h_name == 'References') {
348 $h_val = str_replace(' ', "$rn ", $h_val);
349 }
a3fbf5a4 350 $header .= sprintf("%s: %s%s", $h_name, $h_val, $rn);
e25c2bd3 351 }
352 }
7e235a1a 353
e25c2bd3 354 if ($cc_list) {
a3fbf5a4 355 $header .= "Cc: $cc_list" . $rn; // Who the CCs are
e25c2bd3 356 }
7e235a1a 357
a1e937bb 358 if ($reply_to != '') {
a3fbf5a4 359 $header .= "Reply-To: $reply_to" . $rn;
a1e937bb 360 }
7e235a1a 361
e25c2bd3 362 if ($useSendmail) {
7b67334e 363 if ($bcc_list) {
e25c2bd3 364 // BCCs is removed from header by sendmail
a3fbf5a4 365 $header .= "Bcc: $bcc_list" . $rn;
7b67334e 366 }
e25c2bd3 367 }
7e235a1a 368
a3fbf5a4 369 /* Identify SquirrelMail */
370 $header .= "X-Mailer: SquirrelMail (version $version)" . $rn;
77b88425 371
a1e937bb 372 /* Do the MIME-stuff */
a3fbf5a4 373 $header .= "MIME-Version: 1.0" . $rn;
7e235a1a 374
dd7766e0 375 if (isMultipart($session)) {
604e2c03 376 $header .= 'Content-Type: '.$contentType.' boundary="';
377 $header .= mimeBoundary();
a3fbf5a4 378 $header .= "\"$rn";
e25c2bd3 379 } else {
a3fbf5a4 380 $header .= 'Content-Type: ' . $contentType . $rn;
381 $header .= "Content-Transfer-Encoding: 8bit" . $rn;
e25c2bd3 382 }
a3fbf5a4 383 $header .= $rn; // One blank line to separate header and body
e25c2bd3 384
385 $headerlength = strlen($header);
386 }
387
a1e937bb 388 /* Write the header */
e25c2bd3 389 fputs ($fp, $header);
390
391 return $headerlength;
392}
17ce8467 393
a1e937bb 394/* Send the body
395 */
721a6cac 396function writeBody ($fp, $passedBody, $session, $rn="\r\n") {
e25c2bd3 397 global $default_charset;
a3fbf5a4 398
e25c2bd3 399 $attachmentlength = 0;
400
dd7766e0 401 if (isMultipart($session)) {
a3fbf5a4 402 $body = '--'.mimeBoundary() . $rn;
e25c2bd3 403
a1e937bb 404 if ($default_charset != "") {
a3fbf5a4 405 $body .= "Content-Type: text/plain; charset=$default_charset".$rn;
a1e937bb 406 }
407 else {
a3fbf5a4 408 $body .= "Content-Type: text/plain" . $rn;
a1e937bb 409 }
e25c2bd3 410
a3fbf5a4 411 $body .= "Content-Transfer-Encoding: 8bit" . $rn . $rn;
412 $body .= $passedBody . $rn . $rn;
e25c2bd3 413 fputs ($fp, $body);
414
721a6cac 415 $attachmentlength = attachFiles($fp, $session, $rn);
e25c2bd3 416
a1e937bb 417 if (!isset($postbody)) {
418 $postbody = "";
419 }
a3fbf5a4 420 $postbody .= $rn . "--" . mimeBoundary() . "--" . $rn . $rn;
e25c2bd3 421 fputs ($fp, $postbody);
422 } else {
a3fbf5a4 423 $body = $passedBody . $rn;
e25c2bd3 424 fputs ($fp, $body);
a3fbf5a4 425 $postbody = $rn;
e25c2bd3 426 fputs ($fp, $postbody);
427 }
7e235a1a 428
e25c2bd3 429 return (strlen($body) + strlen($postbody) + $attachmentlength);
430}
17ce8467 431
a1e937bb 432/* Send mail using the sendmail command
433 */
dd7766e0 434function sendSendmail($t, $c, $b, $subject, $body, $more_headers, $session) {
e25c2bd3 435 global $sendmail_path, $popuser, $username, $domain;
436
a1e937bb 437 /* Build envelope sender address. Make sure it doesn't contain
438 * spaces or other "weird" chars that would allow a user to
439 * exploit the shell/pipe it is used in.
440 */
e25c2bd3 441 $envelopefrom = "$popuser@$domain";
442 $envelopefrom = ereg_replace("[[:blank:]]",'', $envelopefrom);
443 $envelopefrom = ereg_replace("[[:space:]]",'', $envelopefrom);
444 $envelopefrom = ereg_replace("[[:cntrl:]]",'', $envelopefrom);
445
a3fbf5a4 446 /**
447 * open pipe to sendmail or qmail-inject
448 * (qmail-inject doesn't accept -t param)
449 */
e25c2bd3 450 if (strstr($sendmail_path, "qmail-inject")) {
451 $fp = popen (escapeshellcmd("$sendmail_path -f$envelopefrom"), "w");
452 } else {
453 $fp = popen (escapeshellcmd("$sendmail_path -t -f$envelopefrom"), "w");
454 }
455
a3fbf5a4 456 $headerlength = write822Header ($fp, $t, $c, $b, $subject,
721a6cac 457 $more_headers, $session, "\n");
458 $bodylength = writeBody($fp, $body, $session, "\n");
e25c2bd3 459
460 pclose($fp);
461
462 return ($headerlength + $bodylength);
463}
464
465function smtpReadData($smtpConnection) {
466 $read = fgets($smtpConnection, 1024);
467 $counter = 0;
468 while ($read) {
469 echo $read . '<BR>';
470 $data[$counter] = $read;
471 $read = fgets($smtpConnection, 1024);
472 $counter++;
473 }
474}
475
dd7766e0 476function sendSMTP($t, $c, $b, $subject, $body, $more_headers, $session) {
e25c2bd3 477 global $username, $popuser, $domain, $version, $smtpServerAddress,
478 $smtpPort, $data_dir, $color, $use_authenticated_smtp, $identity,
479 $key, $onetimepad;
480
481 $to = expandRcptAddrs(parseAddrs($t));
482 $cc = expandRcptAddrs(parseAddrs($c));
483 $bcc = expandRcptAddrs(parseAddrs($b));
a1e937bb 484 if (isset($identity) && $identity != 'default') {
a3fbf5a4 485 $from_addr = getPref($data_dir, $username,
486 'email_address' . $identity);
a1e937bb 487 }
488 else {
e25c2bd3 489 $from_addr = getPref($data_dir, $username, 'email_address');
a1e937bb 490 }
e25c2bd3 491
a1e937bb 492 if (!$from_addr) {
e25c2bd3 493 $from_addr = "$popuser@$domain";
a1e937bb 494 }
2044f95a 495
496 /* POP3 BEFORE SMTP CODE HERE */
497 global $pop_before_smtp;
498 if (isset($pop_before_smtp) && $pop_before_smtp === true) {
499 if (!isset($pop_port)) {
500 $pop_port = 110;
501 }
502 if (!isset($pop_server)) {
503 $pop_server = $smtpServerAddress; /* usually the same host! */
504 }
505 $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str);
506 if (!$popConnection) {
507 error_log("Error connecting to POP Server ($pop_server:$pop_port)"
508 . " $err_no : $err_str");
509 } else {
510 $tmp = fgets($popConnection, 1024); /* banner */
511 if (!eregi("^\+OK", $tmp, $regs)) {
512 return(0);
513 }
514 fputs($popConnection, "USER $username\r\n");
515 $tmp = fgets($popConnection, 1024);
516 if (!eregi("^\+OK", $tmp, $regs)) {
517 return(0);
518 }
519 fputs($popConnection, 'PASS ' . OneTimePadDecrypt($key, $onetimepad) . "\r\n");
520 $tmp = fgets($popConnection, 1024);
521 if (!eregi("^\+OK", $tmp, $regs)) {
522 return(0);
523 }
524 fputs($popConnection, "QUIT\r\n"); /* log off */
525 fclose($popConnection);
526 }
527 }
e25c2bd3 528
a3fbf5a4 529 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort,
530 $errorNumber, $errorString);
e25c2bd3 531 if (!$smtpConnection) {
532 echo 'Error connecting to SMTP Server.<br>';
533 echo "$errorNumber : $errorString<br>";
534 exit;
535 }
536 $tmp = fgets($smtpConnection, 1024);
14c62c12 537 if (errorCheck($tmp, $smtpConnection)!=5) {
538 return(0);
539 }
e25c2bd3 540
541 $to_list = getLineOfAddrs($to);
542 $cc_list = getLineOfAddrs($cc);
543
a1e937bb 544 /* Lets introduce ourselves */
a3fbf5a4 545 if (! isset ($use_authenticated_smtp)
546 || $use_authenticated_smtp == false) {
e25c2bd3 547 fputs($smtpConnection, "HELO $domain\r\n");
548 $tmp = fgets($smtpConnection, 1024);
549 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
550 } else {
551 fputs($smtpConnection, "EHLO $domain\r\n");
552 $tmp = fgets($smtpConnection, 1024);
553 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
554
555 fputs($smtpConnection, "AUTH LOGIN\r\n");
556 $tmp = fgets($smtpConnection, 1024);
a1e937bb 557 if (errorCheck($tmp, $smtpConnection)!=5) {
558 return(0);
559 }
77b88425 560
e25c2bd3 561 fputs($smtpConnection, base64_encode ($username) . "\r\n");
562 $tmp = fgets($smtpConnection, 1024);
a1e937bb 563 if (errorCheck($tmp, $smtpConnection)!=5) {
564 return(0);
565 }
e25c2bd3 566
a3fbf5a4 567 fputs($smtpConnection, base64_encode
568 (OneTimePadDecrypt($key, $onetimepad)) . "\r\n");
e25c2bd3 569 $tmp = fgets($smtpConnection, 1024);
a1e937bb 570 if (errorCheck($tmp, $smtpConnection)!=5) {
571 return(0);
572 }
e25c2bd3 573 }
574
a1e937bb 575 /* Ok, who is sending the message? */
e25c2bd3 576 fputs($smtpConnection, "MAIL FROM: <$from_addr>\r\n");
577 $tmp = fgets($smtpConnection, 1024);
a1e937bb 578 if (errorCheck($tmp, $smtpConnection)!=5) {
579 return(0);
580 }
e25c2bd3 581
a1e937bb 582 /* send who the recipients are */
e25c2bd3 583 for ($i = 0; $i < count($to); $i++) {
584 fputs($smtpConnection, "RCPT TO: $to[$i]\r\n");
585 $tmp = fgets($smtpConnection, 1024);
a1e937bb 586 if (errorCheck($tmp, $smtpConnection)!=5) {
587 return(0);
588 }
e25c2bd3 589 }
590 for ($i = 0; $i < count($cc); $i++) {
591 fputs($smtpConnection, "RCPT TO: $cc[$i]\r\n");
592 $tmp = fgets($smtpConnection, 1024);
a1e937bb 593 if (errorCheck($tmp, $smtpConnection)!=5) {
594 return(0);
595 }
e25c2bd3 596 }
597 for ($i = 0; $i < count($bcc); $i++) {
598 fputs($smtpConnection, "RCPT TO: $bcc[$i]\r\n");
599 $tmp = fgets($smtpConnection, 1024);
77b88425 600 if (errorCheck($tmp, $smtpConnection)!=5) {
a1e937bb 601 return(0);
602 }
e25c2bd3 603 }
77b88425 604
a1e937bb 605 /* Lets start sending the actual message */
e25c2bd3 606 fputs($smtpConnection, "DATA\r\n");
607 $tmp = fgets($smtpConnection, 1024);
a1e937bb 608 if (errorCheck($tmp, $smtpConnection)!=5) {
609 return(0);
610 }
77b88425 611
a1e937bb 612 /* Send the message */
a3fbf5a4 613 $headerlength = write822Header ($smtpConnection, $t, $c, $b,
614 $subject, $more_headers, $session);
dd7766e0 615 $bodylength = writeBody($smtpConnection, $body, $session);
e25c2bd3 616
a1e937bb 617 fputs($smtpConnection, ".\r\n"); /* end the DATA part */
e25c2bd3 618 $tmp = fgets($smtpConnection, 1024);
619 $num = errorCheck($tmp, $smtpConnection, true);
620 if ($num != 250) {
e25c2bd3 621 return(0);
622 }
623
a1e937bb 624 fputs($smtpConnection, "QUIT\r\n"); /* log off */
e25c2bd3 625
626 fclose($smtpConnection);
627
628 return ($headerlength + $bodylength);
629}
60994e13 630
60994e13 631
e25c2bd3 632function errorCheck($line, $smtpConnection, $verbose = false) {
8f8f3a4e 633 global $color, $compose_new_win;
e25c2bd3 634
a1e937bb 635 /* Read new lines on a multiline response */
e25c2bd3 636 $lines = $line;
637 while(ereg("^[0-9]+-", $line)) {
638 $line = fgets($smtpConnection, 1024);
639 $lines .= $line;
640 }
641
a1e937bb 642 /* Status: 0 = fatal
643 * 5 = ok
644 */
e25c2bd3 645 $err_num = substr($line, 0, strpos($line, " "));
646 switch ($err_num) {
647 case 500: $message = 'Syntax error; command not recognized';
648 $status = 0;
649 break;
650 case 501: $message = 'Syntax error in parameters or arguments';
651 $status = 0;
652 break;
653 case 502: $message = 'Command not implemented';
654 $status = 0;
655 break;
656 case 503: $message = 'Bad sequence of commands';
657 $status = 0;
658 break;
659 case 504: $message = 'Command parameter not implemented';
660 $status = 0;
a1e937bb 661 break;
e25c2bd3 662
663 case 211: $message = 'System status, or system help reply';
664 $status = 5;
665 break;
666 case 214: $message = 'Help message';
667 $status = 5;
668 break;
669
e25c2bd3 670 case 220: $message = 'Service ready';
671 $status = 5;
672 break;
673 case 221: $message = 'Service closing transmission channel';
674 $status = 5;
675 break;
a1e937bb 676
e25c2bd3 677 case 421: $message = 'Service not available, closing chanel';
678 $status = 0;
679 break;
680
a1e937bb 681 case 235: return(5);
682 break;
e25c2bd3 683 case 250: $message = 'Requested mail action okay, completed';
684 $status = 5;
685 break;
686 case 251: $message = 'User not local; will forward';
687 $status = 5;
688 break;
689 case 334: return(5); break;
690 case 450: $message = 'Requested mail action not taken: mailbox unavailable';
691 $status = 0;
692 break;
693 case 550: $message = 'Requested action not taken: mailbox unavailable';
694 $status = 0;
695 break;
696 case 451: $message = 'Requested action aborted: error in processing';
697 $status = 0;
698 break;
699 case 551: $message = 'User not local; please try forwarding';
700 $status = 0;
701 break;
702 case 452: $message = 'Requested action not taken: insufficient system storage';
703 $status = 0;
704 break;
705 case 552: $message = 'Requested mail action aborted: exceeding storage allocation';
706 $status = 0;
707 break;
708 case 553: $message = 'Requested action not taken: mailbox name not allowed';
709 $status = 0;
710 break;
711 case 354: $message = 'Start mail input; end with .';
712 $status = 5;
713 break;
714 case 554: $message = 'Transaction failed';
715 $status = 0;
716 break;
717 default: $message = 'Unknown response: '. nl2br(htmlspecialchars($lines));
718 $status = 0;
719 $error_num = '001';
720 break;
721 }
7e235a1a 722
e25c2bd3 723 if ($status == 0) {
724 include_once('../functions/page_header.php');
8f8f3a4e 725 if ($compose_new_win == '1') {
726 compose_Header($color, 'None');
727 }
7e235a1a 728 else {
8f8f3a4e 729 displayPageHeader($color, 'None');
730 }
e25c2bd3 731 include_once('../functions/display_messages.php');
732 $lines = nl2br(htmlspecialchars($lines));
733 $msg = $message . "<br>\nServer replied: $lines";
734 plain_error_message($msg, $color);
735 }
736 if (! $verbose) return $status;
737 return $err_num;
738}
739
67c2f5ea 740/* create new reference header per rfc2822 */
741
742function calculate_references($refs, $inreplyto, $old_reply_to) {
721a6cac 743
67c2f5ea 744 $refer = "";
745 for ($i=1;$i<count($refs[0]);$i++) {
746 if (!empty($refs[0][$i])) {
747 if (preg_match("/^References:(.+)$/", $refs[0][$i], $regs)) {
748 $refer = trim($regs[1]);
749 }
750 else {
751 $refer .= ' ' . trim($refs[0][$i]);
752 }
753 }
754 }
755 $refer = trim($refer);
756 if (strlen($refer) > 2) {
757 $refer .= ' ' . $inreplyto;
758 }
759 else {
760 if (!empty($old_reply_to)) {
761 $refer .= $old_reply_to . ' ' . $inreplyto;
762 }
763 else {
764 $refer .= $inreplyto;
2044f95a 765 }
67c2f5ea 766 }
767 trim($refer);
67c2f5ea 768 return $refer;
769}
770
a3fbf5a4 771function sendMessage($t, $c, $b, $subject, $body, $reply_id, $MDN,
772 $prio = 3, $session) {
77b88425 773 global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad,
a3fbf5a4 774 $data_dir, $username, $domain, $key, $version, $sent_folder,
775 $imapServerAddress, $imapPort, $default_use_priority, $more_headers,
776 $request_mdn, $request_dr;
77b88425 777
e25c2bd3 778 $more_headers = Array();
779
77b88425 780 do_hook('smtp_send');
781
a3fbf5a4 782 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
783 $imapPort, 1);
77b88425 784
e25c2bd3 785 if (isset($reply_id) && $reply_id) {
786 sqimap_mailbox_select ($imap_stream, $mailbox);
787 sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, 'Answered');
77b88425 788
789 /* Insert In-Reply-To and References headers if the
a1e937bb 790 * message-id of the message we reply to is set (longer than "<>")
791 * The References header should really be the old Referenced header
67c2f5ea 792 * with the message ID appended, and now it is (jmunro)
a1e937bb 793 */
41336c5f 794 $sid = sqimap_session_id();
795 $query = "$sid FETCH $reply_id (BODY.PEEK[HEADER.FIELDS (Message-Id In-Reply-To)])\r\n";
796 fputs ($imap_stream, $query);
721a6cac 797 $read = sqimap_read_data($imap_stream, $sid, true, $response, $message);
41336c5f 798 $message_id = '';
799 $in_reply_to = '';
721a6cac 800
801 foreach ($read as $r) {
41336c5f 802 if (preg_match("/^message-id:(.*)/iA", $r, $regs)) {
803 $message_id = trim($regs[1]);
804 }
805 if (preg_match("/^in-reply-to:(.*)/iA", $r, $regs)) {
806 $in_reply_to = trim($regs[1]);
807 }
41336c5f 808 }
721a6cac 809
41336c5f 810 if(strlen($message_id) > 2) {
67c2f5ea 811 $refs = get_reference_header ($imap_stream, $reply_id);
41336c5f 812 $inreplyto = $message_id;
813 $old_reply_to = $in_reply_to;
67c2f5ea 814 $refer = calculate_references ($refs, $inreplyto, $old_reply_to);
815 $more_headers['In-Reply-To'] = $inreplyto;
816 $more_headers['References'] = $refer;
e25c2bd3 817 }
41336c5f 818
e25c2bd3 819 }
820 if ($default_use_priority) {
821 $more_headers = array_merge($more_headers, createPriorityHeaders($prio));
822 }
77b88425 823
604e2c03 824 $requestRecipt = 0;
825 if (isset($request_dr)) {
826 $requestRecipt += 1;
827 }
828 if (isset($request_mdn)) {
829 $requestRecipt += 2;
830 }
831 if ( $requestRecipt > 0) {
832 $more_headers = array_merge($more_headers, createReceiptHeaders($requestRecipt));
833 }
834
a1e937bb 835 /* In order to remove the problem of users not able to create
836 * messages with "." on a blank line, RFC821 has made provision
837 * in section 4.5.2 (Transparency).
838 */
e25c2bd3 839 $body = ereg_replace("\n\\.", "\n..", $body);
840 $body = ereg_replace("^\\.", "..", $body);
77b88425 841
a1e937bb 842 /* this is to catch all plain \n instances and
843 * replace them with \r\n. All newlines were converted
844 * into just \n inside the compose.php file.
a3fbf5a4 845 * But only if delimiter is, in fact, \r\n.
a1e937bb 846 */
604e2c03 847
848 if ($MDN) {
849 $more_headers["Content-Type"] = "multipart/report; ".
850 "report-type=disposition-notification;";
851 }
77b88425 852
e25c2bd3 853 if ($useSendmail) {
a3fbf5a4 854 $length = sendSendmail($t, $c, $b, $subject, $body, $more_headers,
855 $session);
e25c2bd3 856 } else {
721a6cac 857 $body = ereg_replace("\n", "\r\n", $body);
a3fbf5a4 858 $length = sendSMTP($t, $c, $b, $subject, $body, $more_headers,
859 $session);
e25c2bd3 860 }
e25c2bd3 861 if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
721a6cac 862 if ($useSendmail) $body = ereg_replace("\n", "\r\n", $body);
e25c2bd3 863 sqimap_append ($imap_stream, $sent_folder, $length);
a3fbf5a4 864 write822Header ($imap_stream, $t, $c, $b, $subject, $more_headers,
865 $session);
dd7766e0 866 writeBody ($imap_stream, $body, $session);
e25c2bd3 867 sqimap_append_done ($imap_stream);
868 }
869 sqimap_logout($imap_stream);
a1e937bb 870 /* Delete the files uploaded for attaching (if any).
871 * only if $length != 0 (if there was no error)
872 */
77b88425 873 if ($length) {
dd7766e0 874 ClearAttachments($session);
77b88425 875 }
876
e25c2bd3 877 return $length;
878}
d1b8b679 879
e25c2bd3 880function createPriorityHeaders($prio) {
881 $prio_headers = Array();
77b88425 882 $prio_headers['X-Priority'] = $prio;
883
e25c2bd3 884 switch($prio) {
77b88425 885 case 1: $prio_headers['Importance'] = 'High';
886 $prio_headers['X-MSMail-Priority'] = 'High';
e25c2bd3 887 break;
77b88425 888
889 case 3: $prio_headers['Importance'] = 'Normal';
890 $prio_headers['X-MSMail-Priority'] = 'Normal';
e25c2bd3 891 break;
77b88425 892
e25c2bd3 893 case 5:
77b88425 894 $prio_headers['Importance'] = 'Low';
895 $prio_headers['X-MSMail-Priority'] = 'Low';
e25c2bd3 896 break;
897 }
898 return $prio_headers;
899}
020abcf3 900
604e2c03 901function createReceiptHeaders($receipt) {
0804c276 902
a250d021 903 GLOBAL $data_dir, $username, $identity, $popuser, $domain;
0804c276 904
905 $receipt_headers = Array();
7aaa1434 906 if (isset($identity) && $identity != 'default') {
907 $from = getPref($data_dir, $username, 'full_name' . $identity);
908 $from_addr = getPref($data_dir, $username, 'email_address' . $identity);
909 } else {
910 $from = getPref($data_dir, $username, 'full_name');
911 $from_addr = getPref($data_dir, $username, 'email_address');
912 }
913 if ($from_addr == '') {
914 $from_addr = $popuser.'@'.$domain;
915 }
0804c276 916
917 if ($from == '') {
918 $from = "<$from_addr>";
919 }
920 else {
921 $from = '"' . encodeHeader($from) . "\" <$from_addr>";
922 }
923
924 /* On Delivery */
925 if ( $receipt == 1
926 || $receipt == 3 ) {
927 $receipt_headers["Return-Receipt-To"] = $from;
928 }
929 /* On Read */
930 if ($receipt == 2
931 || $receipt == 3 ) {
932 /* Pegasus Mail */
933 $receipt_headers["X-Confirm-Reading-To"] = $from;
934 /* RFC 2298 */
935 $receipt_headers["Disposition-Notification-To"] = $from;
936 }
937 return $receipt_headers;
938}
604e2c03 939
940
020abcf3 941?>