Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 SwiftMailer's Send: how to get error code

Tags:

php

email

laravel

I am sending email similar to this: Laravel 5: Sending Email. This is how I do it:

$mail_status = Mail::send( 'emails.followup', $data, function( $message ) use ($data) {
             $message->to( $data['email'] )
            ->from( $data['email_from'], $data['email_name'] )
            ->subject( $data['subject'] );
        });

if($mail_status) {
    $ret_status = 'sent';
}
else
    $ret_status = 'sent_failed';

Now, if the $ret_status is 'sent_failed', I want to know what happened. How do I do it? How can I see the message from the mail server?

Here is my .env file:

MAIL_DRIVER=smtp
MAIL_HOST=mail.mydomain.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=thepassword
MAIL_ENCRYPTION=ssl

Update

Looks like the approach above is Laravel 4. If you know how to get the error code using Laravel 5+, I can consider it as the correct answer.

like image 278
user1506104 Avatar asked Aug 22 '17 13:08

user1506104


3 Answers

Checking Mail::failures() immediately after Mail::send() will return an array of failed email addresses you tried to send an email to.

However, as far as I know you cannot get the exact error on failure using Mail facade. For that, you need to set the debug true on your .env and check on Laravel log.

like image 140
Pubudu Jayawardana Avatar answered Nov 08 '22 13:11

Pubudu Jayawardana


You can use try catch for that, also you mistaken that use $email, $email_from, $email_name, $subject without passing into function scope.

try{
    $mail_status = Mail::send( 'emails.followup', $data, function( $message ) use ($data, $email, $email_from, $email_name, $subject) {
                        $message->to( $email )
                        ->from( $email_from, $email_name )
                        ->subject( $subject );
                    });
    //If error from Mail::send
    if($mail_status->failures() > 0){
        //Fail for which email address...
        foreach(Mail::failures as $address) {
            print $address . ', ';
        }
        exit;
    }  
}
catch(\Exception $e){
    // Get error here
    print $e->getMessage();
    exit;
}

Added failure printing for email address to check email fail for which address.

like image 5
AddWeb Solution Pvt Ltd Avatar answered Nov 08 '22 12:11

AddWeb Solution Pvt Ltd


  1. To get dev version of errors (on screen), set in your .env file APP_DEBUG=true
  2. To get the exact message, do do the try catch, but catch Swift_TransportException https://stackoverflow.com/a/42221501/2119863
  3. You may also, if needed, get the mailer instance via app('mailer') and do some research there.

here is a working example:

try{
    $mail_status = Mail::send( 'emails.followup', $data, function( $message ) use ($data) {
        $message->to( $data['email'] )
        ->from( $data['email_from'], $data['email_name'] )
        ->subject( $data['subject'] );
    });
}catch(\Swift_TransportException $e){
    dd($e, app('mailer'));
}

and the dd() result:

Swift_TransportException {#237 ▼
  #message: """
    Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required\r\n
    "
    """
  #code: 530
  #file: ...
like image 3
Unamata Sanatarai Avatar answered Nov 08 '22 12:11

Unamata Sanatarai