13

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

4

3 回答 3

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

编辑

在 4.1 中,您现在可以使用以下方式访问失败的收件人

Mail::failures();
于 2013-06-11T02:57:26.133 回答
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.');
}
于 2014-03-03T01:21:21.733 回答
1
if(count(Mail::failures()) > 0){
                //$errors = 'Failed to send password reset email, please try again.';
                $message = "Email not send";
            }
return $message;
于 2015-11-29T10:38:58.237 回答