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