2

I have upgraded a site to Laravel 5.5 and suddenly I can no longer send mail, as it gives me the following error:

production.ERROR: Expected response code 250 but got code "550", with message "550-Not authenticated, please enable SMTP Authentication in email software and 550 check login credentials

I have no idea why it is trying to use SMTP when I have specified mail in my .env, which it seemingly ignores:

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

This is a completely fresh installation of Laravel 5.5. I have run composer dumpautoload and artisan cache:clear.

Help?

EDIT:

A simple PHP file with the following code works fine:

$to      = 'my_email_address@hotmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

exit();
Inigo
  • 8,110
  • 18
  • 62
  • 110
  • what is your mail driver? – Mr. Pyramid Oct 03 '17 at 17:31
  • 1
    Mail is using SMTP. It's supposed to, as that's the Send Mail Transfer Protocol, which is on port 25. The only difference is that the `mail` driver uses PHP's `mail()` function. Make sure that it's connecting to localhost, if that's who you're attempting to send through, and make sure that the `config/mail.php` is getting your environment variables. – aynber Oct 03 '17 at 17:33
  • 1
    remove null for username and password – Disfigure Oct 03 '17 at 17:46
  • Does the raw `mail()` command work if you use it in a test PHP file or in `php artisan tinker` or something? – ceejayoz Oct 03 '17 at 17:47
  • @ceejayoz Yes, it does- see edit. – Inigo Oct 04 '17 at 10:53
  • Agree with @Disfigure, thank you for your answer. For me MAIL_DRIVER=smtp MAIL_HOST=localhost MAIL_PORT=25 MAIL_USERNAME= MAIL_PASSWORD= MAIL_ENCRYPTION=null works fine – Vaha Mar 02 '18 at 08:57

3 Answers3

2

Swiftmailer 6.0 does not support the mail driver anymore

With laravel, you also updated swiftmailer, and thus, the mail driver does not work anymore.

Here is the swiftmailer commit that removed the mail driver in 6.0.

It was deprecated since Swiftmailer 5.4.5 with this commit.

Here is a long discussion about why it was removed, where the most useful comment says that:

PHP's mail() function is insecure

The fifth parameter can be exploited to execute arbitrary code on most Linux systems. This is mainly because PHP incorrectly escapes shell arguments. You can read the full explanation in "Why mail() is dangerous in PHP" by RIPS.

A couple of applications had security issues because of that, including Roundcube webmailer, MediaWiki, PHPMailer, ZendFramework, SquirrelMail and Swiftmailer. And your app. So be thankful that it got removed and use SMTP instead. You can usually simply use your local mail server using:

MAIL_DRIVER=smtp
MAIL_HOST=localhost
MAIL_PORT=25
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=null

(You may need to use tls encryption if your server does not accept local unencrypted mail.)

Laravel

The main issue with Laravel is that its documentation still mentions the mail driver in the Laravel 5.7 documentation and did only mention in their upgrade notes for 5.5 that swiftmailer 6.0 is required, but not that this means you cannot use the mail driver anymore.

Christopher K.
  • 1,015
  • 12
  • 18
  • You get the points. This was the answer that eluded me at the time. Shame you weren't around last year when I asked this, I seem to remember this being a particularly frustrating day :D – Inigo Oct 26 '18 at 12:43
0

Eventually, I have faced this error couple of days back and it turns out to be a problem of authentication error. This error itself clears the question it's an error that appears when credentials are not valid and in your case you haven't specified the credentials also you specified your MAIL_DRIVER = mail mail uses php's mail function whereas laravel itself ships with it's own mail mechanism I really recommend you to use that.

The geniune .env for mail will look like this ( I've used mail functionality using gmail)

MAIL_DRIVER=smtp
MAIL_HOST= smtp@gmail.com
MAIL_PORT=25/587/465/2525 (any one)
MAIL_USERNAME=youremail@gmail.com
MAIL_PASSWORD=your app password
MAIL_ENCRYPTION=tls

For App Password visit here

If you have looked into default .env file you can see something like this it is served with mailtrap.io which recommend you for testing purpose specifically for local environemnt.

MAIL_DRIVER=smtp
MAIL_HOST= smtp@mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=dfj233ddfk3 // you will get this as you signup for mailtrap
MAIL_PASSWORD=your_mailtrap_password
MAIL_ENCRYPTION=tls

UPDATE 1

If you need to go on with PHP's mail() you need to install postfix or sendbox to send mails for this follow these instructions alternatively I guess check this SO post I think this might help you

Mr. Pyramid
  • 3,855
  • 5
  • 32
  • 56
  • I don't get it. I've used exactly the .env configuration as per my OP in other sites running Laravel 5.2 and it's worked fine. Why do you say `MAIL_DRIVER = mail` is wrong? – Inigo Oct 04 '17 at 10:50
  • mail uses php's mail. if you are fully using laravel I really recommend laravel's approach of using smtp try my solution you will get rid of this error – Mr. Pyramid Oct 04 '17 at 11:06
  • So I am now forced to send emails through a gmail, or some other email provider, account? – Inigo Oct 04 '17 at 11:09
  • if you are using gmail's smtp which is smtp@gmail.com then you will need to use gmail account for that if you want your own domain like example@domain.com then you need to have your own smtp and then in env it will look like this smtp@domain.com followed by it's email address and password I don't know much about how to get your own smtp so I can't help in how to get your smtp stuff. – Mr. Pyramid Oct 04 '17 at 11:12
  • maybe you will get smtp from host provider when you get your domain – Mr. Pyramid Oct 04 '17 at 11:13
  • Thanks for your help, and if necessary I will use my own SMTP server as you suggest, just to get around this stupid problem. But I'm still not really understanding, and am annoyed that I now have this extra hassle when in the in past I could easily just leave out authentication and use native mail function. Do I understand that this is simply no longer possible with Laravel 5.5- is that what you're saying? This is not explicity mentioned in the docs, in fact it still states "*drivers for SMTP, Mailgun ... PHP's mail function*" https://laravel.com/docs/5.5/mail – Inigo Oct 04 '17 at 11:41
  • I don't feel my question has been answered: Why is my .env file being ignored? What is wrong with `MAIL_DRIVER=mail`? Why is Laravel ignoring this, instead trying to use SMTP, and then crying that I haven't specified username and password? – Inigo Oct 04 '17 at 11:43
  • No you can use mail too but you need to check you are getting your credentials right try to put your credentials into env file – Mr. Pyramid Oct 04 '17 at 12:17
  • OK. Sorry if these are stupid questions. And thank you again for your help. But... *what credentials*? PHP's mail function doesn't require any credentials, right? See the edit to the OP. No credentials there. And I still have a raft of websites running Laravel 5.2 whose .env files just look like the OP – Inigo Oct 04 '17 at 12:20
  • for security laravel uses these credentials. Yes you are right in Laravel 5.2 you won't need these but afterwards you need these credentials as far as I know. – Mr. Pyramid Oct 04 '17 at 12:28
  • Thanks. If you can provide a link to the Laravel docs (or somewhere 'official') where this change is stated, and tell me *what credentials I'm supposed to supply here in order just to use the native PHP mail() function*, that will answer my question (and I'll mark your answer correct). Thanks – Inigo Oct 04 '17 at 12:30
  • yeah I have added an alternative way to use mail() function in laravel you need to use your email id and password to send that email like we use in PHPMailer class check my answer I guess my effort will help you :) – Mr. Pyramid Oct 04 '17 at 12:34
  • We're going round in circles here. Thanks for the link to the other SO question- that is exactly my question (this could be marked as a duplicate, in fact). The accepted answer there doesn't answer the question though- See the comment underneath: "*These are not the correct settings for using PHP's mail() function. That function does not need username and email. These settings are used if you want to send the emails from another server like Gmail.*" <- EXACTLY! – Inigo Oct 04 '17 at 12:52
  • OK bro no problem! I tried but looks like I'm short to help you happy coding :) – Mr. Pyramid Oct 04 '17 at 12:55
  • 1
    OK, thanks for your input anyway, appreciate your time. – Inigo Oct 04 '17 at 12:58
  • Pleasure, if you find the solution then please post it back here it will enlighten my knowledge :) – Mr. Pyramid Oct 04 '17 at 12:59
  • 1
    Will do. I've also commented on the accepted answer on the post that you linked to, so we'll see if that brings in any wisdom! :) – Inigo Oct 04 '17 at 13:00
  • I hope I could enlighten your knowledge with my new answer ;) – Christopher K. Oct 25 '18 at 20:07
0

2021 Update

For Laravel 7 and up, use MAIL_MAILER=sendmail

Erin
  • 5,315
  • 2
  • 20
  • 36