Search code examples
phpmailermailing-listswiftmailer

batch email send with response


I want to send out batch mails using SwiftMail or any similar system. The SwiftMailer docs state that:

"Each recipient of the messages receives a different copy with only their own email address on the To: field. An integer is returned which includes the number of successful recipients."

http://swiftmailer.org/docs/batchsend-method

I want to know whether it's possible to find out which email addresses failed, and optionally obtain the error reason/code.


Solution

  • There's a another page in the instructions there that talks about batchsend() failures http://swiftmailer.org/docs/finding-failures and there is an example, and I suspect batchsend will be done exactly the same way.

    $mailer = Swift_Mailer::newInstance( ... );
    
    $message = Swift_Message::newInstance( ... )
      ->setFrom( ... )
      ->setTo(array(
        'receiver@bad-domain.org' => 'Receiver Name',
        'other@domain.org' => 'A name',
        'other-receiver@bad-domain.org' => 'Other Name'
      ))
      ->setBody( ... )
      ;
    
    //Pass a variable name to the send() method
    if (!$mailer->send($message, $failures))
    {
      echo "Failures:";
      print_r($failures);
    }
    
    /*
    Failures:
    Array (
      0 => receiver@bad-domain.org,
      1 => other-receiver@bad-domain.org
    )
    */