From 4f5ec65a36a7e360209d886d5c8db01fba19aae5 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Wed, 7 Dec 2016 10:39:35 +0100 Subject: [PATCH] Close SMTP connection ASAP after the message has been sent We should not keep the connection open until the script shuts down. It may take long time e.g. to save the message in IMAP. --- program/lib/Roundcube/rcube.php | 8 +++++++- program/steps/mail/func.inc | 2 +- program/steps/mail/sendmail.inc | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/program/lib/Roundcube/rcube.php b/program/lib/Roundcube/rcube.php index 4ac54072e..977f7784a 100644 --- a/program/lib/Roundcube/rcube.php +++ b/program/lib/Roundcube/rcube.php @@ -1578,10 +1578,12 @@ class rcube * @param string $body_file Location of file with saved message body (reference), * used when delay_file_io is enabled * @param array $options SMTP options (e.g. DSN request) + * @param bool $disconnect Close SMTP connection ASAP * * @return boolean Send status. */ - public function deliver_message(&$message, $from, $mailto, &$error, &$body_file = null, $options = null) + public function deliver_message(&$message, $from, $mailto, &$error, + &$body_file = null, $options = null, $disconnect = false) { $plugin = $this->plugins->exec_hook('message_before_send', array( 'message' => $message, @@ -1681,6 +1683,10 @@ class rcube fclose($msg_body); } + if ($disconnect) { + $this->smtp->disconnect(); + } + $message->headers($headers, true); return $sent; diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc index 3f9503afa..19d7e7a8a 100644 --- a/program/steps/mail/func.inc +++ b/program/steps/mail/func.inc @@ -1910,7 +1910,7 @@ function rcmail_send_mdn($message, &$smtp_error) // SMTP options $options = array('mdn_use_from' => (bool) $RCMAIL->config->get('mdn_use_from')); - $sent = $RCMAIL->deliver_message($compose, $identity['email'], $mailto, $smtp_error, $body_file, $options); + $sent = $RCMAIL->deliver_message($compose, $identity['email'], $mailto, $smtp_error, $body_file, $options, true); if ($sent) { $RCMAIL->storage->set_flag($message->uid, 'MDNSENT'); diff --git a/program/steps/mail/sendmail.inc b/program/steps/mail/sendmail.inc index e5e461c08..afaa7ac84 100644 --- a/program/steps/mail/sendmail.inc +++ b/program/steps/mail/sendmail.inc @@ -537,7 +537,7 @@ if (!$savedraft && !$saveonly) { $smtp_opts['dsn'] = $dsn_enabled; $sent = $RCMAIL->deliver_message($MAIL_MIME, $from, $mailto, - $smtp_error, $mailbody_file, $smtp_opts); + $smtp_error, $mailbody_file, $smtp_opts, true); // return to compose page if sending failed if (!$sent) {