gpt4 book ai didi

php - 使用 Swiftmailer 捕获 block 异常不起作用

转载 作者:行者123 更新时间:2023-12-02 15:23:56 29 4
gpt4 key购买 nike

我尝试了几种方法,但都没有用,包括 StackOverFlow -> Swiftmailer TransportException get uncaught by try catch block 中的一种.

我需要捕获可以在我的类中的 block 中生成的异常,该类运行类 Swiftmailer 的实例来发送电子邮件

 /**
* @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";
}

在控制台运行时显示错误

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

最佳答案

您将 try ... catch-block 包裹在您编写 swiftmailer 邮件的错误部分。

摘自 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.

因此,您应该在将地址添加到您的 Swift_Message 对象时使用它,如下所示:

$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.

关于php - 使用 Swiftmailer 捕获 block 异常不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32023499/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com