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