【问题标题】:Laravel 4 mail class, how to know if the email was sent?Laravel 4 邮件类,如何知道邮件是否已发送?
【发布时间】:2013-06-11 01:58:49
【问题描述】:

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

【问题讨论】:

  • 是的,你不能只是去 app->config->mail 并更改假装=>true 吗?这样您就可以查看日志中的消息

标签: laravel laravel-4 swiftmailer


【解决方案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 并将该功能 ($failedRecipients) 添加到新类的方法 send 中。

编辑

在 4.1 中,您现在可以使用

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

【讨论】:

  • 谢谢,但我只需要第一部分,但如果我有这个收件人“ebc@vd”,我仍然得到第 1 位,如果我使用 'fdfdfd' 作为收件人,它就会挂起!我想在确实的情况下得到 0。我认为这是 Laravel 实现的问题..
  • 是啊,奇怪,不应该这样挂,但是Laravel用的是SwiftMailer,所以肯定是里面有问题。关于发送邮件,有时您在发送邮件时不会收到错误消息,因为 SMTP 服务器接受了该邮件,并且会发回一封电子邮件,告知您的邮件未送达。
  • 请注意,使用 4.1 我们现在可以通过failures() 获取“failedRecipients” :)
【解决方案2】:

Antonio 有一个很好的观点,即不知道哪个失败了。

真正的问题是成功。你不在乎哪个失败,就像任何失败一样。 这是一个检查是否失败的示例。

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

【讨论】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 2018-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 2012-08-29
      相关资源
      最近更新 更多