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] . '>';
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
86 function expandRcptAddrs ($array) {
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
92 $abook = addressbook_init(false, true);
93 for ($i=0; $i < count($array); $i++
) {
94 $result = $abook->lookup($array[$i]);
96 if (isset($result['email'])) {
97 $ret = '<'.$result['email'].'>';
101 if (strpos($array[$i], '@') === false) {
102 $array[$i] .= '@' . $domain;
104 $array[$i] = '<' . $array[$i] . '>';
111 /* Attach the files that are due to be attached
113 function attachFiles ($fp, $session, $rn="\r\n", $checkdot = false) {
114 global $attachments, $attachment_dir, $username, $languages, $squirrelmail_language;
118 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
119 if (isMultipart($session)) {
120 foreach ($attachments as $info) {
121 if ($info['session'] == $session) {
122 if (isset($info['type'])) {
123 $filetype = $info['type'];
126 $filetype = 'application/octet-stream';
129 $header = '--' . mimeBoundary() . "$rn";
130 if ( isset($info['remotefilename'])
131 && $info['remotefilename'] != '') {
132 $remotefilename = $info['remotefilename'];
133 if (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);
161 $checkdot_begin=true;
163 $checkdot_begin=false;
165 while ($tmp = fgets($file, 4096)) {
166 $tmp = str_replace("\r\n", "\n", $tmp);
167 $tmp = str_replace("\r", "\n", $tmp);
168 /* In order to remove the problem of users not able to create
169 * messages with "." on a blank line, RFC821 has made provision
170 * in section 4.5.2 (Transparency).
172 if ($tmp{0} == '.' && $checkdot_begin) {
176 $tmp = str_replace("\n.","\n..",$tmp);
180 $tmp = str_replace("\n", "\r\n", $tmp);
182 $tmp_length = strlen($tmp);
183 if ($tmp{$tmp_length-1} == "\n" && $checkdot) {
184 $checkdot_begin = true;
186 $checkdot_begin = false;
191 $length +
= strlen($tmp_length);
194 if (substr($tmp, strlen($tmp) - strlen($rn), strlen($rn)) != $rn) {
200 $header .= "Content-Transfer-Encoding: base64"
202 if ($fp) fputs ($fp, $header);
203 $length +
= strlen($header);
204 while ($tmp = fread($file, 570)) {
205 $encoded = chunk_split(base64_encode($tmp));
206 $length +
= strlen($encoded);
207 if ($fp) fputs ($fp, $encoded);
217 /* Delete files that are uploaded for attaching
219 function deleteAttachments($session) {
220 global $username, $attachments, $attachment_dir, $data_dir;
221 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
223 $rem_attachments = array();
224 foreach ($attachments as $info) {
225 if ($info['session'] == $session) {
226 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
227 if (file_exists($attached_file)) {
228 unlink($attached_file);
231 $rem_attachments[] = $info;
234 $attachments = $rem_attachments;
235 setPref($data_dir, $username, 'attachments', serialize($attachments));
238 /* Return a nice MIME-boundary
240 function mimeBoundary () {
241 static $mimeBoundaryString;
243 if ( !isset( $mimeBoundaryString ) ||
244 $mimeBoundaryString == '') {
245 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
246 mt_rand( 10000, 99999 );
249 return $mimeBoundaryString;
252 /* Time offset for correct timezone */
253 function timezone () {
256 $diff_second = date('Z');
258 $diff_second = - $diff_second;
260 if ($diff_second > 0) {
267 $diff_second = abs($diff_second);
269 $diff_hour = floor ($diff_second / 3600);
270 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
272 $zonename = '('.strftime('%Z').')';
273 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute,
278 /* Print all the needed RFC822 headers */
279 function write822Header ($fp, $t, $c, $b, $subject, $body, $more_headers, $session, $rn="\r\n") {
280 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
281 global $data_dir, $username, $popuser, $domain, $version, $useSendmail;
282 global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
283 global $REMOTE_HOST, $identity;
285 /* Storing the header to make sure the header is the same
286 * everytime the header is printed.
288 static $header, $headerlength, $headerrn;
292 $to = expandAddrs(parseAddrs($t));
293 $cc = expandAddrs(parseAddrs($c));
294 $bcc = expandAddrs(parseAddrs($b));
295 if (isset($identity) && $identity != 'default') {
296 $reply_to = getPref($data_dir, $username, 'reply_to' . $identity);
297 $from = getPref($data_dir, $username, 'full_name' . $identity);
298 $from_addr = getFrom();
300 $reply_to = getPref($data_dir, $username, 'reply_to');
301 $from = getPref($data_dir, $username, 'full_name');
302 $from_addr = getFrom();
305 $to_list = getLineOfAddrs($to);
306 $cc_list = getLineOfAddrs($cc);
307 $bcc_list = getLineOfAddrs($bcc);
309 /* Encoding 8-bit characters and making from line */
310 $subject = encodeHeader($subject);
312 $from = "<$from_addr>";
315 $from = '"' . encodeHeader($from) . "\" <$from_addr>";
318 /* This creates an RFC 822 date */
319 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
321 /* Create a message-id */
322 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
323 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
325 /* Make an RFC822 Received: line */
326 if (isset($REMOTE_HOST)) {
327 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
330 $received_from = $REMOTE_ADDR;
333 if (isset($HTTP_VIA) ||
isset ($HTTP_X_FORWARDED_FOR)) {
334 if ($HTTP_X_FORWARDED_FOR == '') {
335 $HTTP_X_FORWARDED_FOR = 'unknown';
337 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
340 $header = "Received: from $received_from" . $rn;
341 $header .= " (SquirrelMail authenticated user $username)" . $rn;
342 $header .= " by $SERVER_NAME with HTTP;" . $rn;
343 $header .= " $date" . $rn;
345 /* Insert the rest of the header fields */
346 $header .= "Message-ID: $message_id" . $rn;
347 $header .= "Date: $date" . $rn;
348 $header .= "Subject: $subject" . $rn;
349 $header .= "From: $from" . $rn;
350 $header .= "To: $to_list" . $rn; // Who it's TO
352 if (isset($more_headers["Content-Type"])) {
353 $contentType = $more_headers["Content-Type"];
354 unset($more_headers["Content-Type"]);
357 if (isMultipart($session)) {
358 $contentType = "multipart/mixed;";
361 if ($default_charset != '') {
362 $contentType = 'text/plain; charset='.$default_charset;
365 $contentType = 'text/plain;';
370 /* Insert headers from the $more_headers array */
371 if(is_array($more_headers)) {
372 reset($more_headers);
373 while(list($h_name, $h_val) = each($more_headers)) {
374 if ($h_name == 'References') {
375 $h_val = str_replace(' ', "$rn ", $h_val);
377 $header .= sprintf("%s: %s%s", $h_name, $h_val, $rn);
382 $header .= "Cc: $cc_list" . $rn; // Who the CCs are
385 if ($reply_to != '') {
386 $header .= "Reply-To: $reply_to" . $rn;
391 // BCCs is removed from header by sendmail
392 $header .= "Bcc: $bcc_list" . $rn;
396 /* Identify SquirrelMail */
397 $header .= "X-Mailer: SquirrelMail (version $version)" . $rn;
399 /* Do the MIME-stuff */
400 $header .= "MIME-Version: 1.0" . $rn;
402 if (isMultipart($session)) {
403 $header .= 'Content-Type: '.$contentType.' boundary="';
404 $header .= mimeBoundary();
407 if (strtolower($default_charset) == 'iso-2022-jp') {
408 if (mb_detect_encoding($body) == 'ASCII') {
409 $header .= 'Content-Type: text/plain; US-ASCII' . $rn;
410 $header .= "Content-Transfer-Encoding: 8bit" . $rn;
412 $header .= 'Content-Type: '.$contentType . $rn;
413 $header .= "Content-Transfer-Encoding: 7bit" . $rn;
416 $header .= 'Content-Type: ' . $contentType . $rn;
417 $header .= "Content-Transfer-Encoding: 8bit" . $rn;
420 $header .= $rn; // One blank line to separate header and body
422 $headerlength = strlen($header);
425 if ($headerrn != $rn) {
426 $header = str_replace($headerrn, $rn, $header);
427 $headerlength = strlen($header);
431 /* Write the header */
432 if ($fp) fputs ($fp, $header);
434 return $headerlength;
439 function writeBody ($fp, $passedBody, $session, $rn="\r\n", $checkdot = false) {
440 global $default_charset;
442 $attachmentlength = 0;
444 if (isMultipart($session)) {
445 $body = '--'.mimeBoundary() . $rn;
447 if ($default_charset != "") {
448 $body .= "Content-Type: text/plain; charset=$default_charset".$rn;
451 $body .= "Content-Type: text/plain" . $rn;
454 if (strtolower($default_charset) == 'iso-2022-jp') {
455 if (mb_detect_encoding($passedBody) == 'ASCII') {
456 $body .= "Content-Transfer-Encoding: 8bit" . $rn . $rn .
457 $passedBody . $rn . $rn;
459 $body .= "Content-Transfer-Encoding: 7bit\r\n\r\n" .
460 mb_convert_encoding($passedBody, 'JIS') . "\r\n\r\n";
463 $body .= "Content-Transfer-Encoding: 8bit" . $rn . $rn .
464 $passedBody . $rn . $rn;
470 $attachmentlength = attachFiles($fp, $session, $rn, $checkdot);
472 if (!isset($postbody)) {
475 $postbody .= $rn . "--" . mimeBoundary() . "--" . $rn . $rn;
476 if ($fp) fputs ($fp, $postbody);
478 if (strtolower($default_charset) == 'iso-2022-jp') {
479 $body = mb_convert_encoding($passedBody, 'JIS') . $rn;
481 $body = $passedBody . $rn;
488 fputs ($fp, $postbody);
492 return (strlen($body) +
strlen($postbody) +
$attachmentlength);
495 /* Send mail using the sendmail command
497 function sendSendmail($t, $c, $b, $subject, $body, $more_headers, $session) {
498 global $sendmail_path, $popuser, $username, $domain;
500 /* Build envelope sender address. Make sure it doesn't contain
501 * spaces or other "weird" chars that would allow a user to
502 * exploit the shell/pipe it is used in.
504 $envelopefrom = getFrom();
505 $envelopefrom = ereg_replace("[[:blank:]]",'', $envelopefrom);
506 $envelopefrom = ereg_replace("[[:space:]]",'', $envelopefrom);
507 $envelopefrom = ereg_replace("[[:cntrl:]]",'', $envelopefrom);
510 * open pipe to sendmail or qmail-inject
511 * (qmail-inject doesn't accept -t param)
513 if (strstr($sendmail_path, "qmail-inject")) {
514 $fp = popen (escapeshellcmd("$sendmail_path -f$envelopefrom"), "w");
516 $fp = popen (escapeshellcmd("$sendmail_path -t -f$envelopefrom"), "w");
519 $headerlength = write822Header ($fp, $t, $c, $b, $subject, $body,
520 $more_headers, $session, "\n");
521 $bodylength = writeBody($fp, $body, $session, "\n", true);
525 return ($headerlength +
$bodylength);
528 function smtpReadData($smtpConnection) {
529 $read = fgets($smtpConnection, 1024);
533 $data[$counter] = $read;
534 $read = fgets($smtpConnection, 1024);
539 function sendSMTP($t, $c, $b, $subject, $body, $more_headers, $session) {
540 global $username, $popuser, $domain, $version, $smtpServerAddress,
541 $smtpPort, $data_dir, $color, $use_authenticated_smtp, $identity,
544 $to = expandRcptAddrs(parseAddrs($t));
545 $cc = expandRcptAddrs(parseAddrs($c));
546 $bcc = expandRcptAddrs(parseAddrs($b));
547 if (isset($identity) && $identity != 'default') {
548 $from_addr = getPref($data_dir, $username,
549 'email_address' . $identity);
552 $from_addr = getPref($data_dir, $username, 'email_address');
556 $from_addr = "$popuser@$domain";
559 /* POP3 BEFORE SMTP CODE HERE */
560 global $pop_before_smtp;
561 if (isset($pop_before_smtp) && $pop_before_smtp === true) {
562 if (!isset($pop_port)) {
565 if (!isset($pop_server)) {
566 $pop_server = $smtpServerAddress; /* usually the same host! */
568 $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str);
569 if (!$popConnection) {
570 error_log("Error connecting to POP Server ($pop_server:$pop_port)"
571 . " $err_no : $err_str");
573 $tmp = fgets($popConnection, 1024); /* banner */
574 if (!eregi("^\+OK", $tmp, $regs)) {
577 fputs($popConnection, "USER $username\r\n");
578 $tmp = fgets($popConnection, 1024);
579 if (!eregi("^\+OK", $tmp, $regs)) {
582 fputs($popConnection, 'PASS ' . OneTimePadDecrypt($key, $onetimepad) . "\r\n");
583 $tmp = fgets($popConnection, 1024);
584 if (!eregi("^\+OK", $tmp, $regs)) {
587 fputs($popConnection, "QUIT\r\n"); /* log off */
588 fclose($popConnection);
592 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort,
593 $errorNumber, $errorString);
594 if (!$smtpConnection) {
595 echo 'Error connecting to SMTP Server.<br>';
596 echo "$errorNumber : $errorString<br>";
599 $tmp = fgets($smtpConnection, 1024);
600 if (errorCheck($tmp, $smtpConnection)!=5) {
604 $to_list = getLineOfAddrs($to);
605 $cc_list = getLineOfAddrs($cc);
607 /* Lets introduce ourselves */
608 if (! isset ($use_authenticated_smtp)
609 ||
$use_authenticated_smtp == false) {
610 fputs($smtpConnection, "HELO $domain\r\n");
611 $tmp = fgets($smtpConnection, 1024);
612 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
614 fputs($smtpConnection, "EHLO $domain\r\n");
615 $tmp = fgets($smtpConnection, 1024);
616 if (errorCheck($tmp, $smtpConnection)!=5) return(0);
618 fputs($smtpConnection, "AUTH LOGIN\r\n");
619 $tmp = fgets($smtpConnection, 1024);
620 if (errorCheck($tmp, $smtpConnection)!=5) {
624 fputs($smtpConnection, base64_encode ($username) . "\r\n");
625 $tmp = fgets($smtpConnection, 1024);
626 if (errorCheck($tmp, $smtpConnection)!=5) {
630 fputs($smtpConnection, base64_encode
631 (OneTimePadDecrypt($key, $onetimepad)) . "\r\n");
632 $tmp = fgets($smtpConnection, 1024);
633 if (errorCheck($tmp, $smtpConnection)!=5) {
638 /* Ok, who is sending the message? */
639 fputs($smtpConnection, "MAIL FROM: <$from_addr>\r\n");
640 $tmp = fgets($smtpConnection, 1024);
641 if (errorCheck($tmp, $smtpConnection)!=5) {
645 /* send who the recipients are */
646 for ($i = 0; $i < count($to); $i++
) {
647 fputs($smtpConnection, "RCPT TO: $to[$i]\r\n");
648 $tmp = fgets($smtpConnection, 1024);
649 if (errorCheck($tmp, $smtpConnection)!=5) {
653 for ($i = 0; $i < count($cc); $i++
) {
654 fputs($smtpConnection, "RCPT TO: $cc[$i]\r\n");
655 $tmp = fgets($smtpConnection, 1024);
656 if (errorCheck($tmp, $smtpConnection)!=5) {
660 for ($i = 0; $i < count($bcc); $i++
) {
661 fputs($smtpConnection, "RCPT TO: $bcc[$i]\r\n");
662 $tmp = fgets($smtpConnection, 1024);
663 if (errorCheck($tmp, $smtpConnection)!=5) {
668 /* Lets start sending the actual message */
669 fputs($smtpConnection, "DATA\r\n");
670 $tmp = fgets($smtpConnection, 1024);
671 if (errorCheck($tmp, $smtpConnection)!=5) {
675 /* Send the message */
676 $headerlength = write822Header ($smtpConnection, $t, $c, $b,
677 $subject, $body, $more_headers, $session);
678 $bodylength = writeBody($smtpConnection, $body, $session, "\r\n", true);
680 fputs($smtpConnection, ".\r\n"); /* end the DATA part */
681 $tmp = fgets($smtpConnection, 1024);
682 $num = errorCheck($tmp, $smtpConnection, true);
687 fputs($smtpConnection, "QUIT\r\n"); /* log off */
689 fclose($smtpConnection);
691 return ($headerlength +
$bodylength);
695 function errorCheck($line, $smtpConnection, $verbose = false) {
696 global $color, $compose_new_win;
698 /* Read new lines on a multiline response */
700 while(ereg("^[0-9]+-", $line)) {
701 $line = fgets($smtpConnection, 1024);
708 $err_num = substr($line, 0, strpos($line, " "));
710 case 500: $message = 'Syntax error; command not recognized';
713 case 501: $message = 'Syntax error in parameters or arguments';
716 case 502: $message = 'Command not implemented';
719 case 503: $message = 'Bad sequence of commands';
722 case 504: $message = 'Command parameter not implemented';
726 case 211: $message = 'System status, or system help reply';
729 case 214: $message = 'Help message';
733 case 220: $message = 'Service ready';
736 case 221: $message = 'Service closing transmission channel';
740 case 421: $message = 'Service not available, closing chanel';
746 case 250: $message = 'Requested mail action okay, completed';
749 case 251: $message = 'User not local; will forward';
752 case 334: return(5); break;
753 case 450: $message = 'Requested mail action not taken: mailbox unavailable';
756 case 550: $message = 'Requested action not taken: mailbox unavailable';
759 case 451: $message = 'Requested action aborted: error in processing';
762 case 551: $message = 'User not local; please try forwarding';
765 case 452: $message = 'Requested action not taken: insufficient system storage';
768 case 552: $message = 'Requested mail action aborted: exceeding storage allocation';
771 case 553: $message = 'Requested action not taken: mailbox name not allowed';
774 case 354: $message = 'Start mail input; end with .';
777 case 554: $message = 'Transaction failed';
780 default: $message = 'Unknown response: '. nl2br(htmlspecialchars($lines));
787 include_once('../functions/page_header.php');
788 if ($compose_new_win == '1') {
789 compose_Header($color, 'None');
792 displayPageHeader($color, 'None');
794 include_once('../functions/display_messages.php');
795 $lines = nl2br(htmlspecialchars($lines));
796 $msg = $message . "<br>\nServer replied: $lines";
797 plain_error_message($msg, $color);
799 if (! $verbose) return $status;
803 /* create new reference header per rfc2822 */
805 function calculate_references($refs, $inreplyto, $old_reply_to) {
808 for ($i=1;$i<count($refs[0]);$i++
) {
809 if (!empty($refs[0][$i])) {
810 if (preg_match("/^References:(.+)$/UA", $refs[0][$i], $regs)) {
811 $refer = trim($regs[1]);
814 $refer .= ' ' . trim($refs[0][$i]);
819 $refer_a = explode(' ', $refer);
821 foreach ($refer_a as $ref) {
823 if ($ref{0} == '<' && $ref{(strlen($ref)-1)} == '>') {
824 $refer .= $ref . ' ';
827 $refer = trim($refer);
828 if (strlen($refer) > 2) {
829 $refer .= ' ' . $inreplyto;
832 if (!empty($old_reply_to)) {
833 $refer .= $old_reply_to . ' ' . $inreplyto;
836 $refer .= $inreplyto;
843 function sendMessage($t, $c, $b, $subject, $body, $reply_id, $MDN,
844 $prio = 3, $session) {
845 global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad,
846 $data_dir, $username, $domain, $key, $version, $sent_folder,
847 $imapServerAddress, $imapPort, $default_use_priority, $more_headers,
848 $request_mdn, $request_dr, $uid_support;
850 $more_headers = Array();
852 do_hook('smtp_send');
854 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
857 if (isset($reply_id) && $reply_id) {
858 sqimap_mailbox_select ($imap_stream, $mailbox);
859 sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, 'Answered', false);
861 /* Insert In-Reply-To and References headers if the
862 * message-id of the message we reply to is set (longer than "<>")
863 * The References header should really be the old Referenced header
864 * with the message ID appended, and now it is (jmunro)
866 $sid = sqimap_session_id($uid_support);
867 $query = "$sid FETCH $reply_id (BODY.PEEK[HEADER.FIELDS (Message-Id In-Reply-To)])\r\n";
868 fputs ($imap_stream, $query);
869 $read = sqimap_read_data($imap_stream, $sid, true, $response, $message);
873 foreach ($read as $r) {
874 if (preg_match("/^message-id:(.*)/iA", $r, $regs)) {
875 $message_id = trim($regs[1]);
877 if (preg_match("/^in-reply-to:(.*)/iA", $r, $regs)) {
878 $in_reply_to = trim($regs[1]);
882 if(strlen($message_id) > 2) {
883 $refs = get_reference_header ($imap_stream, $reply_id);
884 $inreplyto = $message_id;
885 $old_reply_to = $in_reply_to;
886 $refer = calculate_references ($refs, $inreplyto, $old_reply_to);
887 $more_headers['In-Reply-To'] = $inreplyto;
888 $more_headers['References'] = $refer;
892 if ($default_use_priority) {
893 $more_headers = array_merge($more_headers, createPriorityHeaders($prio));
897 if (isset($request_dr)) {
900 if (isset($request_mdn)) {
903 if ( $requestRecipt > 0) {
904 $more_headers = array_merge($more_headers, createReceiptHeaders($requestRecipt));
907 /* this is to catch all plain \n instances and
908 * replace them with \r\n. All newlines were converted
909 * into just \n inside the compose.php file.
910 * But only if delimiter is, in fact, \r\n.
914 $more_headers["Content-Type"] = "multipart/report; ".
915 "report-type=disposition-notification;";
919 /* In order to remove the problem of users not able to create
920 * messages with "." on a blank line, RFC821 has made provision
921 * in section 4.5.2 (Transparency).
923 $body_sendmail = $body;
924 if (($body_sendmail{0} == '.')) {
925 $body_sendmail = '.' . $body_sendmail;
927 $body_sendmail = str_replace("\n.","\n..",$body_sendmail);
929 $length = sendSendmail($t, $c, $b, $subject, $body_sendmail, $more_headers,
931 $body = ereg_replace("\n", "\r\n", $body);
933 $body = ereg_replace("\n", "\r\n", $body);
934 /* In order to remove the problem of users not able to create
935 * messages with "." on a blank line, RFC821 has made provision
936 * in section 4.5.2 (Transparency).
939 if (($body_smtp{0} == '.')) {
940 $body_smtp = '.' . $body_smtp;
942 $body_smtp = str_replace("\n.","\n..",$body_smtp);
945 $length = sendSMTP($t, $c, $b, $subject, $body_smtp, $more_headers,
948 if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
949 $headerlength = write822Header (false, $t, $c, $b, $subject, $more_headers, $session, "\r\n");
950 $bodylength = writeBody(false, $body, $session, "\r\n");
951 $length = $headerlength +
$bodylength;
953 sqimap_append ($imap_stream, $sent_folder, $length);
954 write822Header ($imap_stream, $t, $c, $b, $subject, $body, $more_headers,
956 writeBody ($imap_stream, $body, $session);
957 sqimap_append_done ($imap_stream);
959 sqimap_logout($imap_stream);
960 /* Delete the files uploaded for attaching (if any).
961 * only if $length != 0 (if there was no error)
964 ClearAttachments($session);
970 function createPriorityHeaders($prio) {
971 $prio_headers = Array();
972 $prio_headers['X-Priority'] = $prio;
975 case 1: $prio_headers['Importance'] = 'High';
976 $prio_headers['X-MSMail-Priority'] = 'High';
979 case 3: $prio_headers['Importance'] = 'Normal';
980 $prio_headers['X-MSMail-Priority'] = 'Normal';
984 $prio_headers['Importance'] = 'Low';
985 $prio_headers['X-MSMail-Priority'] = 'Low';
988 return $prio_headers;
991 function createReceiptHeaders($receipt) {
993 GLOBAL $data_dir, $username, $identity, $popuser, $domain;
995 $receipt_headers = Array();
996 if (isset($identity) && $identity != 'default') {
997 $from = getPref($data_dir, $username, 'full_name' . $identity);
998 $from_addr = getPref($data_dir, $username, 'email_address' . $identity);
1000 $from = getPref($data_dir, $username, 'full_name');
1001 $from_addr = getPref($data_dir, $username, 'email_address');
1003 if ($from_addr == '') {
1004 $from_addr = $popuser.'@'.$domain;
1008 $from = "<$from_addr>";
1011 $from = '"' . encodeHeader($from) . "\" <$from_addr>";
1016 ||
$receipt == 3 ) {
1017 $receipt_headers["Return-Receipt-To"] = $from;
1021 ||
$receipt == 3 ) {
1023 $receipt_headers["X-Confirm-Reading-To"] = $from;
1025 $receipt_headers["Disposition-Notification-To"] = $from;
1027 return $receipt_headers;
1030 /* Figure out what the 'From:' address is
1033 function getFrom() {
1034 global $username, $popuser, $domain, $data_dir, $identity;
1035 if (isset($identity) && $identity != 'default') {
1036 $from_addr = getPref($data_dir, $username,
1037 'email_address' . $identity);
1040 $from_addr = getPref($data_dir, $username, 'email_address');
1044 $from_addr = "$popuser@$domain";