6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This contains all the functions needed to send messages through
10 * an smtp server or sendmail.
15 require_once('../functions/addressbook.php');
16 require_once('../functions/plugin.php');
17 require_once('../functions/prefs.php');
19 global $username, $popuser, $domain;
21 /* This should most probably go to some initialization... */
22 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
23 $popuser = $usernamedata[1];
24 $domain = $usernamedata[2];
29 /* We need domain for smtp */
31 $domain = getenv('HOSTNAME');
34 /* Returns true only if this message is multipart */
35 function isMultipart ($session) {
38 foreach ($attachments as $info) {
39 if ($info['session'] == $session) {
46 /* looks up aliases in the addressbook and expands them to
49 * Adds @$domain if it wasn't in the address book and if it
50 * doesn't have an @ symbol in it
52 function expandAddrs ($array) {
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
58 $abook = addressbook_init(false, true);
59 for ($i=0; $i < count($array); $i++
) {
60 $result = $abook->lookup($array[$i]);
62 if (isset($result['email'])) {
63 if (isset($result['name'])) {
64 $ret = '"'.$result['name'].'" ';
66 $ret .= '<'.$result['email'].'>';
71 if (strpos($array[$i], '@') === false) {
72 $array[$i] .= '@' . $domain;
74 $array[$i] = '<' . $array[$i] . '>';
80 /* looks up aliases in the addressbook and expands them to
81 * the RFC 821 valid RCPT address. ie <user@example.com>
82 * Adds @$domain if it wasn't in the address book and if it
83 * doesn't have an @ symbol in it
85 function expandRcptAddrs ($array) {
88 /* don't show errors -- kinda critical that we don't see
89 * them here since the redirect won't work if we do show them
91 $abook = addressbook_init(false, true);
92 for ($i=0; $i < count($array); $i++
) {
93 $result = $abook->lookup($array[$i]);
95 if (isset($result['email'])) {
96 $ret = '<'.$result['email'].'>';
100 if (strpos($array[$i], '@') === false) {
101 $array[$i] .= '@' . $domain;
103 $array[$i] = '<' . $array[$i] . '>';
110 /* Attach the files that are due to be attached
112 function attachFiles ($fp, $session, $rn="\r\n", $checkdot = false) {
113 global $attachments, $attachment_dir, $username, $languages, $squirrelmail_language;
117 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
118 if (isMultipart($session)) {
119 foreach ($attachments as $info) {
120 if ($info['session'] == $session) {
121 if (isset($info['type'])) {
122 $filetype = $info['type'];
125 $filetype = 'application/octet-stream';
128 $header = '--' . mimeBoundary() . "$rn";
129 if ( isset($info['remotefilename'])
130 && $info['remotefilename'] != '') {
131 $remotefilename = $info['remotefilename'];
132 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
133 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
135 $languages[$squirrelmail_language]['XTRA_CODE']('encode', $remotefilename);
137 $header .= "Content-Type: $filetype; name=\"" .
138 encodeHeader($remotefilename) . "\"$rn";
139 $header .= "Content-Disposition: attachment; filename=\""
140 . encodeHeader($remotefilename) . "\"$rn";
142 $header .= "Content-Type: $filetype$rn";
146 /* Use 'rb' for NT systems -- read binary
147 * Unix doesn't care -- everything's binary! :-)
150 $filename = $hashed_attachment_dir . '/'
151 . $info['localfilename'];
152 $file = fopen ($filename, 'rb');
153 if (substr($filetype, 0, 5) == 'text/' ||
154 substr($filetype, 0, 8) == 'message/' ) {
157 fputs ($fp, $header);
159 $length +
= strlen($header);
162 $checkdot_begin=true;
164 $checkdot_begin=false;
167 while ($tmp = fgets($file, 4096)) {
168 $tmp = str_replace("\r\n", "\n", $tmp);
169 $tmp = str_replace("\r", "\n", $tmp);
171 $tmp = str_replace("\n", "\r\n", $tmp);
173 if ($tmp{0} == '.' && $checkdot_begin) {
177 $tmp = str_replace("\n.","\n..",$tmp);
180 $tmp_length = strlen($tmp);
181 if ($tmp_length && $tmp{$tmp_length-1} == "\n" && $checkdot) {
182 $checkdot_begin = true;
184 $checkdot_begin = false;
191 $length +
= $tmp_length;
193 if (substr($tmp, $tmp_length - strlen($rn), strlen($rn)) != $rn) {
197 $length +
= strlen($rn);
202 $header .= "Content-Transfer-Encoding: base64"
204 if ($fp) fputs ($fp, $header);
205 $length +
= strlen($header);
206 while ($tmp = fread($file, 570)) {
207 $encoded = chunk_split(base64_encode($tmp));
208 $length +
= strlen($encoded);
209 if ($fp) fputs ($fp, $encoded);
219 /* Delete files that are uploaded for attaching
221 function deleteAttachments($session) {
222 global $username, $attachments, $attachment_dir, $data_dir;
223 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
225 $rem_attachments = array();
226 foreach ($attachments as $info) {
227 if ($info['session'] == $session) {
228 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
229 if (file_exists($attached_file)) {
230 unlink($attached_file);
233 $rem_attachments[] = $info;
236 $attachments = $rem_attachments;
237 setPref($data_dir, $username, 'attachments', serialize($attachments));
240 /* Return a nice MIME-boundary
242 function mimeBoundary () {
243 static $mimeBoundaryString;
245 if ( !isset( $mimeBoundaryString ) ||
246 $mimeBoundaryString == '') {
247 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
248 mt_rand( 10000, 99999 );
251 return $mimeBoundaryString;
254 /* Time offset for correct timezone */
255 function timezone () {
258 $diff_second = date('Z');
260 $diff_second = - $diff_second;
262 if ($diff_second > 0) {
269 $diff_second = abs($diff_second);
271 $diff_hour = floor ($diff_second / 3600);
272 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
274 $zonename = '('.strftime('%Z').')';
275 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute,
280 /* Print all the needed RFC822 headers */
281 function write822Header ($fp, $t, $c, $b, $subject, $body, $more_headers, $session, $rn="\r\n") {
282 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
283 global $data_dir, $username, $popuser, $domain, $version, $useSendmail;
284 global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
285 global $REMOTE_HOST, $identity;
287 /* Storing the header to make sure the header is the same
288 * everytime the header is printed.
290 static $header, $headerlength, $headerrn;
294 $to = expandAddrs(parseAddrs($t));
295 $cc = expandAddrs(parseAddrs($c));
296 $bcc = expandAddrs(parseAddrs($b));
297 if (isset($identity) && $identity != 'default') {
298 $reply_to = getPref($data_dir, $username, 'reply_to' . $identity);
299 $from = getPref($data_dir, $username, 'full_name' . $identity);
300 $from_addr = getFrom();
302 $reply_to = getPref($data_dir, $username, 'reply_to');
303 $from = getPref($data_dir, $username, 'full_name');
304 $from_addr = getFrom();
307 $to_list = getLineOfAddrs($to);
308 $cc_list = getLineOfAddrs($cc);
309 $bcc_list = getLineOfAddrs($bcc);
311 /* Encoding 8-bit characters and making from line */
312 $subject = encodeHeader($subject);
314 $from = "<$from_addr>";
317 $from = '"' . encodeHeader($from) . "\" <$from_addr>";
320 /* This creates an RFC 822 date */
321 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
323 /* Create a message-id */
324 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
325 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
327 /* Make an RFC822 Received: line */
328 if (isset($REMOTE_HOST)) {
329 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
332 $received_from = $REMOTE_ADDR;
335 if (isset($HTTP_VIA) ||
isset ($HTTP_X_FORWARDED_FOR)) {
336 if ($HTTP_X_FORWARDED_FOR == '') {
337 $HTTP_X_FORWARDED_FOR = 'unknown';
339 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
342 $header = "Received: from $received_from" . $rn;
343 $header .= " (SquirrelMail authenticated user $username)" . $rn;
344 $header .= " by $SERVER_NAME with HTTP;" . $rn;
345 $header .= " $date" . $rn;
347 /* Insert the rest of the header fields */
348 $header .= "Message-ID: $message_id" . $rn;
349 $header .= "Date: $date" . $rn;
350 $header .= "Subject: $subject" . $rn;
351 $header .= "From: $from" . $rn;
352 $header .= "To: $to_list" . $rn; // Who it's TO
354 if (isset($more_headers["Content-Type"])) {
355 $contentType = $more_headers["Content-Type"];
356 unset($more_headers["Content-Type"]);
359 if (isMultipart($session)) {
360 $contentType = "multipart/mixed;";
363 if ($default_charset != '') {
364 $contentType = 'text/plain; charset='.$default_charset;
367 $contentType = 'text/plain;';
372 /* Insert headers from the $more_headers array */
373 if(is_array($more_headers)) {
374 reset($more_headers);
375 while(list($h_name, $h_val) = each($more_headers)) {
376 if ($h_name == 'References') {
377 $h_val = str_replace(' ', "$rn ", $h_val);
379 $header .= sprintf("%s: %s%s", $h_name, $h_val, $rn);
384 $header .= "Cc: $cc_list" . $rn; // Who the CCs are
387 if ($reply_to != '') {
388 $header .= "Reply-To: $reply_to" . $rn;
393 // BCCs is removed from header by sendmail
394 $header .= "Bcc: $bcc_list" . $rn;
398 /* Identify SquirrelMail */
399 $header .= "X-Mailer: SquirrelMail (version $version)" . $rn;
401 /* Do the MIME-stuff */
402 $header .= "MIME-Version: 1.0" . $rn;
404 if (isMultipart($session)) {
405 $header .= 'Content-Type: '.$contentType.' boundary="';
406 $header .= mimeBoundary();
409 if (strtolower($default_charset) == 'iso-2022-jp') {
410 if (mb_detect_encoding($body) == 'ASCII') {
411 $header .= 'Content-Type: text/plain; US-ASCII' . $rn;
412 $header .= "Content-Transfer-Encoding: 8bit" . $rn;
414 $header .= 'Content-Type: '.$contentType . $rn;
415 $header .= "Content-Transfer-Encoding: 7bit" . $rn;
418 $header .= 'Content-Type: ' . $contentType . $rn;
419 $header .= "Content-Transfer-Encoding: 8bit" . $rn;
422 $header .= $rn; // One blank line to separate header and body
424 $headerlength = strlen($header);
427 if ($headerrn != $rn) {
428 $header = str_replace($headerrn, $rn, $header);
429 $headerlength = strlen($header);
433 /* Write the header */
434 if ($fp) fputs ($fp, $header);
436 return $headerlength;
441 function writeBody ($fp, $passedBody, $session, $rn="\r\n", $checkdot = false) {
442 global $default_charset;
444 $attachmentlength = 0;
446 if (isMultipart($session)) {
447 $body = '--'.mimeBoundary() . $rn;
449 if ($default_charset != "") {
450 $body .= "Content-Type: text/plain; charset=$default_charset".$rn;
453 $body .= "Content-Type: text/plain" . $rn;
456 if (strtolower($default_charset) == 'iso-2022-jp') {
457 if (mb_detect_encoding($passedBody) == 'ASCII') {
458 $body .= "Content-Transfer-Encoding: 8bit" . $rn . $rn .
459 $passedBody . $rn . $rn;
461 $body .= "Content-Transfer-Encoding: 7bit\r\n\r\n" .
462 mb_convert_encoding($passedBody, 'JIS') . "\r\n\r\n";
465 $body .= "Content-Transfer-Encoding: 8bit" . $rn . $rn .
466 $passedBody . $rn . $rn;
472 $attachmentlength = attachFiles($fp, $session, $rn, $checkdot);
474 if (!isset($postbody)) {
477 $postbody .= $rn . "--" . mimeBoundary() . "--" . $rn . $rn;
478 if ($fp) fputs ($fp, $postbody);
480 if (strtolower($default_charset) == 'iso-2022-jp') {
481 $body = mb_convert_encoding($passedBody, 'JIS') . $rn;
483 $body = $passedBody . $rn;
490 fputs ($fp, $postbody);
494 return (strlen($body) +
strlen($postbody) +
$attachmentlength);
498 /* Send mail using the sendmail command
500 function sendSendmail($t, $c, $b, $subject, $body, $more_headers, $session) {
501 global $sendmail_path, $popuser, $username, $domain;
503 /* Build envelope sender address. Make sure it doesn't contain
504 * spaces or other "weird" chars that would allow a user to
505 * exploit the shell/pipe it is used in.
507 $envelopefrom = getFrom();
508 $envelopefrom = ereg_replace("[[:blank:]]",'', $envelopefrom);
509 $envelopefrom = ereg_replace("[[:space:]]",'', $envelopefrom);
510 $envelopefrom = ereg_replace("[[:cntrl:]]",'', $envelopefrom);
513 * open pipe to sendmail or qmail-inject
514 * (qmail-inject doesn't accept -t param)
516 if (strstr($sendmail_path, "qmail-inject")) {
517 $fp = popen (escapeshellcmd("$sendmail_path -f$envelopefrom"), "w");
519 $fp = popen (escapeshellcmd("$sendmail_path -t -f$envelopefrom"), "w");
522 $headerlength = write822Header ($fp, $t, $c, $b, $subject, $body,
523 $more_headers, $session, "\n");
524 $bodylength = writeBody($fp, $body, $session, "\n", true);
528 return ($headerlength +
$bodylength);
531 function smtpReadData($smtpConnection) {
532 $read = fgets($smtpConnection, 1024);
536 $data[$counter] = $read;
537 $read = fgets($smtpConnection, 1024);
542 function sendSMTP($t, $c, $b, $subject, $body, $more_headers, $session) {
543 global $username, $popuser, $domain, $version, $smtpServerAddress,
544 $smtpPort, $data_dir, $color, $use_authenticated_smtp, $identity,
547 $to = expandRcptAddrs(parseAddrs($t));
548 $cc = expandRcptAddrs(parseAddrs($c));
549 $bcc = expandRcptAddrs(parseAddrs($b));
550 if (isset($identity) && $identity != 'default') {
551 $from_addr = getPref($data_dir, $username,
552 'email_address' . $identity);
555 $from_addr = getPref($data_dir, $username, 'email_address');
559 $from_addr = "$popuser@$domain";
562 /* POP3 BEFORE SMTP CODE HERE */
563 global $pop_before_smtp;
564 if (isset($pop_before_smtp) && $pop_before_smtp === true) {
565 if (!isset($pop_port)) {
568 if (!isset($pop_server)) {
569 $pop_server = $smtpServerAddress; /* usually the same host! */
571 $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str);
572 if (!$popConnection) {
573 error_log("Error connecting to POP Server ($pop_server:$pop_port)"
574 . " $err_no : $err_str");
576 $tmp = fgets($popConnection, 1024); /* banner */
577 if (!eregi("^\+OK", $tmp, $regs)) {
580 fputs($popConnection, "USER $username\r\n");
581 $tmp = fgets($popConnection, 1024);
582 if (!eregi("^\+OK", $tmp, $regs)) {
585 fputs($popConnection, 'PASS ' . OneTimePadDecrypt($key, $onetimepad) . "\r\n");
586 $tmp = fgets($popConnection, 1024);
587 if (!eregi("^\+OK", $tmp, $regs)) {
590 fputs($popConnection, "QUIT\r\n"); /* log off */
591 fclose($popConnection);
595 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort,
596 $errorNumber, $errorString);
597 if (!$smtpConnection) {
598 echo 'Error connecting to SMTP Server.<br>';
599 echo "$errorNumber : $errorString<br>";
602 $tmp = fgets($smtpConnection, 1024);
603 if (errorCheck($tmp, $smtpConnection)!=5) {
607 $to_list = getLineOfAddrs($to);
608 $cc_list = getLineOfAddrs($cc);
610 /* Lets introduce ourselves */
611 if (! isset ($use_authenticated_smtp)
612 ||
$use_authenticated_smtp == false) {
613 fputs($smtpConnection, "HELO $domain\r\n");
614 $tmp = fgets($smtpConnection, 1024);
615 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
617 fputs($smtpConnection, "EHLO $domain\r\n");
618 $tmp = fgets($smtpConnection, 1024);
619 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
621 fputs($smtpConnection, "AUTH LOGIN\r\n");
622 $tmp = fgets($smtpConnection, 1024);
623 if (errorCheck($tmp, $smtpConnection)!=5) {
627 fputs($smtpConnection, base64_encode ($username) . "\r\n");
628 $tmp = fgets($smtpConnection, 1024);
629 if (errorCheck($tmp, $smtpConnection)!=5) {
633 fputs($smtpConnection, base64_encode
634 (OneTimePadDecrypt($key, $onetimepad)) . "\r\n");
635 $tmp = fgets($smtpConnection, 1024);
636 if (errorCheck($tmp, $smtpConnection)!=5) {
641 /* Ok, who is sending the message? */
642 fputs($smtpConnection, "MAIL FROM: <$from_addr>\r\n");
643 $tmp = fgets($smtpConnection, 1024);
644 if (errorCheck($tmp, $smtpConnection)!=5) {
648 /* send who the recipients are */
649 for ($i = 0; $i < count($to); $i++
) {
650 fputs($smtpConnection, "RCPT TO: $to[$i]\r\n");
651 $tmp = fgets($smtpConnection, 1024);
652 if (errorCheck($tmp, $smtpConnection)!=5) {
656 for ($i = 0; $i < count($cc); $i++
) {
657 fputs($smtpConnection, "RCPT TO: $cc[$i]\r\n");
658 $tmp = fgets($smtpConnection, 1024);
659 if (errorCheck($tmp, $smtpConnection)!=5) {
663 for ($i = 0; $i < count($bcc); $i++
) {
664 fputs($smtpConnection, "RCPT TO: $bcc[$i]\r\n");
665 $tmp = fgets($smtpConnection, 1024);
666 if (errorCheck($tmp, $smtpConnection)!=5) {
671 /* Lets start sending the actual message */
672 fputs($smtpConnection, "DATA\r\n");
673 $tmp = fgets($smtpConnection, 1024);
674 if (errorCheck($tmp, $smtpConnection)!=5) {
678 /* Send the message */
679 $headerlength = write822Header ($smtpConnection, $t, $c, $b,
680 $subject, $body, $more_headers, $session);
681 $bodylength = writeBody($smtpConnection, $body, $session,"\r\n", true);
683 fputs($smtpConnection, ".\r\n"); /* end the DATA part */
684 $tmp = fgets($smtpConnection, 1024);
685 $num = errorCheck($tmp, $smtpConnection, true);
690 fputs($smtpConnection, "QUIT\r\n"); /* log off */
692 fclose($smtpConnection);
694 return ($headerlength +
$bodylength);
698 function errorCheck($line, $smtpConnection, $verbose = false) {
699 global $color, $compose_new_win;
701 /* Read new lines on a multiline response */
703 while(ereg("^[0-9]+-", $line)) {
704 $line = fgets($smtpConnection, 1024);
711 $err_num = substr($line, 0, strpos($line, " "));
713 case 500: $message = 'Syntax error; command not recognized';
716 case 501: $message = 'Syntax error in parameters or arguments';
719 case 502: $message = 'Command not implemented';
722 case 503: $message = 'Bad sequence of commands';
725 case 504: $message = 'Command parameter not implemented';
729 case 211: $message = 'System status, or system help reply';
732 case 214: $message = 'Help message';
736 case 220: $message = 'Service ready';
739 case 221: $message = 'Service closing transmission channel';
743 case 421: $message = 'Service not available, closing chanel';
749 case 250: $message = 'Requested mail action okay, completed';
752 case 251: $message = 'User not local; will forward';
755 case 334: return(5); break;
756 case 450: $message = 'Requested mail action not taken: mailbox unavailable';
759 case 550: $message = 'Requested action not taken: mailbox unavailable';
762 case 451: $message = 'Requested action aborted: error in processing';
765 case 551: $message = 'User not local; please try forwarding';
768 case 452: $message = 'Requested action not taken: insufficient system storage';
771 case 552: $message = 'Requested mail action aborted: exceeding storage allocation';
774 case 553: $message = 'Requested action not taken: mailbox name not allowed';
777 case 354: $message = 'Start mail input; end with .';
780 case 554: $message = 'Transaction failed';
783 default: $message = 'Unknown response: '. nl2br(htmlspecialchars($lines));
790 include_once('../functions/page_header.php');
791 if ($compose_new_win == '1') {
792 compose_Header($color, 'None');
795 displayPageHeader($color, 'None');
797 include_once('../functions/display_messages.php');
798 $lines = nl2br(htmlspecialchars($lines));
799 $msg = $message . "<br>\nServer replied: $lines";
800 plain_error_message($msg, $color);
802 if (! $verbose) return $status;
806 /* create new reference header per rfc2822 */
808 function calculate_references($refs, $inreplyto, $old_reply_to) {
811 for ($i=1;$i<count($refs[0]);$i++
) {
812 if (!empty($refs[0][$i])) {
813 if (preg_match("/^References:(.+)$/UAi", $refs[0][$i], $regs)) {
814 $refer = trim($regs[1]);
817 $refer .= ' ' . trim($refs[0][$i]);
821 $refer_a = explode(' ', $refer);
823 foreach ($refer_a as $ref) {
825 if ($ref && $ref{0} == '<' && $ref{(strlen($ref)-1)} == '>') {
826 $refer .= $ref . ' ';
829 $refer = trim($refer);
830 if (strlen($refer) > 2) {
831 $refer .= ' ' . $inreplyto;
834 if (!empty($old_reply_to)) {
835 $refer .= $old_reply_to . ' ' . $inreplyto;
838 $refer .= $inreplyto;
845 function sendMessage($t, $c, $b, $subject, $body, $reply_id, $MDN,
846 $prio = 3, $session) {
847 global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad,
848 $data_dir, $username, $domain, $key, $version, $sent_folder,
849 $imapServerAddress, $imapPort, $default_use_priority, $more_headers,
850 $request_mdn, $request_dr, $uid_support;
852 $more_headers = Array();
854 do_hook('smtp_send');
856 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
859 if (isset($reply_id) && $reply_id) {
860 sqimap_mailbox_select ($imap_stream, $mailbox);
861 sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, 'Answered', true);
863 /* Insert In-Reply-To and References headers if the
864 * message-id of the message we reply to is set (longer than "<>")
865 * The References header should really be the old Referenced header
866 * with the message ID appended, and now it is (jmunro)
868 $sid = sqimap_session_id($uid_support);
869 $query = "$sid FETCH $reply_id (BODY.PEEK[HEADER.FIELDS (Message-Id In-Reply-To)])\r\n";
870 fputs ($imap_stream, $query);
871 $read = sqimap_read_data($imap_stream, $sid, true, $response, $message);
875 foreach ($read as $r) {
876 if (preg_match("/^message-id:(.*)/iA", $r, $regs)) {
877 $message_id = trim($regs[1]);
879 if (preg_match("/^in-reply-to:(.*)/iA", $r, $regs)) {
880 $in_reply_to = trim($regs[1]);
884 if(strlen($message_id) > 2) {
885 $refs = get_reference_header ($imap_stream, $reply_id);
886 $inreplyto = $message_id;
887 $old_reply_to = $in_reply_to;
888 $refer = calculate_references ($refs, $inreplyto, $old_reply_to);
889 $more_headers['In-Reply-To'] = $inreplyto;
890 $more_headers['References'] = $refer;
894 if ($default_use_priority) {
895 $more_headers = array_merge($more_headers, createPriorityHeaders($prio));
899 if (isset($request_dr)) {
902 if (isset($request_mdn)) {
905 if ( $requestRecipt > 0) {
906 $more_headers = array_merge($more_headers, createReceiptHeaders($requestRecipt));
909 $more_headers["Content-Type"] = "multipart/report; ".
910 "report-type=disposition-notification;";
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).
918 if (($body && $body{0} == '.')) {
921 $body = str_replace("\n.","\n..",$body);
922 $length = sendSendmail($t, $c, $b, $subject, $body, $more_headers,
925 /* In order to remove the problem of users not able to create
926 * messages with "." on a blank line, RFC821 has made provision
927 * in section 4.5.2 (Transparency).
929 if (($body && $body{0} == '.')) {
932 $body = str_replace("\n.","\n..",$body);
933 $length = sendSMTP($t, $c, $b, $subject, $body, $more_headers,
936 if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
938 $body = ereg_replace("\n", "\r\n", $body);
939 $headerlength = write822Header (false, $t, $c, $b, $subject, $body, $more_headers, $session, "\r\n");
940 $bodylength = writeBody(false, $body, $session, "\r\n");
941 $length = $headerlength +
$bodylength;
942 sqimap_append ($imap_stream, $sent_folder, $length);
943 write822Header ($imap_stream, $t, $c, $b, $subject, $body, $more_headers,
945 writeBody ($imap_stream, $body, $session);
946 sqimap_append_done ($imap_stream);
948 sqimap_logout($imap_stream);
949 /* Delete the files uploaded for attaching (if any).
950 * only if $length != 0 (if there was no error)
953 ClearAttachments($session);
958 function createPriorityHeaders($prio) {
959 $prio_headers = Array();
960 $prio_headers['X-Priority'] = $prio;
963 case 1: $prio_headers['Importance'] = 'High';
964 $prio_headers['X-MSMail-Priority'] = 'High';
967 case 3: $prio_headers['Importance'] = 'Normal';
968 $prio_headers['X-MSMail-Priority'] = 'Normal';
972 $prio_headers['Importance'] = 'Low';
973 $prio_headers['X-MSMail-Priority'] = 'Low';
976 return $prio_headers;
979 function createReceiptHeaders($receipt) {
981 GLOBAL $data_dir, $username, $identity, $popuser, $domain;
983 $receipt_headers = Array();
984 if (isset($identity) && $identity != 'default') {
985 $from = getPref($data_dir, $username, 'full_name' . $identity);
986 $from_addr = getPref($data_dir, $username, 'email_address' . $identity);
988 $from = getPref($data_dir, $username, 'full_name');
989 $from_addr = getPref($data_dir, $username, 'email_address');
991 if ($from_addr == '') {
992 $from_addr = $popuser.'@'.$domain;
996 $from = "<$from_addr>";
999 $from = '"' . encodeHeader($from) . "\" <$from_addr>";
1004 ||
$receipt == 3 ) {
1005 $receipt_headers["Return-Receipt-To"] = $from;
1009 ||
$receipt == 3 ) {
1011 $receipt_headers["X-Confirm-Reading-To"] = $from;
1013 $receipt_headers["Disposition-Notification-To"] = $from;
1015 return $receipt_headers;
1018 /* Figure out what the 'From:' address is
1021 function getFrom() {
1022 global $username, $popuser, $domain, $data_dir, $identity;
1023 if (isset($identity) && $identity != 'default') {
1024 $from_addr = getPref($data_dir, $username,
1025 'email_address' . $identity);
1028 $from_addr = getPref($data_dir, $username, 'email_address');
1032 $from_addr = "$popuser@$domain";