From b517af4a471283adc62ef82cb30b97e896bb6799 Mon Sep 17 00:00:00 2001 From: thomascube Date: Wed, 6 Dec 2006 21:37:37 +0000 Subject: Optimized memory usage when sending mail; Include SMTP response in log; Fixed wrong header encoding --- program/include/rcube_smtp.inc | 57 ++++++++++++++++++++++++----------------- program/lib/Mail/mime.php | 2 +- program/lib/Net/SMTP.php | 5 ++-- program/steps/mail/sendmail.inc | 20 ++++++++++----- 4 files changed, 50 insertions(+), 34 deletions(-) (limited to 'program') diff --git a/program/include/rcube_smtp.inc b/program/include/rcube_smtp.inc index 3108675f5..6953e2666 100644 --- a/program/include/rcube_smtp.inc +++ b/program/include/rcube_smtp.inc @@ -49,9 +49,9 @@ $SMTP_CONN = null; * @return bool Returns TRUE on success, or FALSE on error * @access public */ -function smtp_mail($from, $recipients, &$headers, &$body) +function smtp_mail($from, $recipients, &$headers, &$body, &$response) { - global $SMTP_CONN, $CONFIG, $SMTP_ERROR; + global $SMTP_CONN, $CONFIG; $smtp_timeout = null; $smtp_host = $CONFIG['smtp_server']; $smtp_port = is_numeric($CONFIG['smtp_port']) ? $CONFIG['smtp_port'] : 25; @@ -85,7 +85,7 @@ function smtp_mail($from, $recipients, &$headers, &$body) if (PEAR::isError($result)) { $SMTP_CONN = null; - $SMTP_ERROR .= "Connection failed: ".$result->getMessage()."\n"; + $response[] = "Connection failed: ".$result->getMessage(); return FALSE; } @@ -93,22 +93,22 @@ function smtp_mail($from, $recipients, &$headers, &$body) if ($CONFIG['smtp_user'] && $CONFIG['smtp_pass']) { if (strstr($CONFIG['smtp_user'], '%u')) - $smtp_user = str_replace('%u', $_SESSION['username'], $CONFIG['smtp_user']); + $smtp_user = str_replace('%u', $_SESSION['username'], $CONFIG['smtp_user']); else - $smtp_user = $CONFIG['smtp_user']; + $smtp_user = $CONFIG['smtp_user']; - if (strstr($CONFIG['smtp_pass'], '%p')) - $smtp_pass = str_replace('%p', decrypt_passwd($_SESSION['password']), $CONFIG['smtp_pass']); + if (strstr($CONFIG['smtp_pass'], '%p')) + $smtp_pass = str_replace('%p', decrypt_passwd($_SESSION['password']), $CONFIG['smtp_pass']); else - $smtp_pass = $CONFIG['smtp_pass']; + $smtp_pass = $CONFIG['smtp_pass']; - $smtp_auth_type = empty($CONFIG['smtp_auth_type']) ? NULL : $CONFIG['smtp_auth_type']; - $result = $SMTP_CONN->auth($smtp_user, $smtp_pass, $smtp_auth_type); - + $smtp_auth_type = empty($CONFIG['smtp_auth_type']) ? NULL : $CONFIG['smtp_auth_type']; + $result = $SMTP_CONN->auth($smtp_user, $smtp_pass, $smtp_auth_type); + if (PEAR::isError($result)) { smtp_reset(); - $SMTP_ERROR .= "Authentication failure: ".$result->getMessage()."\n"; + $response[] .= "Authentication failure: ".$result->getMessage(); return FALSE; } } @@ -132,7 +132,7 @@ function smtp_mail($from, $recipients, &$headers, &$body) else { smtp_reset(); - $SMTP_ERROR .= "Invalid message headers\n"; + $response[] .= "Invalid message headers"; return FALSE; } @@ -140,7 +140,7 @@ function smtp_mail($from, $recipients, &$headers, &$body) if (!isset($from)) { smtp_reset(); - $SMTP_ERROR .= "No From address has been provided\n"; + $response[] .= "No From address has been provided"; return FALSE; } @@ -149,7 +149,7 @@ function smtp_mail($from, $recipients, &$headers, &$body) if (PEAR::isError($SMTP_CONN->mailFrom($from))) { smtp_reset(); - $SMTP_ERROR .= "Failed to set sender '$from'\n"; + $response[] .= "Failed to set sender '$from'"; return FALSE; } @@ -169,21 +169,30 @@ function smtp_mail($from, $recipients, &$headers, &$body) if (PEAR::isError($SMTP_CONN->rcptTo($recipient))) { smtp_reset(); - $SMTP_ERROR .= "Failed to add recipient '$recipient'\n"; + $response[] .= "Failed to add recipient '$recipient'"; return FALSE; } } + // Concatenate headers and body so it can be passed by reference to SMTP_CONN->data + // so preg_replace in SMTP_CONN->quotedata will store a reference instead of a copy. + // We are still forced to make another copy here for a couple ticks so we don't really + // get to save a copy in the method call. + $data = $text_headers . "\r\n" . $body; + + // unset old vars to save data and so we can pass into SMTP_CONN->data by reference. + unset($text_headers, $body); + // Send the message's headers and the body as SMTP data. - if (PEAR::isError($SMTP_CONN->data("$text_headers\r\n$body"))) + if (PEAR::isError($SMTP_CONN->data($data))) { smtp_reset(); - $SMTP_ERROR .= "Failed to send data\n"; + $response[] .= "Failed to send data"; return FALSE; } - + $response[] = join(': ', $SMTP_CONN->getResponse()); return TRUE; } @@ -318,9 +327,9 @@ function smtp_parse_rfc822($recipients) reset($recipients); while (list($k, $recipient) = each($recipients)) { - $a = explode(" ", $recipient); - while (list($k2, $word) = each($a)) - { + $a = explode(" ", $recipient); + while (list($k2, $word) = each($a)) + { if ((strpos($word, "@") > 0) && (strpos($word, "\"")===false)) { $word = ereg_replace('^<|>$', '', trim($word)); @@ -340,13 +349,13 @@ function smtp_explode_quoted_str($delimiter, $string) if (($key % 2) == 1) $quotes[$key] = str_replace($delimiter, "_!@!_", $quotes[$key]); $string=implode("\"", $quotes); - + $result=explode($delimiter, $string); while (list($key, $val) = each($result)) $result[$key] = str_replace("_!@!_", $delimiter, $result[$key]); return $result; - } + } ?> diff --git a/program/lib/Mail/mime.php b/program/lib/Mail/mime.php index 96d7025d8..a9f798ed8 100644 --- a/program/lib/Mail/mime.php +++ b/program/lib/Mail/mime.php @@ -820,7 +820,7 @@ class Mail_mime if (preg_match('#[\x80-\xFF]{1}#', $value)) { $suffix = ''; // Don't encode e-mail address - if (preg_match('/(.+)\s(<.+@[a-z0-9\-\.]+\.[a-z]{2,5}>)$/Ui', $value, $matches)) { + if (preg_match('/(.+)\s(<.+@[a-z0-9\-\.]+>)$/Ui', $value, $matches)) { $value = $matches[1]; $suffix = ' '.$matches[2]; } diff --git a/program/lib/Net/SMTP.php b/program/lib/Net/SMTP.php index 4a29f4d0a..bda1494d8 100644 --- a/program/lib/Net/SMTP.php +++ b/program/lib/Net/SMTP.php @@ -760,7 +760,7 @@ class Net_SMTP * @access public * @since 1.0 */ - function data($data) + function data(&$data) { /* RFC 1870, section 3, subsection 3 states "a value of zero * indicates that no fixed maximum message size is in force". @@ -784,7 +784,8 @@ class Net_SMTP return $error; } - if (PEAR::isError($result = $this->_send($data . "\r\n.\r\n"))) { + $data .= "\r\n.\r\n"; + if (PEAR::isError($result = $this->_send($data))) { return $result; } if (PEAR::isError($error = $this->_parseResponse(250))) { diff --git a/program/steps/mail/sendmail.inc b/program/steps/mail/sendmail.inc index 41735881c..98f413c85 100644 --- a/program/steps/mail/sendmail.inc +++ b/program/steps/mail/sendmail.inc @@ -167,7 +167,7 @@ if (empty($identity_arr['string'])) // compose headers array $headers = array('Date' => date('D, j M Y H:i:s O'), - 'From' => $identity_arr['string'], + 'From' => rcube_charset_convert($identity_arr['string'], $CHARSET, $message_charset), 'To' => rcube_charset_convert($mailto, $input_charset, $message_charset)); // additional recipients @@ -295,12 +295,17 @@ $message_param = array('text_encoding' => $transfer_encoding, 'text_charset' => $message_charset); // compose message body and get headers -$msg_body = &$MAIL_MIME->get($message_param); +$msg_body = $MAIL_MIME->get($message_param); +// unset to save memory. +unset($MAIL_MIME->_parts); $msg_subject = $headers['Subject']; if ($MBSTRING && function_exists("mb_encode_mimeheader")) - $headers['Subject'] = mb_encode_mimeheader($headers['Subject'], $message_charset); +{ + mb_internal_encoding($CHARSET); + $headers['Subject'] = mb_encode_mimeheader($headers['Subject'], $message_charset, 'Q'); +} // Begin SMTP Delivery Block if (!$savedraft) { @@ -324,7 +329,8 @@ if (!$savedraft) { $header_str = $MAIL_MIME->txtHeaders($send_headers); // send message - $sent = smtp_mail($from, $a_recipients, $header_str, $msg_body); + $smtp_response = array(); + $sent = smtp_mail($from, $a_recipients, $header_str, $msg_body, $smtp_response); // log error if (!$sent) @@ -333,7 +339,7 @@ if (!$savedraft) { 'type' => 'smtp', 'line' => __LINE__, 'file' => __FILE__, - 'message' => "SMTP error: $SMTP_ERROR"), TRUE, FALSE); + 'message' => "SMTP error: ".join("\n", $smtp_response)), TRUE, FALSE); } } @@ -446,12 +452,12 @@ else { if ($CONFIG['smtp_log']) { - $log_entry = sprintf("[%s] User: %d on %s; Message for %s; Subject: %s\n", + $log_entry = sprintf("[%s] User: %d on %s; Message for %s; %s\n", date("d-M-Y H:i:s O", mktime()), $_SESSION['user_id'], $_SERVER['REMOTE_ADDR'], $mailto, - $msg_subject); + !empty($smtp_response) ? join('; ', $smtp_response) : ''); if ($fp = @fopen($CONFIG['log_dir'].'/sendmail', 'a')) { -- cgit v1.2.3