0

I am sending mails with Laravel like this:

foreach ($users as $user) {
   \Mail::to($user())->send(new Newsletter($user));
}

I would like to have an array of all the users who had a bad_domain response. I found in the docs that Laravel uses Swiftmailer which has a way to find bad_domain respones:

// 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
)
*/

However, I want to use the a Mailable class. I am not sure how I can do this with the Swiftmailer (which I can access through \Mail::getSwiftMailer()).

Is there any easy way of getting the bad_domains when using Mailable from Laravel?

Adam
  • 25,960
  • 22
  • 158
  • 247

1 Answers1

0

You may only access bad_domains, but not bounces with Swiftmailer (Swiftmailer 4 does not retrieve bounces as $failedRecipients).

One can get bad_domains it with

\Mail::to($user)->send(new \App\Mail\Hi());

dd(\Mail::failures());

See Illuminate\Mail\Mailer.php

  /**
     * Send a Swift Message instance.
     *
     * @param  \Swift_Message  $message
     * @return void
     */
    protected function sendSwiftMessage($message)
    {
        try {
            return $this->swift->send($message, $this->failedRecipients);
        } finally {
            $this->forceReconnection();
        }
    }
Adam
  • 25,960
  • 22
  • 158
  • 247