From 10adeb76533751d8f9dd78c5b27e8e0828e1d5b1 Mon Sep 17 00:00:00 2001 From: pdontthink Date: Wed, 13 Feb 2008 03:56:19 +0000 Subject: [PATCH] Change approach for making things like Message-ID available after sending. Tracker #1322898 inspired me to do what I was originally thinking, even though this shows SM's design weaknesses. I also now guarantee that the Message-ID and Date headers will be the same between that sent to the recipient and that stored in the Sent folder. git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@12940 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- class/deliver/Deliver.class.php | 74 ++++++++++++++++++++------------- src/compose.php | 14 +++++-- 2 files changed, 54 insertions(+), 34 deletions(-) diff --git a/class/deliver/Deliver.class.php b/class/deliver/Deliver.class.php index 2f8e6a31..e9038fdd 100644 --- a/class/deliver/Deliver.class.php +++ b/class/deliver/Deliver.class.php @@ -28,18 +28,16 @@ */ class Deliver { - /** - * Most recently calculated Message-ID - * External code should NEVER access this directly! - * @var string - */ - var $message_id; - /** * function mail - send the message parts to the SMTP stream * * @param Message $message Message object to send - * @param resource $stream Handle to the SMTP stream + * NOTE that this is passed by + * reference and will be modified + * upon return with updated + * fields such as Message ID, References, + * In-Reply-To and Date headers. + * @param resource $stream Handle to the outgoing stream * (when FALSE, nothing will be * written to the stream; this can * be used to determine the actual @@ -59,17 +57,15 @@ class Deliver { * an overloaded version of this method * if needed. * - * @return array An array containing at least these elements in this order: - * - The number of bytes written (or that would have been - * written) to the output stream - * - The message ID (WARNING: if $stream is FALSE, this - * may not be supplied, or may not be accurate) + * @return integer The number of bytes written (or that would have been + * written) to the output stream. * */ - function mail($message, $stream=false, $reply_id=0, $reply_ent_id=0, + function mail(&$message, $stream=false, $reply_id=0, $reply_ent_id=0, $extra=NULL) { - $rfc822_header = $message->rfc822_header; + $rfc822_header = &$message->rfc822_header; + if (count($message->entities)) { $boundary = $this->mimeBoundary(); $rfc822_header->content_type->properties['boundary']='"'.$boundary.'"'; @@ -115,7 +111,7 @@ class Deliver { $this->send_mail($message, $header, $boundary, $stream, $raw_length, $extra); - return array($raw_length, $this->message_id); + return $raw_length; } /** @@ -487,7 +483,7 @@ class Deliver { * * @return string $header */ - function prepareRFC822_Header($rfc822_header, $reply_rfc822_header, &$raw_length) { + function prepareRFC822_Header(&$rfc822_header, $reply_rfc822_header, &$raw_length) { global $domain, $username, $encode_header_key, $edit_identity, $hide_auth_header; @@ -510,16 +506,18 @@ class Deliver { $date = date('D, j M Y H:i:s ', time()) . $this->timezone(); /* Create a message-id */ - $message_id = '<'; - /* user-specifc data to decrease collision chance */ - $seed_data = $username . '.'; - $seed_data .= (!empty($REMOTE_PORT) ? $REMOTE_PORT . '.' : ''); - $seed_data .= (!empty($REMOTE_ADDR) ? $REMOTE_ADDR . '.' : ''); - /* add the current time in milliseconds and randomness */ - $seed_data .= uniqid(mt_rand(),true); - /* put it through one-way hash and add it to the ID */ - $message_id .= md5($seed_data) . '.squirrel@' . $SERVER_NAME .'>'; - $this->message_id = $message_id; + $message_id = 'MESSAGE ID GENERATION ERROR! PLEASE CONTACT SQUIRRELMAIL DEVELOPERS'; + if (empty($rfc822_header->message_id)) { + $message_id = '<'; + /* user-specifc data to decrease collision chance */ + $seed_data = $username . '.'; + $seed_data .= (!empty($REMOTE_PORT) ? $REMOTE_PORT . '.' : ''); + $seed_data .= (!empty($REMOTE_ADDR) ? $REMOTE_ADDR . '.' : ''); + /* add the current time in milliseconds and randomness */ + $seed_data .= uniqid(mt_rand(),true); + /* put it through one-way hash and add it to the ID */ + $message_id .= md5($seed_data) . '.squirrel@' . $SERVER_NAME .'>'; + } /* Make an RFC822 Received: line */ if (isset($REMOTE_HOST)) { @@ -552,6 +550,7 @@ class Deliver { */ $show_sm_header = ( defined('hide_squirrelmail_header') ? ! hide_squirrelmail_header : 1 ); + // FIXME: The following headers may generate slightly differently between the message sent to the destination and that stored in the Sent folder because this code will be called before both actions. This is not necessarily a big problem, but other headers such as Message-ID and Date are preserved between both actions if ( $show_sm_header ) { if (isset($encode_header_key) && trim($encode_header_key)!='') { @@ -571,17 +570,32 @@ class Deliver { } /* Insert the rest of the header fields */ - $header[] = 'Message-ID: '. $message_id . $rn; + + if (!empty($rfc822_header->message_id)) { + $header[] = 'Message-ID: '. $rfc822_header->message_id . $rn; + } else { + $header[] = 'Message-ID: '. $message_id . $rn; + $rfc822_header->message_id = $message_id; + } + if (is_object($reply_rfc822_header) && isset($reply_rfc822_header->message_id) && $reply_rfc822_header->message_id) { $rep_message_id = $reply_rfc822_header->message_id; - // $this->strip_crlf($message_id); $header[] = 'In-Reply-To: '.$rep_message_id . $rn; + $rfc822_header->in_reply_to = $rep_message_id; $references = $this->calculate_references($reply_rfc822_header); $header[] = 'References: '.$references . $rn; + $rfc822_header->references = $references; } - $header[] = "Date: $date" . $rn; + + if (!empty($rfc822_header->date) && $rfc822_header->date != -1) { + $header[] = 'Date: '. $rfc822_header->date . $rn; + } else { + $header[] = "Date: $date" . $rn; + $rfc822_header->date = $date; + } + $header[] = 'Subject: '.encodeHeader($rfc822_header->subject) . $rn; $header[] = 'From: '. $rfc822_header->getAddr_s('from',",$rn ",true) . $rn; diff --git a/src/compose.php b/src/compose.php index 302f2f03..685a9706 100644 --- a/src/compose.php +++ b/src/compose.php @@ -1470,13 +1470,19 @@ function getByteSize($ini_size) { * and conf.pl should show a list of available backends. * The message also should be constructed by the message class. * + * @param object $composeMessage The message being sent. Please note + * that it is passed by reference and + * will be returned modified, with additional + * headers, such as Message-ID, Date, In-Reply-To, + * References, and so forth. + * * @return boolean FALSE if delivery failed, or some non-FALSE value * upon success. * */ -function deliverMessage($composeMessage, $draft=false) { +function deliverMessage(&$composeMessage, $draft=false) { global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body, - $username, $identity, $idents, $data_dir, $message_id, + $username, $identity, $idents, $data_dir, $request_mdn, $request_dr, $default_charset, $useSendmail, $domain, $action, $default_move_to_sent, $move_to_sent, $imapServerAddress, $imapPort, $sent_folder, $key; @@ -1600,7 +1606,7 @@ function deliverMessage($composeMessage, $draft=false) { if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) { require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php'); $imap_deliver = new Deliver_IMAP(); - list($success, $ignore) = $imap_deliver->mail($composeMessage, $imap_stream, $reply_id, $reply_ent_id, $draft_folder); + $success = $imap_deliver->mail($composeMessage, $imap_stream, $reply_id, $reply_ent_id, $draft_folder); sqimap_logout($imap_stream); unset ($imap_deliver); $composeMessage->purgeAttachments(); @@ -1613,7 +1619,7 @@ function deliverMessage($composeMessage, $draft=false) { } $success = false; if ($stream) { - list($ignore, $message_id) = $deliver->mail($composeMessage, $stream, $reply_id, $reply_ent_id); + $deliver->mail($composeMessage, $stream, $reply_id, $reply_ent_id); $success = $deliver->finalizeStream($stream); } if (!$success) { -- 2.25.1