Laravel 4邮件类,如何知道邮件是否已发送?

时间:2013-06-11 01:58:50

标签: laravel laravel-4 swiftmailer

我正在使用Laravel 4中的新邮件类,是否有人知道如何检查电子邮件是否已发送?至少邮件已成功移交给MTA ......

3 个答案:

答案 0 :(得分:11)

如果你这样做

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并将该功能($ failedRecipients)添加到你的新类的方法发送中。

修改

在4.1中,您现在可以使用

访问失败的收件人
Mail::failures();

答案 1 :(得分:1)

安东尼奥有一个很好的观点,就是不知道哪个失败了。

真正的问题是成功。如果任何失败,你不关心哪个失败了。 以下是检查是否有任何失败的示例。

$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.');
}

答案 2 :(得分:1)

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