13

I'm using the new mail class in Laravel 4, does anybody know how to check if the email was sent? At least that the mail was successfully handed over to the MTA...

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
ebelendez
  • 848
  • 2
  • 12
  • 22
  • Yeah cant you just go to app->config->mail and change pretend=>true?? That way you can view the messages in the log – Kylie Jun 11 '13 at 02:23

3 Answers3

11

If you do

if ( ! Mail::send(array('text' => 'view'), $data, $callback) )
{
   return View::make('errors.sendMail');
}

You will know when it was sent or not, but it could be better, because SwiftMailer knows to wich recipients it failed, but Laravel is not exposing the related parameter to help us get that information:

/**
 * Send the given Message like it would be sent in a mail client.
 *
 * All recipients (with the exception of Bcc) will be able to see the other
 * recipients this message was sent to.
 *
 * Recipient/sender data will be retrieved from the Message object.
 *
 * The return value is the number of recipients who were accepted for
 * delivery.
 *
 * @param Swift_Mime_Message $message
 * @param array              $failedRecipients An array of failures by-reference
 *
 * @return integer
 */
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
    $failedRecipients = (array) $failedRecipients;

    if (!$this->_transport->isStarted()) {
        $this->_transport->start();
    }

    $sent = 0;

    try {
        $sent = $this->_transport->send($message, $failedRecipients);
    } catch (Swift_RfcComplianceException $e) {
        foreach ($message->getTo() as $address => $name) {
            $failedRecipients[] = $address;
        }
    }

    return $sent;
}

But you can extend Laravel's Mailer and add that functionality ($failedRecipients) to the method send of your new class.

EDIT

In 4.1 you can now have access to failed recipients using

Mail::failures();
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • Thanks but I just need the first part but if for example I have this recipient "ebc@vd" I still get number 1, and if I use 'fdfdfd' for recipient it hangs! I would like to get 0 in does cases. I think its a proble with the Laravel implementation.. – ebelendez Jun 14 '13 at 00:47
  • Yeah, that's strange, should not hang this way, but Laravel uses SwiftMailer, so it must be a problem in it. About sending mail, sometimes you don't get an error while sending a message, because the SMTP server accepted the message and it will send an e-mail back telling that your message wasn't delivered. – Antonio Carlos Ribeiro Jun 14 '13 at 00:52
  • 2
    Just note that with 4.1 we are now able to obtain "failedRecipients" through `failures()` :) – seus May 07 '14 at 19:59
1

Antonio has a good point about not knowing which failed.

The real questions is success though. You do not care which failed as much as if ANY failed. Here is a example for checking if any failed.

$count=0;
$success_count = \Mail::send(array('email.html', 'email.text'), $data, function(\Illuminate\Mail\Message $message) use ($user,&$count)
{
    $message->from($user->primary_email, $user->attributes->first.' '.$user->attributes->last );
    // send a copy to me
    $message->to('me@example.com', 'Example')->subject('Example Email');
    $count++
    // send a copy to sender
    $message->cc($user->primary_email);
    $count++
}
if($success_count < $count){
    throw new Exception('Failed to send one or more emails.');
}
Artistan
  • 1,982
  • 22
  • 33
1
if(count(Mail::failures()) > 0){
                //$errors = 'Failed to send password reset email, please try again.';
                $message = "Email not send";
            }
return $message;
Mohaimin Moin
  • 821
  • 11
  • 22