【发布时间】:2013-06-11 01:58:49
【问题描述】:
我正在使用 Laravel 4 中的新邮件类,有人知道如何检查电子邮件是否已发送吗?至少邮件已经成功移交给 MTA...
【问题讨论】:
-
是的,你不能只是去 app->config->mail 并更改假装=>true 吗?这样您就可以查看日志中的消息
标签: laravel laravel-4 swiftmailer
我正在使用 Laravel 4 中的新邮件类,有人知道如何检查电子邮件是否已发送吗?至少邮件已经成功移交给 MTA...
【问题讨论】:
标签: laravel laravel-4 swiftmailer
如果你这样做
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();
【讨论】:
failures() 获取“failedRecipients” :)
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.');
}
【讨论】:
if(count(Mail::failures()) > 0){
//$errors = 'Failed to send password reset email, please try again.';
$message = "Email not send";
}
return $message;
【讨论】: