55

I've been on a project for the past four months now and I am really pissed at what am facing with Laravel right now. It's not sending emails; I set it up to use the mail driver and put in the right code, but it seems not to work at all. Besides not working, it doesn't even give me an error!

Here is my configuration:

return array(

/*
|--------------------------------------------------------------------------
| Mail Driver
|--------------------------------------------------------------------------
|
| Laravel supports both SMTP and PHP's "mail" function as drivers for the
| sending of e-mail. You may specify which one you're using throughout
| your application here. By default, Laravel is setup for SMTP mail.
|
| Supported: "smtp", "mail", "sendmail"
|
*/

'driver' => 'mail',

/*
|--------------------------------------------------------------------------
| SMTP Host Address
|--------------------------------------------------------------------------
|
| Here you may provide the host address of the SMTP server used by your
| applications. A default option is provided that is compatible with
| the Postmark mail service, which will provide reliable delivery.
|
*/

'host' => 'smtp.mailgun.org',

/*
|--------------------------------------------------------------------------
| SMTP Host Port
|--------------------------------------------------------------------------
|
| This is the SMTP port used by your application to delivery e-mails to
| users of your application. Like the host we have set this value to
| stay compatible with the Postmark e-mail application by default.
|
*/

'port' => 587,

/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| You may wish for all e-mails sent by your application to be sent from
| the same address. Here, you may specify a name and address that is
| used globally for all e-mails that are sent by your application.
|
*/

'from' => array('address' => null, 'name' => null),

/*
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/

'encryption' => 'tls',

/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
|
| If your SMTP server requires a username for authentication, you should
| set it here. This will get used to authenticate with your server on
| connection. You may also set the "password" value below this one.
|
*/

'username' => null,

/*
|--------------------------------------------------------------------------
| SMTP Server Password
|--------------------------------------------------------------------------
|
| Here you may set the password required by your SMTP server to send out
| messages from your application. This will be given to the server on
| connection so that the application will be able to send messages.
|
*/

'password' => null,

/*
|--------------------------------------------------------------------------
| Sendmail System Path
|--------------------------------------------------------------------------
|
| When using the "sendmail" driver to send e-mails, we will need to know
| the path to where Sendmail lives on this server. A default path has
| been provided here, which will work well on most of your systems.
|
*/

'sendmail' => '/usr/sbin/sendmail -bs',

/*
|--------------------------------------------------------------------------
| Mail "Pretend"
|--------------------------------------------------------------------------
|
| When this option is enabled, e-mail will not actually be sent over the
| web and will instead be written to your application's logs files so
| you may inspect the message. This is great for local development.
|
*/

'pretend' => false,

);

Here is my PHP code for sending email:

$data["mail_message"] = "Hello!";

    Mail::send('emails.welcome', $data, function($message)
    {
        $message
            ->to('me@mydomain.com')
            ->from('info@otherdomain.com')
            ->subject('TEST');
    });
Peter
  • 373
  • 3
  • 10
Daniel Barde
  • 2,603
  • 5
  • 31
  • 40

18 Answers18

19

You can check general E-Mail delivery by typing:

php artisan tinker

Mail::getSwiftMailer()->registerPlugin (
    new Swift_Plugins_LoggerPlugin(new Swift_Plugins_Loggers_EchoLogger(false))
);
$to = 'youraddress@example.com';
Mail::raw('Testmail', function ($message) use($to) {  
    $message->to($to)->subject('Testmail'); 
});

And you will get even the output of the SMTP dialog.

Make sure to replace $to with your mail address to check if it arrives.

EDIT If this works and you still have problems with email delivery, you might have a queuing issue.

Alex
  • 32,506
  • 16
  • 106
  • 171
  • Very useful snippet thanks. Unfortunately only confused me more as this worked perfectly but mail called by the queue worker hangs for one minute – Snapey Feb 03 '21 at 18:26
  • Good point - it's only for testing mail delivery when queuing is not involved :) – Alex Feb 04 '21 at 17:56
  • 3
    Unfortunately, `Swift_Plugins_LoggerPlugin` and `Swift_Plugins_Loggers_EchoLogger` do not exist in Laravel 8. – Martin Braun Sep 01 '21 at 08:40
18

First and foremost, go to the app/config/mail.php and change the driver to “mail”. Also put the host as blank.

Issam Ben Ameur
  • 249
  • 2
  • 12
  • If you have not purchased smtp then change settings for mail as above. – Ashwani Panwar Dec 14 '15 at 10:21
  • I tried various settings and it doesn't work, in fact their documentation says "mail" setting is removed. And unfortunately "sendmail" doesn't just send mail from your server using sendmail command, it appears smtp is the only way it can send anymore: https://github.com/swiftmailer/swiftmailer/issues/866 – NoBugs Nov 17 '19 at 16:17
  • 2
    Anyone trying this method, do not forget to clear the cache, `php artisan config:cache` – VishalParkash Mar 12 '20 at 09:46
18

In my scenario email was being queued so that is why I got no output. I had forgotten I set email to queue by default. I looked in the Jobs table I saw all my messages waiting in there. I ran php artisan queue:work to get the queue running/sending them.

Justin
  • 26,443
  • 16
  • 111
  • 128
  • I didn't add the queue to supervisor so it didn't start after a server restart, thanks for the tip! – funerr Feb 02 '18 at 17:22
6

The Fix That Worked For Me

  1. In your .env file change APP_URL=127.0.01 to APP_URL=http://localhost
  2. Set your SMTP details correctly, and do config:cache, then restart server.

After that my mail started sending!

How I Arrived At That Fix

I had previously set my SMTP details up correctly, but even after doing config:cache config:clear and restarting my server, the emails were not sending. After that I compared it to my working Laravel App, and the only difference was the 127.0.0.1 -> http://localhost, so I changed that as a last ditch attempt, and it worked.

Hearen
  • 7,420
  • 4
  • 53
  • 63
Greggers
  • 131
  • 1
  • 6
6

A solution for notifications not sent via 'mail'

https://laravel.com/docs/5.7/notifications#customizing-the-recipient

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * Route notifications for the mail channel.
     *
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return string
     */
    public function routeNotificationForMail($notification)
    {
        return $this->email_address;
    }
}
5

Go to .env file and set

MAIL_DRIVER=smtp

Or you can set driver from the config files. Go to file Laravel 4 : app/config/mail.php Laravel 5 : config/mail.php and set

'driver' => 'smtp',

You may use SMTP. Hope It will help.

Brn.Rajoriya
  • 1,534
  • 2
  • 23
  • 35
  • Hiya, this may well solve the problem... but it'd be good if you could edit your answer and provide a little more explanation about how and why it works :) Don't forget - there are heaps of newbies on Stack overflow, and they could learn a thing or two from your expertise - what's obvious to you might not be so to them. – Taryn East Nov 19 '14 at 05:38
  • Ok @TarynEast i will ensure that i should provide more description for better understanding from next time. Thanks – Brn.Rajoriya Nov 05 '15 at 11:20
  • 1
    You can still provide more information this time - you just need to edit it :) – Taryn East Nov 05 '15 at 23:53
  • Thanks to remember me. I have just change the description. Please check. – Brn.Rajoriya Aug 29 '16 at 09:44
5

It could be because you have a ".env" file, in the Laravel project root, with mail server configuration like this:

...
MAIL_DRIVER=mail
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

It seems the ".env" file just overrides "config/mail.php" file. You can just remove same lines from the ".env" file for use "config/mail.php" configuration options.

Alex
  • 1,297
  • 1
  • 16
  • 12
  • No, the `config/mail.php` doesn't need to be touched because of `.env` file. Better is to put it's name (`.env`) in `.gitignore` file and distribute the `.env.example` to allow local changes to `.env`. – Roland Jun 29 '18 at 15:38
  • 1
    @Roland not "doesn't need".. It's strictly related to your solution. In most cases with different deployment environment you really need a .env file, but in case if it's local / local=production environment you can use mail.php. But of course it's preferred to use more robust solution in the .env. – Alex Jun 30 '18 at 20:13
  • 2
    Or should I better write "shouldn't be touched" as it is mostly committed (and only in rare cases like yours maybe it is touched)? – Roland Jul 02 '18 at 08:31
3

Configuration file:

MAIL_DRIVER=smtp 
MAIL_HOST=smtp.gmail.com 
MAIL_PORT=587 
MAIL_USERNAME=abc@gmail.com 
MAIL_PASSWORD=xxxxxxxxxx 
MAIL_ENCRYPTION=tls

Then run the make:mail command:

public function build(Request $request)
{
    return $this->view('mail['variable'=>$request->message])->to($request->to);
}

Routes:

Route::post('send', 'mailController@send');
Route::get('email', 'mailController@email');

Controller:

public function send()
{
    Mail::send(new name());
}

public function email()
{
    return view('email');
}

View:

<body><br><h1>Send Mail</h1><form method="post" action="send">to:<input type="text" name="to">message: <input type="text"  name="message" cols="30" rows="10"></input><input type="submit" name="submit" value="send"></form></body>

Mail:

<body><h1>Welcome, to this mail system, </h1>{!! $variable !!}</body>

Thing to remember: The message variable is a build-in variable and will generate error so use any other variable instead of message.

More details:

Sven
  • 1,450
  • 3
  • 33
  • 58
3

i had the same issue ! and then i noticed that my boss made changes to config/mail.php. Here is what you need :

set Mail variables in your .env file :

MAIL_DRIVER=smtp
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls

in config/mail.php, make sure you have something => env('ENV_VARIABLE_NAME', 'default_value')

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
    'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
    'name' => env('MAIL_FROM_NAME', 'Example'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
    'theme' => 'default',

    'paths' => [
        resource_path('views/vendor/mail'),
    ],
],
'log_channel' => env('MAIL_LOG_CHANNEL'),

If you have ssl issues, add to config/mail.php :

'stream' => [
        'ssl' => [
            'allow_self_signed' => true,
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],
    ]

Good luck everyone.

esmee
  • 31
  • 1
2

I had the same problem with Bluehost shared hosting. I have contacted their support 3 times and they said that there is no problem on their side, but I think that it is something in the hosting settings, because I send the emails from Bulgarian hosting SMTP without any problems.

But let me tell you what solves this problem with Bluehost:

I added this to my .env file:

MAIL_EHLO_DOMAIN="yourdomain.com"

Replace yourdomain.com with your domain.

Rado
  • 21
  • 2
1

I finally got it to work. If you're using mailgun, add this information below to your .env file

MAILGUN_DOMAIN=your.mailgundomain.com
MAILGUN_SECRET=key-yourmailgunkey

and then run

php artisan config:clear
Muhammad Ibnuh
  • 360
  • 5
  • 14
1

For Laravel 6, if verification email is not send anymore:

in user.php add

use Illuminate\Foundation\Auth\User as Authenticatable;

and

class User extends Authenticatable implements MustVerifyEmail

and in routes/web.php

Auth::routes(['verify' => true]);
mart
  • 354
  • 3
  • 14
1

Use Mail::to instead of Mail::send

Check my answer below

Laravel Mail

Ahmet Firat Keler
  • 2,603
  • 2
  • 11
  • 22
0

Change the driver in your config file to 'smtp' from 'mail'. I guess that should work.

Suresh Bala
  • 2,294
  • 1
  • 13
  • 7
  • That didn't work, i tried smtp with gmail and it sends but it sends with the gmail account which isn't what i want, i wanted the sender to bear an email from my domain like so, info@mydomain.com. – Daniel Barde Jun 01 '14 at 19:08
  • Can the smtp username in the config file be empty? I don't have experience with mailgun, but I use mandrill. In this case, the username is the username of the mandrill account. Same with password as well. Password is not the password of the account but the API key – Suresh Bala Jun 01 '14 at 19:33
0

Not a technical answer but it fixed my issue.

If your using GMAIL to check the incoming mail just make sure you check your SPAM folder.

Andrew
  • 1,126
  • 11
  • 28
0

Firstle go to your project root on cmd and run this cmd -

php artisan route:cache (clear your routes cache)
php artisan cache:clear (clear your application cache)
php artisan config:cache (clear your config cache)
php artisan view:clear (clear your view (blade) cache)

Go to .env file

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=YourUserName
MAIL_PASSWORD=YourPassword
MAIL_ENCRYPTION=tls

Go to project root and use this cmd

php artisan make:mail WelcomeUserVerifyMail

after that goto App\mail\WelcomeUserVerifyMail add these line -

use Illuminate\Mail\Mailable;
public $mailData;
public function __construct(array $mailData)
{
  $this->mailData = $mailData;
}
public function build()
{
return $this->from(env('MAIL_FROM'))->subject('Test')->view('email-template.email'); //view load
}

In your controller where you use mail functionality -

use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeUserVerifyMail;

// Send Verify email
$mailData = ['name' => "Shubham Gupta", 'verifyButtonUrl' => $verifyButtonUrl];
Mail::to('shubham@yopmail.com')->send(new WelcomeUserVerifyMail($mailData));
Shubham Gupta
  • 291
  • 3
  • 10
0

I got same problem.

Mail was no sending because I didn't set the recipient for email.

Do not forget to set it.

$mail->to('example@example.com');
112Legion
  • 1,129
  • 12
  • 25
0

I spent several days on this issue. For me (Laravel 9) the solution was to set local_domain in mail.php in smtp section, or MAIL_EHLO_DOMAIN in .env file, it should not be null because by default 127.0.0.1 was set and it didn't work for me:

   'mailers' => [
        'smtp' => [
            'transport' => 'smtp',
            'host' => env('MAIL_HOST', 'mail.example.org'),
            'port' => env('MAIL_PORT', 465),
            'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => null,
            'local_domain' => env('MAIL_EHLO_DOMAIN', 'example.org')
        ],

Also if you use SSL/TLS encryption (465 port in my case), 'encryption' value should be 'ssl', not 'tls'.

Igorock
  • 2,691
  • 6
  • 28
  • 39