2

I tried several ways but none works including one that is in StackOverFlow -> Swiftmailer TransportException get uncaught by try catch block.

I need to catch the exceptions that can be generated in the block in my class who runs an instance of the class Swiftmailer to send email

 /**
 * @throws Be msut catch swiftmailer exception Swift_TransportException because ->setTo($send_to) containt '' (not valid email sender)
 * But not catch my code.
 */

 $transport = \Swift_SmtpTransport::newInstance()
            ->setHost($settings['smtp_host'])
            ->setPort($settings['smtp_port'])
            ->setEncryption($settings['smtp_security'])
            ->setUsername($settings['smtp_user'])
            ->setPassword($settings['smtp_pass']);
        $mailer = \Swift_Mailer::newInstance($transport);

    echo "Verify code\n";
    try {
        $message = \Swift_Message::newInstance()
            ->setSubject($subject)
            ->setFrom(array($settings['smtp_mail_sender'] => $settings['smtp_name_sender']))
            ->setTo($send_to)
            ->setBody($msg);
        $result = $mailer->send($message);
    } catch (\Swift_TransportException $Ste) {
        echo "EROORRRRRRRRRRRRRRRRRRRR\n\n\n";
    }

Show error when run on console

php -q cprsyncbackup.php -option param
No llegue
PHP Fatal error:  Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [] does not comply with RFC 2822, 3.6.2.' in /Volumes/iDatos/abkrim/ownCloudTDC/tamainut/desarrollo/cprsync/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php:348
Stack trace:
#0 /Volumes/iDatos/abkrim/ownCloudTDC/tamainut/desarrollo/cprsync/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php(263): Swift_Mime_Headers_MailboxHeader->_assertValidAddress('')
#1 /Volumes/iDatos/abkrim/ownCloudTDC/tamainut/desarrollo/cprsync/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php(106): Swift_Mime_Headers_MailboxHeader->normalizeMailboxes(Array)
#2 /Volumes/iDatos/abkrim/ownCloudTDC/tamainut/desarrollo/cprsync/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php(63): Swift_Mime_Headers_MailboxHeader->setNameAddresses(Array)
#3 /Volumes/iDatos/abkrim/ownCloudTDC/tamainut/desarrollo/cprsync/vendor/swiftmailer/swiftmailer/li in /Volumes/iDatos/abkrim/ownCloudTDC/tamainut/desarrollo/cprsync/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php on line 348
Community
  • 1
  • 1
abkrim
  • 3,512
  • 7
  • 43
  • 69

1 Answers1

7

You're wrapping the try ... catch-block around the wrong part of your composition of the swiftmailer mail.

Excerpt from the manual:

If you add recipients automatically based on a data source that may contain invalid email addresses, you can prevent possible exceptions by validating the addresses using Swift_Validate::email($email) and only adding addresses that validate. Another way would be to wrap your setTo(), setCc() and setBcc() calls in a try-catch block and handle the Swift_RfcComplianceException in the catch block.

Therefore you should use it while adding addresses to your Swift_Message-object, like this:

$message = Swift_Message::newInstance();

// add some message composing here...

$email = "somewrongadress.org";
try {
    $message->setTo(array($email));
} catch(Swift_RfcComplianceException $e) {
    echo "Address ".$email." seems invalid";
}

/* and now your transport... */
try {
    $result = $mailer->send($message);
} catch (\Swift_TransportException $Ste) {
    echo "EROORRRRRRRRRRRRRRRRRRRR\n\n\n";
}

That should do the job.
Bjoern
  • 15,934
  • 4
  • 43
  • 48
  • For me work fine but after read your tip, I prefer create my own excerpt with Swift_Validate::emaIL($EMAIL) – abkrim Aug 17 '15 at 16:31