Подтвердить что ты не робот

Класс почты Laravel 4, как узнать, было ли отправлено электронное письмо?

Я использую новый класс почты в Laravel 4, кто-нибудь знает, как проверить, отправлено ли электронное письмо? По крайней мере, почта была успешно передана MTA...

4b9b3361

Ответ 1

Если вы делаете

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

Вы узнаете, когда оно было отправлено или нет, но это может быть лучше, потому что SwiftMailer знает, что получателям это не удалось, но Laravel не раскрывает связанный параметр, чтобы помочь нам получить эту информацию:

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

Но вы можете расширить Laravel Mailer и добавить эту функцию ($ failRecipients) к методу отправки вашего нового класса.

ИЗМЕНИТЬ

В 4.1 теперь вы можете получить доступ к неудавшимся получателям, используя

Mail::failures();

Ответ 2

У Антонио есть хорошая мысль о том, чтобы не знать, что не удалось.

Реальные вопросы - это успех. Вам все равно, что не удалось, как если бы ЛЮБОЙ не удалось. Ниже приведен пример проверки того, не удалось ли выполнить.

$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('[email protected]', '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.');
}

Ответ 3

if(count(Mail::failures()) > 0){
                //$errors = 'Failed to send password reset email, please try again.';
                $message = "Email not send";
            }
return $message;