检查邮件是否在 Laravel 5 上发送成功
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32882357/
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
Check mail is sent successfully or not on Laravel 5
提问by Goper Leo Zosa
I have a function that can send mail on Laravel5 using this
我有一个功能可以使用这个在 Laravel5 上发送邮件
/**
* Send Mail from Parts Specification Form
*/
public function sendMail(Request $request) {
$data = $request->all();
$messageBody = $this->getMessageBody($data);
Mail::raw($messageBody, function ($message) {
$message->from('[email protected]', 'Learning Laravel');
$message->to('[email protected]');
$message->subject('Learning Laravel test email');
});
return redirect()->back();
}
/**
* Return message body from Parts Specification Form
* @param object $data
* @return string
*/
private function getMessageBody($data) {
$messageBody = 'dummy dummy dummy dummy';
}
and is sent successfully. But how to check if it was sent or not? Like
并成功发送。但是如何检查它是否已发送?喜欢
if (Mail::sent == 'error') {
echo 'Mail not sent';
} else {
echo 'Mail sent successfully.';
}
I'm just guessing that code.
我只是在猜测那个代码。
回答by haakym
I'm not entirely sure this would work but you can give it a shot
我不完全确定这会起作用,但你可以试一试
/**
* Send Mail from Parts Specification Form
*/
public function sendMail(Request $request) {
$data = $request->all();
$messageBody = $this->getMessageBody($data);
Mail::raw($messageBody, function ($message) {
$message->from('[email protected]', 'Learning Laravel');
$message->to('[email protected]');
$message->subject('Learning Laravel test email');
});
// check for failures
if (Mail::failures()) {
// return response showing failed emails
}
// otherwise everything is okay ...
return redirect()->back();
}
回答by Abd El-Rahman Rafaat
Hope this helps
希望这可以帮助
The Mail::failures()
will return an array of failed emails.
该Mail::failures()
会返回失败的邮件的数组。
Mail::send(...)
if( count(Mail::failures()) > 0 ) {
echo "There was one or more failures. They were: <br />";
foreach(Mail::failures() as $email_address) {
echo " - $email_address <br />";
}
} else {
echo "No errors, all sent successfully!";
}
source : http://laravel.io/forum/08-08-2014-how-to-know-if-e-mail-was-sent
来源:http: //laravel.io/forum/08-08-2014-how-to-know-if-e-mail-was-sent
回答by vkGunasekaran
You may additionally can make use "Swift_TransportException" to identify any errors.
您还可以使用“Swift_TransportException”来识别任何错误。
try{
//code to send the mail
}catch(\Swift_TransportException $transportExp){
//$transportExp->getMessage();
}
回答by nitrocode
You can use the Mail::failures() function for that. It will have a collection of failed mails if it exists so you can use the code below to check for it.
为此,您可以使用 Mail::failures() 函数。如果存在,它将包含一组失败的邮件,因此您可以使用下面的代码来检查它。
public function sendMail(Request $request) {
$data = $request->all();
$messageBody = $this->getMessageBody($data);
Mail::raw($messageBody, function ($message) use ($messageBody) {
$message->from('[email protected]', 'Learning Laravel');
$message->to('[email protected]');
$message->subject($messageBody);
});
// check for failed ones
if (Mail::failures()) {
// return failed mails
return new Error(Mail::failures());
}
// else do redirect back to normal
return redirect()->back();
}