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