Artistic peacock feather

Tip for Laravel Mail Not Sending Using PHP Mail

Laravel Mail Not Working

Try adding this line to your code

 
Laravel banner for mail::send() task.

If you are having trouble sending email with Laravel and PHP’s mail(), try these steps on your development or staging server.

Laravel Config

Make sure your /config/mail.php file is looking to your .env file for mail configuration, and set up the following parameters:

MAIL_DRIVER=mail
MAIL_HOST=null
MAIL_PORT=null
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

The Problematic Code

Do you have something like this?

Mail::send('emails.my_message', $data, function($message){
    $message->to(Request::input('email'))->subject('My Subject');
});

Your email may not be delivered and it may be failing silently.

Try This in Your Controller or Model Code

Laravel’s Mail::send() will return false if the email was not sent, and we will get more information by using Mail::failures().

$result = Mail::send('emails.my_message', $data, function($message){
    $message->from('noreply@mydomain.com', 'My Website');
    $message->to(Request::input('email'))->subject('My Subject');
});

// Laravel tells us exactly what email addresses failed, let's send back the first
$fail = Mail::failures();
if(!empty($fail)) throw new \Exception('Could not send message to '.$fail[0]);

if(empty($result)) throw new \Exception('Email could not be sent.');

// Now do what you do

In addition to adding some checks to ensure the result of the Mail::send() actually worked, it seems to be very important to add in the From address line. Make sure the line

$message->from('noreply@mydomain.com', 'My Website');

is present and see if that does not fix the problem. This also seems to work for Mail::raw().

Last updated on