dongmen5867 2015-12-18 18:16
浏览 371
已采纳

使用swiftmailer发送多封电子邮件时如何绕过失败的电子邮件?

I've got an email queue in a MySQL database, and via cron I process X unsent emails per minute. The problem I have is that if any specific email fails, it stops execution of the rest of the emails. In one case it was because of an SMTP authentication failure, and email processing basically came to a halt because the attempt to send the failing email kept happening. Are there other ways swiftmailer can fail that I should know about?

I'm wondering what I can do here to make this loop bulletproof? If any single email fails, I'd like to mark the record in the database with an error code (mine or swiftmailers).

Swiftmailer isn't the only way we send email, and the following method is part of a driver. What are some solutions?

public function process_queue()
{
    $result = [... a few mail queue records ...];

    foreach( $result as $params )
    {
        if( ! class_exists( 'Swift', FALSE ) )
        {
            require '/libraries/Mail/swiftmailer/lib/swift_required.php';
        }

        // Prepare transport for sending mail through SMTP
        if( $params['protocol'] == 'smtp' )
        {
            $transport = Swift_SmtpTransport::newInstance( $params['smtp_host'], $params['smtp_port'] )
                ->setUsername( $params['smtp_user'] )
                ->setPassword( $params['smtp_pass'] );
        }

        // Prepare transport for simple mail sending
        else
        {
            $transport = Swift_MailTransport::newInstance();
        }

        // Prepare swiftmailer mailer object
        $mailer = Swift_Mailer::newInstance( $transport );

        // Get a new message instance, and apply its attributes
        $message = Swift_Message::newInstance()
            ->setSubject( $params['subject'] )
            ->setFrom( [ $params['from_email'] => $params['from_name'] ] )
            ->setTo( $params['to'] )
            ->setBody( $params['body'], $params['mailtype'] );

        $mailer->send( $message );
    }
}
  • 写回答

1条回答 默认 最新

  • douci1196 2015-12-18 18:38
    关注

    You could put the send in a try-catch block and handle any exceptions after completion of the loop.

    try {
        $mailer->send($message);
    } catch(Exception $exception) {
        // do something with $exception that contains the error message
    }
    

    Or you can add the second parameter to send and make use of the failures.

    // Pass a variable name to the send() method
    if (!$mailer->send($message, $failures))
    {
      // do something with $failures that contains the error message
    }
    

    Also, Swift will return an error if setTo fails because of an invalid email address, so instead of chaining to build the message you could do each method separately and catch/handle any errors.

    try {
        $message->setTo($params['to']);
    } catch(Swift_RfcComplianceException $e) {
        echo "The email ".$params['to']." seems invalid";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵