From 2818f8e9ed5e8853e33636ac8efe09e2a4161618 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Mon, 06 Jul 2009 05:13:10 -0400
Subject: [PATCH] - Show SMTP errors in browser (#1485927)

---
 skins/default/common.css                |    2 -
 CHANGELOG                               |    1 
 program/localization/en_US/messages.inc |    6 +++
 program/steps/mail/func.inc             |    8 ++--
 program/lib/Net/SMTP.php                |    2 
 program/include/rcube_smtp.inc          |   23 +++++++----
 program/localization/pl_PL/messages.inc |    6 +++
 program/steps/mail/sendmail.inc         |    7 ++-
 program/steps/mail/sendmdn.inc          |    5 ++
 program/localization/en_GB/messages.inc |    6 +++
 10 files changed, 47 insertions(+), 19 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 743304d..2e94ea3 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG RoundCube Webmail
 ===========================
 
+- Show SMTP errors in browser (#1485927)
 - Allow WBR tag in HTML message (#1485960)
 - Use spl_autoload_register() instead of __autoload (#1485947)
 - Add hook for identities listing (#1485958)
diff --git a/program/include/rcube_smtp.inc b/program/include/rcube_smtp.inc
index b5bd183..066e5ed 100644
--- a/program/include/rcube_smtp.inc
+++ b/program/include/rcube_smtp.inc
@@ -49,7 +49,7 @@
  *
  * @return bool  Returns TRUE on success, or FALSE on error
  */
-function smtp_mail($from, $recipients, &$headers, &$body, &$response)
+function smtp_mail($from, $recipients, &$headers, &$body, &$response, &$error)
   {
   global $SMTP_CONN, $RCMAIL;
   
@@ -96,8 +96,9 @@
     $result = $SMTP_CONN->connect($smtp_timeout);
     if (PEAR::isError($result))
       {
-      $SMTP_CONN = null;
       $response[] = "Connection failed: ".$result->getMessage();
+      $error = array('label' => 'smtpconnerror', 'vars' => array('code' => $SMTP_CONN->_code));
+      $SMTP_CONN = null;
       return FALSE;
       }
       
@@ -119,8 +120,9 @@
     
       if (PEAR::isError($result))
         {
-        smtp_reset();
+        $error = array('label' => 'smtpautherror', 'vars' => array('code' => $SMTP_CONN->_code));
         $response[] .= 'Authentication failure: ' . $result->getMessage() . ' (Code: ' . $result->getCode() . ')';
+        smtp_reset();
         return FALSE;
         }
       }
@@ -160,8 +162,9 @@
   // set From: address
   if (PEAR::isError($SMTP_CONN->mailFrom($from)))
     {
-    smtp_reset();
+    $error = array('label' => 'smtpfromerror', 'vars' => array('from' => $from, 'code' => $SMTP_CONN->_code));
     $response[] .= "Failed to set sender '$from'";
+    smtp_reset();
     return FALSE;
     }
 
@@ -170,6 +173,7 @@
   $recipients = smtp_parse_rfc822($recipients);
   if (PEAR::isError($recipients))
     {
+    $error = array('label' => 'smtprecipientserror');
     smtp_reset();
     return FALSE;
     }
@@ -180,8 +184,9 @@
     {
     if (PEAR::isError($SMTP_CONN->rcptTo($recipient)))
       {
-      smtp_reset();
+      $error = array('label' => 'smtptoerror', 'vars' => array('to' => $recipient, 'code' => $SMTP_CONN->_code));
       $response[] .= "Failed to add recipient '$recipient'";
+      smtp_reset();
       return FALSE;
       }
     }
@@ -197,10 +202,11 @@
   unset($text_headers, $body);
    
   // Send the message's headers and the body as SMTP data.
-  if (PEAR::isError($SMTP_CONN->data($data)))
+  if (PEAR::isError($result = $SMTP_CONN->data($data)))
     {
-    smtp_reset();
+    $error = array('label' => 'smtperror', 'vars' => array('msg' => $result->getMessage()));
     $response[] .= "Failed to send data";
+    smtp_reset();
     return FALSE;
     }
 
@@ -218,13 +224,12 @@
   {
   global $SMTP_CONN;
 
-  if (is_object($SMTP_CONN))
+  if (is_object($SMTP_CONN) && is_resource($SMTP_CONN->_socket->fp))
     {
     $SMTP_CONN->rset();
     smtp_disconnect();
     }
   }
-
 
 
 /**
diff --git a/program/lib/Net/SMTP.php b/program/lib/Net/SMTP.php
index 550383e..1248a32 100644
--- a/program/lib/Net/SMTP.php
+++ b/program/lib/Net/SMTP.php
@@ -856,7 +856,7 @@
         if (isset($this->_esmtp['SIZE']) && ($this->_esmtp['SIZE'] > 0)) {
             if (strlen($data) >= $this->_esmtp['SIZE']) {
                 $this->disconnect();
-                return PEAR::raiseError('Message size excedes the server limit');
+                return PEAR::raiseError('Message size exceedes the server limit');
             }
         }
 
diff --git a/program/localization/en_GB/messages.inc b/program/localization/en_GB/messages.inc
index 137214b..0552591 100644
--- a/program/localization/en_GB/messages.inc
+++ b/program/localization/en_GB/messages.inc
@@ -97,5 +97,11 @@
 $messages['nofromaddress'] = 'Missing e-mail address in selected identity';
 $messages['editorwarning'] = 'Switching to the plain text editor will cause all text formatting to be lost. Do you wish to continue?';
 $messages['httpreceivedencrypterror'] = 'A fatal configuration error occurred. Contact your administrator immediately. <b>Your message can not be sent.</b>';
+$messages['smtpconnerror'] = 'SMTP Error ($code): Connection to server failed';
+$messages['smtpautherror'] = 'SMTP Error ($code): Authentication failed';
+$messages['smtpfromerror'] = 'SMTP Error ($code): Failed to set sender "$from"';
+$messages['smtptoerror'] = 'SMTP Error ($code): Failed to add recipient "$to"';
+$messages['smtprecipientserror'] = 'SMTP Error: Unable to parse recipients list';
+$messages['smtperror'] = 'SMTP Error: $msg';
 
 ?>
diff --git a/program/localization/en_US/messages.inc b/program/localization/en_US/messages.inc
index ed233b1..fa0b307 100644
--- a/program/localization/en_US/messages.inc
+++ b/program/localization/en_US/messages.inc
@@ -97,5 +97,11 @@
 $messages['nofromaddress'] = 'Missing e-mail address in selected identity';
 $messages['editorwarning'] = 'Switching to the plain text editor will cause all text formatting to be lost. Do you wish to continue?';
 $messages['httpreceivedencrypterror'] = 'A fatal configuration error occurred. Contact your administrator immediately. <b>Your message can not be sent.</b>';
+$messages['smtpconnerror'] = 'SMTP Error ($code): Connection to server failed';
+$messages['smtpautherror'] = 'SMTP Error ($code): Authentication failed';
+$messages['smtpfromerror'] = 'SMTP Error ($code): Failed to set sender "$from"';
+$messages['smtptoerror'] = 'SMTP Error ($code): Failed to add recipient "$to"';
+$messages['smtprecipientserror'] = 'SMTP Error: Unable to parse recipients list';
+$messages['smtperror'] = 'SMTP Error: $msg';
 
 ?>
diff --git a/program/localization/pl_PL/messages.inc b/program/localization/pl_PL/messages.inc
index 53926a2..1688154 100644
--- a/program/localization/pl_PL/messages.inc
+++ b/program/localization/pl_PL/messages.inc
@@ -102,5 +102,11 @@
 $messages['nofromaddress'] = 'Brak adresu e-mail w wybranej tożsamości';
 $messages['editorwarning'] = 'Zmiana edytora spowoduje utratę formatowania tekstu. Czy jesteś pewien, że chcesz to zrobić?';
 $messages['httpreceivedencrypterror'] = 'Wystąpił błąd krytyczny. Skontaktuj się z administratorem. <b>Nie można wysłać wiadomości.</b>';
+$messages['smtpconnerror'] = 'Błąd SMTP ($code): Nie można nawiązać połączenia z serwerem';
+$messages['smtpautherror'] = 'Błąd SMTP ($code): Uwierzytelnianie nie powiodło się';
+$messages['smtpfromerror'] = 'Błąd SMTP ($code): Nie można ustawić nadawcy "$from"';
+$messages['smtptoerror'] = 'Błąd SMTP ($code): Nie można dodać odbiorcy "$to"';
+$messages['smtprecipientserror'] = 'Błąd SMTP: Parsowanie listy odbiorców nie powiodło się';
+$messages['smtperror'] = 'Błąd SMTP: $msg';
 
 ?>
diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc
index e7eb4b3..a942c1a 100644
--- a/program/steps/mail/func.inc
+++ b/program/steps/mail/func.inc
@@ -1302,7 +1302,7 @@
 /**
  * Send the given message compose object using the configured method
  */
-function rcmail_deliver_message(&$message, $from, $mailto)
+function rcmail_deliver_message(&$message, $from, $mailto, &$smtp_error)
 {
   global $CONFIG, $RCMAIL;
 
@@ -1327,7 +1327,7 @@
 
     // send message
     $smtp_response = array();
-    $sent = smtp_mail($from, $a_recipients, ($foo = $message->txtHeaders($send_headers, true)), $msg_body, $smtp_response);
+    $sent = smtp_mail($from, $a_recipients, ($foo = $message->txtHeaders($send_headers, true)), $msg_body, $smtp_response, $smtp_error);
 
     // log error
     if (!$sent)
@@ -1380,7 +1380,7 @@
 }
 
 
-function rcmail_send_mdn($uid)
+function rcmail_send_mdn($uid, &$smtp_error)
 {
   global $RCMAIL, $IMAP;
 
@@ -1438,7 +1438,7 @@
     $compose->setTXTBody(rc_wordwrap($body, 75, "\r\n"));
     $compose->addAttachment($report, 'message/disposition-notification', 'MDNPart2.txt', false, '7bit', 'inline');
 
-    $sent = rcmail_deliver_message($compose, $identity['email'], $mailto);
+    $sent = rcmail_deliver_message($compose, $identity['email'], $mailto, $smtp_error);
 
     if ($sent)
     {
diff --git a/program/steps/mail/sendmail.inc b/program/steps/mail/sendmail.inc
index d550a42..c5a2ea7 100644
--- a/program/steps/mail/sendmail.inc
+++ b/program/steps/mail/sendmail.inc
@@ -441,12 +441,15 @@
     $OUTPUT->send('iframe'); 
   }
 
-  $sent = rcmail_deliver_message($MAIL_MIME, $from, $mailto);
+  $sent = rcmail_deliver_message($MAIL_MIME, $from, $mailto, $smtp_error);
   
   // return to compose page if sending failed
   if (!$sent)
     {
-    $OUTPUT->show_message("sendingfailed", 'error'); 
+    if ($smtp_error)
+      $OUTPUT->show_message($smtp_error['label'], 'error', $smtp_error['vars']); 
+    else
+      $OUTPUT->show_message('sendingfailed', 'error'); 
     $OUTPUT->send('iframe');
     }
 
diff --git a/program/steps/mail/sendmdn.inc b/program/steps/mail/sendmdn.inc
index 3d7755c..c3294e7 100644
--- a/program/steps/mail/sendmdn.inc
+++ b/program/steps/mail/sendmdn.inc
@@ -21,7 +21,7 @@
 
 
 if (!empty($_POST['_uid'])) {
-  $sent = rcmail_send_mdn(get_input_value('_uid', RCUBE_INPUT_POST));
+  $sent = rcmail_send_mdn(get_input_value('_uid', RCUBE_INPUT_POST), $smtp_error);
 }
 
 // show either confirm or error message
@@ -29,6 +29,9 @@
   $OUTPUT->set_env('mdn_request', false);
   $OUTPUT->show_message('receiptsent', 'confirmation');
 }
+else if ($smtp_error) {
+  $OUTPUT->show_message($smtp_error['label'], 'error', $smtp_error['vars']);
+}
 else {
   $OUTPUT->show_message('errorsendingreceipt', 'error');
 }
diff --git a/skins/default/common.css b/skins/default/common.css
index 8fb992a..3e971aa 100644
--- a/skins/default/common.css
+++ b/skins/default/common.css
@@ -185,7 +185,6 @@
   background-position: 0 -75px;
 }
 
-
 #message
 {
   position: absolute;
@@ -201,7 +200,6 @@
 {
   width: 400px;
   margin: 0px auto;
-  height: 22px;
   min-height: 22px;
   padding: 8px 10px 8px 46px;
 }

--
Gitblit v1.9.1