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

tvokkenx  于 2023-01-10  发布在  其他
关注(0)|答案(5)|浏览(258)

我有一个函数可以用这个在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';
 }

并且发送成功,但是如何检查是否发送?比如

if (Mail::sent == 'error') {
 echo 'Mail not sent';
} else {
 echo 'Mail sent successfully.';
}

我只是在猜密码。

lymnna71

lymnna711#

我不太确定这是否可行,但你可以给予看

/**
 *  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();
}
jogvjijk

jogvjijk2#

希望这能帮上忙
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!";
}

来源:http://laravel.io/forum/08-08-2014-how-to-know-if-e-mail-was-sent

uurity8g

uurity8g3#

您还可以使用“Swift_TransportException”来识别任何错误。

try{

   //code to send the mail

}catch(\Swift_TransportException $transportExp){
  //$transportExp->getMessage();
}
holgip5t

holgip5t4#

适用于Laravel 9.11.0

Mail::failures() // is deprecated in laravel 9.11.0

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

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

或者由于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
}
njthzxwz

njthzxwz5#

你可以使用Mail::failures()函数来实现,如果失败邮件存在,它会收集失败邮件,所以你可以使用下面的代码来检查它。

public function sendMail(Request $request) {
    $data = $request->all();

    $messageBody = $this->getMessageBody($data);

    Mail::raw($messageBody, function ($message) use ($messageBody) {
        $message->from('yourEmail@domain.com', 'Learning Laravel');
        $message->to('goper.zosa@gmail.com');
        $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();
}

相关问题