Use exceptions in Horde_Mime
authorMichael M Slusarz <slusarz@curecanti.org>
Sun, 22 Feb 2009 08:54:11 +0000 (01:54 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Sun, 22 Feb 2009 08:54:11 +0000 (01:54 -0700)
14 files changed:
folks/lib/Folks.php
folks/lib/Report/mail.php
folks/scripts/mail.php
imp/compose-dimp.php
imp/compose-mimp.php
imp/compose.php
imp/lib/Compose.php
imp/lib/Identity/imp.php
imp/lib/Spam.php
imp/lib/UI/Message.php
kronolith/attendees.php
kronolith/lib/Kronolith.php
kronolith/scripts/agenda.php
news/mail.php

index 11bd124..84fe1dc 100644 (file)
@@ -227,9 +227,11 @@ class Folks {
         $mail = new Horde_Mime_Mail($subject, $body, $to, $GLOBALS['conf']['support'], NLS::getCharset());
 
         require_once FOLKS_BASE . '/lib/version.php';
-        $mail->addHeader('User-Agent', 'Folks ' . FOLKS_VERSION);
-        $mail->addHeader('X-Originating-IP', $_SERVER['REMOTE_ADDR']);
-        $mail->addHeader('X-Remote-Browser', $_SERVER['HTTP_USER_AGENT']);
+        try {
+            $mail->addHeader('User-Agent', 'Folks ' . FOLKS_VERSION);
+            $mail->addHeader('X-Originating-IP', $_SERVER['REMOTE_ADDR']);
+            $mail->addHeader('X-Remote-Browser', $_SERVER['HTTP_USER_AGENT']);
+        } catch (Horde_Mime_Exception $e) {}
 
         foreach ($attaches as $file) {
             if (file_exists($file)) {
@@ -239,7 +241,9 @@ class Folks {
 
         list($mail_driver, $mail_params) = Horde::getMailerConfig();
 
-        return $mail->send($mail_driver, $mail_params);
+        try {
+            return $mail->send($mail_driver, $mail_params);
+        } catch (Horde_Mime_Exception $e) {}
     }
 
     /**
index f5a7b95..b5fd0eb 100644 (file)
@@ -34,7 +34,9 @@ class Folks_Report_mail extends Folks_Report {
         $mail = new Horde_Mime_Mail($this->getTitle(), $this->getMessage($message), $to, $this->getUserEmail());
 
         //FIXME: This address should be configurable
-        $mail->addHeader('Sender', 'horde-problem@' . $conf['report_content']['maildomain']);
+        try {
+            $mail->addHeader('Sender', 'horde-problem@' . $conf['report_content']['maildomain']);
+        } catch (Horde_Mime_Exception $e) {}
 
         $mail_driver = $conf['mailer']['type'];
         $mail_params = $conf['mailer']['params'];
@@ -50,6 +52,8 @@ class Folks_Report_mail extends Folks_Report {
             }
         }
 
-        return $mail->send($mail_driver, $mail_params);
+        try {
+            return $mail->send($mail_driver, $mail_params);
+        } catch (Horde_Mime_Exception $e) {}
     }
-}
\ No newline at end of file
+}
index 2aed62b..f582da2 100644 (file)
@@ -128,13 +128,15 @@ while ($row =& $res->fetchRow()) {
     $body2 = sprintf($body, $row[0], $registry->get('name', 'horde'), Folks::getUrlFor('user', $row[0], true, -1));
 
     // Send mail
-    $mail = new MIME_Mail($subject, $body2, $row[1], $conf['support'], NLS::getCharset());
-    $mail->addHeader('User-Agent', 'Folks' . FOLKS_VERSION);
-    $sent = $mail->send($conf['mailer']['type'], $conf['mailer']['params']);
-    if ($sent instanceof PEAR_Error) {
-        $cli->message($sent, 'cli.warning');
-    } else {
+    $mail = new Horde_Mime_Mail($subject, $body2, $row[1], $conf['support'], NLS::getCharset());
+    try {
+        $mail->addHeader('User-Agent', 'Folks' . FOLKS_VERSION);
+    } catch (Horde_Mime_Exception $e) {}
+    try {
+        $mail->send($conf['mailer']['type'], $conf['mailer']['params']);
         $cli->message($row[0], 'cli.success');
+    } catch (Horde_Mime_Exception $e) {
+        $cli->message($e, 'cli.warning');
     }
 
     // sleep(1);
index ed9f9ee..3c0fe7a 100644 (file)
@@ -83,9 +83,10 @@ if (count($_POST)) {
     case 'auto_save_draft':
     case 'save_draft':
         /* Set up the From address based on the identity. */
-        $from = $identity->getFromLine(null, Util::getFormData('from'));
-        if (is_a($from, 'PEAR_Error')) {
-            $notification->push($from);
+        try {
+            $from = $identity->getFromLine(null, Util::getFormData('from'));
+        } catch (Horde_Exception $e) {
+            $notification->push($e);
             break;
         }
         $header['from'] = $from;
@@ -111,9 +112,10 @@ if (count($_POST)) {
         break;
 
     case 'send_message':
-        $from = $identity->getFromLine(null, Util::getFormData('from'));
-        if (is_a($from, 'PEAR_Error')) {
-            $notification->push($from);
+        try {
+            $from = $identity->getFromLine(null, Util::getFormData('from'));
+        } catch (Horde_Exception $e) {
+            $notification->push($e);
             break;
         }
         $header['from'] = $from;
index 08d6337..f95fe96 100644 (file)
@@ -186,7 +186,11 @@ case _("Send"):
         $message .= "\n" . $sig;
     }
 
-    $header['from'] = $identity->getFromLine(null, Util::getFormData('from'));
+    try {
+        $header['from'] = $identity->getFromLine(null, Util::getFormData('from'));
+    } catch (Horde_Exception $e) {
+        $header['from'] = '';
+    }
     $header['replyto'] = $identity->getValue('replyto_addr');
     $header['subject'] = Util::getFormData('subject');
 
index 0f0a024..a43f05a 100644 (file)
@@ -401,13 +401,14 @@ case 'send_message':
         break;
     }
 
-    $from = $identity->getFromLine(null, Util::getFormData('from'));
-    if (is_a($from, 'PEAR_Error')) {
+    try {
+        $header['from'] = $identity->getFromLine(null, Util::getFormData('from'));
+    } catch (Horde_Exception $e) {
+        $header['from'] = '';
         $get_sig = false;
-        $notification->push($from);
+        $notification->push($e);
         break;
     }
-    $header['from'] = $from;
     $header['replyto'] = $identity->getValue('replyto_addr');
 
     $header['to'] = $imp_ui->getAddressList(Util::getFormData('to'), Util::getFormData('to_list'), Util::getFormData('to_field'), Util::getFormData('to_new'));
@@ -491,10 +492,12 @@ case 'save_draft':
     }
 
     /* Set up the From address based on the identity. */
-    $header['from'] = $identity->getFromLine(null, Util::getFormData('from'));
-    if (is_a($header['from'], 'PEAR_Error')) {
+    try {
+        $header['from'] = $identity->getFromLine(null, Util::getFormData('from'));
+    } catch (Horde_Exception $e) {
+        $header['from'] = '';
         $get_sig = false;
-        $notification->push($header['from']);
+        $notification->push($e);
         break;
     }
     foreach (array('to', 'cc', 'bcc', 'subject') as $val) {
@@ -955,7 +958,11 @@ if ($redirect) {
     if ($t->get('di_locked')) {
         $t->set('help_compose-from', Help::link('imp', 'compose-from'));
         $t->set('fromaddr_locked', $prefs->isLocked('from_addr'));
-        $t->set('from', htmlspecialchars($identity->getFromLine(null, Util::getFormData('from'))));
+        try {
+            $t->set('from', htmlspecialchars($identity->getFromLine(null, Util::getFormData('from'))));
+        } catch (Horde_Exception $e) {
+            $t->set('from', '');
+        }
         if (!$t->get('fromaddr_locked')) {
             $t->set('fromaddr_tabindex', ++$tabindex);
         }
index f235ab2..67e20ed 100644 (file)
@@ -192,9 +192,10 @@ class IMP_Compose
             if (!empty($headers[$k])) {
                 $addr = $headers[$k];
                 if ($session) {
-                    $addr_check = Horde_Mime::encodeAddress($this->formatAddr($addr), $charset, $_SESSION['imp']['maildomain']);
-                    if (is_a($addr_check, 'PEAR_Error')) {
-                        throw new IMP_Compose_Exception(sprintf(_("Saving the draft failed. The %s header contains an invalid e-mail address: %s."), $k, $addr_check->getMessage()));
+                    try {
+                        Horde_Mime::encodeAddress($this->formatAddr($addr), $charset, $_SESSION['imp']['maildomain']);
+                    } catch (Horde_Mime_Exception $e) {
+                        throw new IMP_Compose_Exception(sprintf(_("Saving the draft failed. The %s header contains an invalid e-mail address: %s."), $k, $e->getMessage()), $e->getCode());
                     }
                 }
                 $draft_headers->addHeader($v, $addr);
@@ -628,14 +629,16 @@ class IMP_Compose
         global $conf;
 
         /* Properly encode the addresses we're sending to. */
-        $email = Horde_Mime::encodeAddress($email, null, $_SESSION['imp']['maildomain']);
-        if (is_a($email, 'PEAR_Error')) {
-            throw new IMP_Compose_Exception($email);
+        try {
+            $email = Horde_Mime::encodeAddress($email, null, $_SESSION['imp']['maildomain']);
+        } catch (Horde_Mime_Exception $e) {
+            throw new IMP_Compose_Exception($e);
         }
 
         /* Validate the recipient addresses. */
-        $result = Horde_Mime_Address::parseAddressList($email, array('defserver' => $_SESSION['imp']['maildomain'], 'validate' => true));
-        if (empty($result)) {
+        try {
+            $result = Horde_Mime_Address::parseAddressList($email, array('defserver' => $_SESSION['imp']['maildomain'], 'validate' => true));
+        } catch (Horde_Mime_Exception $e) {
             return;
         }
 
@@ -719,10 +722,11 @@ class IMP_Compose
             return;
         }
 
-        $r_array = Horde_Mime::encodeAddress($recipients, null, $_SESSION['imp']['maildomain']);
-        if (!is_a($r_array, 'PEAR_Error')) {
+        try {
+            $r_array = Horde_Mime::encodeAddress($recipients, null, $_SESSION['imp']['maildomain']);
             $r_array = Horde_Mime_Address::parseAddressList($r_array, array('validate' => true));
-        }
+        } catch (Horde_Mime_Exception $e) {}
+
         if (empty($r_array)) {
             $notification->push(_("Could not save recipients."));
             return;
@@ -803,8 +807,9 @@ class IMP_Compose
                     continue;
                 }
 
-                $obs = Horde_Mime_Address::parseAddressList($email);
-                if (empty($obs)) {
+                try {
+                    $obs = Horde_Mime_Address::parseAddressList($email);
+                } catch (Horde_Mime_Exception $e) {
                     throw new IMP_Compose_Exception(sprintf(_("Invalid e-mail address: %s."), $email));
                 }
 
@@ -1839,8 +1844,9 @@ class IMP_Compose
         /* First we'll get a comma seperated list of email addresses
            and a comma seperated list of personal names out of $from
            (there just might be more than one of each). */
-        $addr_list = Horde_Mime_Address::parseAddressList($from);
-        if (!empty($addr_list)) {
+        try {
+            $addr_list = Horde_Mime_Address::parseAddressList($from);
+        } catch (Horde_Mime_Exception $e) {
             $addr_list = array();
         }
 
index 7d44246..bb2e08f 100644 (file)
@@ -126,6 +126,7 @@ class Identity_imp extends Identity
      *
      * @return string  A full From: header in the format
      *                 'Fullname <user@example.com>'.
+     * @throws Horde_Exception
      */
     public function getFromLine($ident = null, $from_address = '')
     {
@@ -144,12 +145,10 @@ class Identity_imp extends Identity
             $name = $this->getFullname($ident);
         }
 
-        if (!empty($address)) {
+        try {
             $ob = Horde_Mime_Address::parseAddressList($address, array('defserver' => $_SESSION['imp']['maildomain']));
-        }
-        if (empty($ob)) {
-            $ob['message'] .= ' ' . _("Your From address is not a valid email address. This can be fixed in your Personal Information options page.");
-            return $ob;
+        } catch (Horde_Mime_Exception $e) {
+            throw new Horde_Exception (_("Your From address is not a valid email address. This can be fixed in your Personal Information options page."));
         }
 
         if (empty($name)) {
@@ -346,7 +345,11 @@ class Identity_imp extends Identity
             if (!is_array($bcc)) {
                 $bcc = array($bcc);
             }
-            return Horde_Mime_Address::parseAddressList(implode(', ', $bcc));
+            try {
+                return Horde_Mime_Address::parseAddressList(implode(', ', $bcc));
+            } catch (Horde_Mime_Exception $e) {
+                return array();
+            }
         }
     }
 
@@ -378,8 +381,9 @@ class Identity_imp extends Identity
             $addresses = array($addresses);
         }
 
-        $addr_list = Horde_Mime_Address::parseAddressList(implode(', ', $addresses));
-        if (empty($addr_list)) {
+        try {
+            $addr_list = Horde_Mime_Address::parseAddressList(implode(', ', $addresses));
+        } catch (Horde_Mime_Exception $e) {
             return null;
         }
 
index 521463d..bb864b0 100644 (file)
@@ -104,7 +104,11 @@ class IMP_Spam
                         require_once 'Horde/Identity.php';
                         $imp_compose = &IMP_Compose::singleton();
                         $identity = &Identity::singleton(array('imp', 'imp'));
-                        $from_line = $identity->getFromLine();
+                        try {
+                            $from_line = $identity->getFromLine();
+                        } catch (Horde_Exception $e) {
+                            $from = '';
+                        }
                     }
 
                     /* Build the MIME structure. */
index a8f8fca..1876a7d 100644 (file)
@@ -82,10 +82,10 @@ class IMP_UI_Message
             } else {
                 /* Send out the MDN now. */
                 $mail_driver = IMP_Compose::getMailDriver();
-                $result = $mdn->generate(false, $confirmed, 'displayed', $mail_driver['driver'], $mail_driver['params']);
-                if (!is_a($result, 'PEAR_Error')) {
+                try {
+                    $mdn->generate(false, $confirmed, 'displayed', $mail_driver['driver'], $mail_driver['params']);
                     IMP_Maillog::log('mdn', $msg_id, 'displayed');
-                }
+                } catch (Horde_Mime_Exception $e) {}
                 if ($GLOBALS['conf']['sentmail']['driver'] != 'none') {
                     $sentmail = IMP_Sentmail::factory();
                     $sentmail->log('mdn', '', $return_addr, !is_a($result, 'PEAR_Error'));
index 841066d..e0429ff 100644 (file)
@@ -77,15 +77,11 @@ case 'add':
             $name = empty($newAttendeeParsedPart->personal)
                 ? ''
                 : $newAttendeeParsedPart->personal;
-            $newAttendeeParsedPartNew = Horde_Mime::encodeAddress(
-                Horde_Mime_Address::writeAddress($newAttendeeParsedPart->mailbox,
-                                         $newAttendeeParsedPart->host, $name));
-            $newAttendeeParsedPartValidated = $parser->parseAddressList(
-                $newAttendeeParsedPartNew, '', null, true);
-            if (is_a($newAttendeeParsedPartValidated, 'PEAR_Error')) {
-                $notification->push($newAttendeeParsedPartValidated,
-                                    'horde.error');
-            } else {
+
+            try {
+                $newAttendeeParsedPartNew = Horde_Mime::encodeAddress(Horde_Mime_Address::writeAddress($newAttendeeParsedPart->mailbox, $newAttendeeParsedPart->host, $name));
+                $newAttendeeParsedPartValidated = $parser->parseAddressList($newAttendeeParsedPartNew, '', null, true);
+
                 $email = $newAttendeeParsedPart->mailbox . '@'
                     . $newAttendeeParsedPart->host;
                 // Avoid overwriting existing attendees with the default
@@ -96,6 +92,8 @@ case 'add':
                         'response'   => KRONOLITH_RESPONSE_NONE,
                         'name'       => $name,
                     );
+            } catch (Horde_Mime_Exception $e) {
+                $notification->push($e, 'horde.error');
             }
         }
     }
index 3ab7dce..d2b15be 100644 (file)
@@ -1897,17 +1897,20 @@ class Kronolith {
             $recipient = empty($status['name']) ? $email : Horde_Mime_Address::trimAddress($status['name'] . ' <' . $email . '>');
             $mail = new Horde_Mime_Mail($subject, $message, $recipient, $from, NLS::getCharset());
             require_once KRONOLITH_BASE . '/lib/version.php';
-            $mail->addHeader('User-Agent', 'Kronolith ' . KRONOLITH_VERSION);
+            try {
+                $mail->addHeader('User-Agent', 'Kronolith ' . KRONOLITH_VERSION);
+            } catch (Horde_Mime_Exception $e) {}
             $mail->addMimePart($ics);
-            $status = $mail->send($mail_driver, $mail_params);
-            if (!is_a($status, 'PEAR_Error')) {
+
+            try {
+                $mail->send($mail_driver, $mail_params);
                 $notification->push(
                     sprintf(_("The event notification to %s was successfully sent."), $recipient),
                     'horde.success'
                 );
-            } else {
+            } catch (Horde_Mime_Exception $e) {
                 $notification->push(
-                    sprintf(_("There was an error sending an event notification to %s: %s"), $recipient, $status->getMessage()),
+                    sprintf(_("There was an error sending an event notification to %s: %s"), $recipient, $e->getMessage(), $e->getCode()),
                     'horde.error'
                 );
             }
@@ -2036,10 +2039,9 @@ class Kronolith {
                                                      NLS::getCharset());
                     $mime_mail->setBody($message, NLS::getCharset(), true);
                     Horde::logMessage(sprintf('Sending event notifications for %s to %s', $event->title, implode(', ', $df_recipients)), __FILE__, __LINE__, PEAR_LOG_DEBUG);
-                    $sent = $mime_mail->send($mail_driver, $mail_params, false, false);
-                    if (is_a($sent, 'PEAR_Error')) {
-                        return $sent;
-                    }
+                    try {
+                        $mime_mail->send($mail_driver, $mail_params, false, false);
+                    } catch (Horde_Mime_Exception $e) {}
                 }
             }
         }
index c03c24f..f2841f7 100755 (executable)
@@ -176,12 +176,13 @@ function send_agendas()
         }
 
         $mime_mail->setBody($message, NLS::getCharset(), true);
-        $mime_mail->addRecipients($email);
+        try {
+            $mime_mail->addRecipients($email);
+        } catch (Horde_Mime_Exception $e) {}
         Horde::logMessage(sprintf('Sending daily agenda to %s', $email),
                           __FILE__, __LINE__, PEAR_LOG_DEBUG);
-        $sent = $mime_mail->send($mail_driver, $mail_params, false, false);
-        if (is_a($sent, 'PEAR_Error')) {
-            return $sent;
-        }
+        try {
+            $mime_mail->send($mail_driver, $mail_params, false, false);
+        } catch (Horde_Mime_Exception $e) {}
     }
 }
index 2ae563c..cddd01a 100644 (file)
@@ -54,12 +54,12 @@ $body = sprintf(_("%s would you like to invite you to read the news\n Title: %s\
                 News::getUrlFor('news', $id, true, -1));
 
 $mail = new Horde_Mime_Mail($row['title'], $body, $to, $from, NLS::getCharset());
-$result = $mail->send($conf['mailer']['type'], $conf['mailer']['params']);
-if ($result instanceof PEAR_Error) {
-    $notification->push($result);
-} else {
+try {
+    $mail->send($conf['mailer']['type'], $conf['mailer']['params']);
     $notification->push(sprintf(_("News succesfully send to %s"), $to), 'horde.success');
+} catch (Horde_Mime_Exception $e) {
+    $notification->push($e);
 }
 
 header('Location: ' . News::getUrlFor('news', $id));
-exit;
\ No newline at end of file
+exit;