Laravel 4 邮件类,如何知道邮件是否发送?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17035439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 mail class, how to know if the email was sent?
提问by ebelendez
I'm using the new mail class in Laravel 4, does anybody know how to check if the email was sent? At least that the mail was successfully handed over to the MTA...
我在 Laravel 4 中使用新的邮件类,有人知道如何检查电子邮件是否已发送吗?至少邮件成功地交给了MTA......
回答by Antonio Carlos Ribeiro
If you do
如果你这样做
if ( ! Mail::send(array('text' => 'view'), $data, $callback) )
{
return View::make('errors.sendMail');
}
You will know when it was sent or not, but it could be better, because SwiftMailer knows to wich recipients it failed, but Laravel is not exposing the related parameter to help us get that information:
你会知道它何时被发送,但它可能会更好,因为 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;
}
But you can extend Laravel's Mailer and add that functionality ($failedRecipients) to the method send of your new class.
但是您可以扩展 Laravel 的 Mailer 并将该功能 ($failedRecipients) 添加到新类的方法 send 中。
EDIT
编辑
In 4.1 you can now have access to failed recipients using
在 4.1 中,您现在可以使用访问失败的收件人
Mail::failures();
回答by Artistan
Antonio has a good point about not knowing which failed.
安东尼奥有一个很好的观点,即不知道哪个失败了。
The real questions is success though. You do not care which failed as much as if ANY failed. Here is a example for checking if any failed.
真正的问题是成功。你不在乎哪个失败了,就像任何失败一样。这是检查是否有任何失败的示例。
$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.');
}
回答by Mohaimin Moin
if(count(Mail::failures()) > 0){
//$errors = 'Failed to send password reset email, please try again.';
$message = "Email not send";
}
return $message;