35

I have a function that can send mail on Laravel5 using this

/**
 *  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';
 }

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Goper Leo Zosa
  • 1,185
  • 3
  • 15
  • 33

5 Answers5

40

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('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();
}
haakym
  • 12,050
  • 12
  • 70
  • 98
  • I don't suppose there is an equivalent method for the number of successes? `Mail::successes()` throws an error with `Call to undefined method`. – trysis May 03 '16 at 15:08
  • https://laravel.com/api/5.2/Illuminate/Mail/Mailer.html nope. Just do number of recipients minus count of failures and you will get your success count. – haakym May 03 '16 at 18:58
  • i got "response showing failed emails" what should i do now ? – Youssef Boudaya Jan 10 '20 at 09:52
  • @YoussefBoudaya Do you want to debug your failed emails? Did you check your error logs? You may also want to dive into the laravel and swiftmailer codebase to see what determines if an email is a failure. – haakym Jan 10 '20 at 16:16
  • Nice explanation, but I have a kind of another curiosity. Assume for example an email address is valid, but it's not sent properly because an internet disconnected, or something else maybe because an email address is not valid. Is laravel can give a warning about it? – Sead Lab May 23 '20 at 20:53
  • Are failures limited to invalid emails? Look at my previous comment and I'm sure you'll get your answer. Here's a starting point: https://github.com/laravel/framework/blob/5.8/src/Illuminate/Mail/Mailer.php#L484 and https://swiftmailer.symfony.com/docs/sending.html – haakym May 24 '20 at 03:42
  • how can i use when i am sending email using Notification? – Jigar Dec 16 '20 at 10:50
26

Hope this helps

The Mail::failures() will return an array of failed emails.

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

ch271828n
  • 15,854
  • 5
  • 53
  • 88
Abdelrahman Magraby
  • 1,053
  • 1
  • 12
  • 16
7

For Laravel 9.11.0

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

To check if your email was sent successfully one could wrap mail send in a try catch block:

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

or since Mail::to($email)->send($mailable) on success returns an instance of : SentMessage one could check:

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

if ($welcomeEmailSent instanceof \Illuminate\Mail\SentMessage) {
  //email sent success
} else {
  //email sent failed
}
Baspa
  • 1,099
  • 1
  • 18
  • 49
Leo
  • 7,274
  • 5
  • 26
  • 48
  • it's not working bro. It always going to fail section in Laravel 8 Could you suggest another method? – M Mamoon Khan May 17 '22 at 10:10
  • how do you mean ? – Leo May 17 '22 at 12:15
  • Means to say whenever I run the below code it always print not done. I am unable to check whether the email is sent or not? could you tell me the proper method to check it. Thank you $response = Mail::send('emailTemplate.email', $data, function($message) use ($data) { $message->to($data['email'], 'Notification')->subject('Notification Testing email'); $message->from('example@example.com','Notification Testing email'); }); if($responseinstanceof \Illuminate\Mail\SentMessage){ dd('done'); }else{ dd('not done'); } – M Mamoon Khan May 17 '22 at 14:20
  • wrap it in a try catch instead, if exception not caught email is sent, otherwise it failed, ```try { Mail::to($userEmail)->send($welcomeMailable); } catch (Exception $e) { //Email sent failed. }``` – Leo May 17 '22 at 14:24
  • Firstly I'm very thankful to you because you're trying to solve my problem. Secondly, It's not working I did exactly what you said and I put my wrong email and it still not working. – M Mamoon Khan May 18 '22 at 06:58
4

You may additionally can make use "Swift_TransportException" to identify any errors.

try{

   //code to send the mail

}catch(\Swift_TransportException $transportExp){
  //$transportExp->getMessage();
}
vkGunasekaran
  • 6,668
  • 7
  • 50
  • 59
1

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.

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();
}
Kerel
  • 732
  • 6
  • 20
nitrocode
  • 89
  • 6