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

Laravel 4邮件类,如何知道电子邮件是否已发送?,laravel,laravel-4,swiftmailer,Laravel,Laravel 4,Swiftmailer,我正在使用Laravel 4中的新邮件类,有人知道如何检查电子邮件是否已发送吗?至少邮件已成功移交给MTA…如果您这样做 if ( ! Mail::send(array('text' => 'view'), $data, $callback) ) { return View::make('errors.sendMail'); } 您将知道它何时发送或未发送,但可能会更好,因为SwiftMailer知道它向哪位收件人发送失败,但Laravel没有公开相关参数以帮助我们获取该信息: /

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

如果您这样做

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)添加到新类的send方法中

编辑

在4.1中,您现在可以使用

Mail::failures();

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

但真正的问题是成功。你不在乎哪个失败了,就像不在乎哪个失败了一样。 下面是一个检查是否有失败的示例

$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.');
}
$count=0;
$success\u count=\Mail::send(数组('email.html','email.text'),$data,函数(\light\Mail\Message$Message)使用($user,&$count)
{
$message->from($user->primary_email,$user->attributes->first.'.$user->attributes->last);
//给我寄一份
$message->to($message)me@example.com“,”示例“->主题(“示例电子邮件”);
美元计数++
//将副本发送给发件人
$message->cc($user->primary\u email);
美元计数++
}
如果($success\u count<$count){
抛出新异常('无法发送一封或多封电子邮件');
}

是的,你不能直接进入应用程序->配置->邮件并更改假装=>true??这样你可以在日志中查看邮件谢谢,但我只需要第一部分,但如果我有这个收件人”ebc@vd"我仍然得到数字1,如果我对收件人使用“fdfdfd”,它将挂起!我希望在DOS情况下得到0。我认为这是Laravel实现的一个问题。是的,这很奇怪,不应该以这种方式挂起,但Laravel使用SwiftMailer,因此它一定是一个问题。关于发送邮件,有时在发送邮件时不会出现错误,请注意因为SMTP服务器接受了该邮件,它将发送一封电子邮件,告知您的邮件未送达。请注意,在4.1中,我们现在可以通过
failures()
:)获取“failedRecipients”
if(count(Mail::failures()) > 0){
                //$errors = 'Failed to send password reset email, please try again.';
                $message = "Email not send";
            }
return $message;