检查邮件是否成功发送到Laravel 5

Gop*_*osa 12 smtp sendmail laravel laravel-5

我有一个可以使用它在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('yourEmail@domain.com', 'Learning Laravel');
        $message->to('goper.zosa@gmail.com');
        $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';
 }
Run Code Online (Sandbox Code Playgroud)

并成功发送.但是如何检查它是否被发送?喜欢

if (Mail::sent == 'error') {
 echo 'Mail not sent';
} else {
 echo 'Mail sent successfully.';
}
Run Code Online (Sandbox Code Playgroud)

我只是猜测那段代码.

haa*_*kym 21

我不完全确定这会起作用,但你可以试一试

/**
 *  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('yourEmail@domain.com', 'Learning Laravel');
        $message->to('goper.zosa@gmail.com');
        $message->subject('Learning Laravel test email');
    });

    // check for failures
    if (Mail::failures()) {
        // return response showing failed emails
    }

    // otherwise everything is okay ...
    return redirect()->back();
}
Run Code Online (Sandbox Code Playgroud)


Abd*_*aat 14

希望这可以帮助

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!";
}
Run Code Online (Sandbox Code Playgroud)

来源: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 (2认同)

Leo*_*ent 8

对于 Laravel 9.11.0

Mail::failures() // is deprecated in laravel 9.11.0
Run Code Online (Sandbox Code Playgroud)

要检查您的电子邮件是否已成功发送,可以将发送的邮件包装在 try catch 块中:

try {
    Mail::to($userEmail)->send($welcomeMailable);
} catch (Exception $e) {
  //Email sent failed.
}
Run Code Online (Sandbox Code Playgroud)

或者因为Mail::to($email)->send($mailable)成功返回一个 : 的实例,SentMessage因此可以检查:

$welcomeEmailSent = Mail::to($userEmail)->send($welcomeMailable);

if ($welcomeEmailSent instanceof \Illuminate\Mail\SentMessage) {
  //email sent success
} else {
  //email sent failed
}
Run Code Online (Sandbox Code Playgroud)