richard's avatar

Sending an Email on Laravel 5

I want to send an email on L5. I have edited .env file correctly:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=username@gmail.com
MAIL_PASSWORD=mypassword

I'm using my Gmail a/c.

The above configurations give me this error message: Swift_TransportException in AuthHandler.php line 181: Failed to authenticate on SMTP server with username "username@gmail.com" using 2 possible authenticators

How do I solve this?

0 likes
37 replies
theUnforgiven's avatar

You need to enter an actual email address to work with Google?

1 like
bashy's avatar

Try with 2 step verification on and make an app for your Laravel app and use that new password.

richard's avatar

@lstables yes

@bashy I have created a Laravel app and gotten a new password (16 chars) and tried using it. I have also restarted my server but it still throws the same error.

jcq126's avatar

Have you tried changing the port to 465?

richard's avatar

@jcq126 That gives me Swift_TransportException in StreamBuffer.php line 265: Connection could not be established with host smtp.gmail.com [Connection timed out #110] error

richard's avatar

@Steve_U I have tried this "port 465 (with SSL) and port 587 (with TLS)"

Restarted server.

Still nothing.

richard's avatar

Is anyone here using gmail to send their mails? Please share the your config settings

bashy's avatar

I do but not in Laravel, just testing the settings in L5.

Edit

These work

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=username@example.com
MAIL_PASSWORD=pass

mail.php

'encryption' => 'ssl',
2 likes
sitesense's avatar

@richard if you look in your gmail account associated with this, you will find an email telling you that access was blocked.

Within that email is a link to a setting that you can change that allows less secure apps to have access.

jcq126's avatar

Thats what I just said lol, hopefully it works for you.

sitesense's avatar

@jcq126 yeah sorry about that, somehow I didn't see that and it was right under my nose lol.

richard's avatar

Well, I have managed to send an email using @bashy's configurations on my local machine running Laravel's homestead. However, moving the files to my VPS, it cannot send. It throws Connection could not be established with host smtp.gmail.com [Connection timed out #110] error.

bashy's avatar

Can you ping or telnet to the SMTP server for gmail?

richardbishopme's avatar

Hi @richard,

I had the same problem when I was setting up my Gmail to work with Laravel 5. I had to do what @jcq126 suggested https://support.google.com/accounts/answer/6010255?hl=en

Also, I had to 'train' Google by visiting the following link https://accounts.google.com/DisplayUnlockCaptcha if it didn't work. Eventually Google decided that I was allowed and let me through automatically.

For ref, my env:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=username@gmail.com
MAIL_PASSWORD=password
1 like
sitesense's avatar

Make sure the port is open on your server. 465 or 587 - whichever you have used.

richard's avatar

@bashy I have ping'd it. Shows there is a valid communication. @desloc I have allowed access to less secure apps and clicked the second link... but am still unable to send an email @sitesense How do I 'open' the port? lol... Or how do I check if a port is open or not.

richard's avatar

I have signed up on Mandrill.

MAIL_DRIVER=mandrill
MAIL_HOST=smtp.mandrillapp.com
MAIL_PORT=587
MAIL_USERNAME=username@gmail.com
MAIL_PASSWORD=myapikey

I got an error FatalErrorException in MandrillTransport.php line 114: Class 'GuzzleHttp\Client' not found

sitesense's avatar

@richard if you can ping it then you don't need to open the port... you did ping it from the server and not your local machine right?

richard's avatar
richard
OP
Best Answer
Level 21

Yaaay! I have managed to solve it using these settings

MAIL_DRIVER=mail
MAIL_HOST=smtp.mandrillapp.com
MAIL_PORT=587
MAIL_USERNAME=username@gmail.com
MAIL_PASSWORD=myapikey

though they are sending mails to my spam folder :(

@sitesense thanks for your help

2 likes
bashy's avatar

I would go as far to telnet into the SMTP server so you can see the HELO response, that way you know it's connectable. Make sure to do what @sitesense said above. Server -> Gmail.

For the mandrill API you need Guzzle/http package.

"guzzlehttp/guzzle": "~5.0"
richard's avatar

@bashy @sitesense I pinged it from the server (via Windows - puTTy SSH). I just cant figure out what's wrong with Gmail. Can we now configure Mandrill to send mails to Inbox and not Spam folder?

bashy's avatar

Mainly SPF records etc to "sign" the emails to verify that they came from the domain.

On the Gmail matter, who knows. You'd have to try a telnet to make sure you can connect on the other port. A ping alone won't confirm that.

rahulyadav's avatar

MAIL_DRIVER=smtp

MAIL_HOST=smtp.mandrillapp.com

MAIL_PORT=587

ENCRYPTION=tls

MAIL_USERNAME=username@gmail.com

MAIL_PASSWORD=myapikey

It is Working fine in my localhost but i got error "Connection could not be established with host smtp.mandrillapp.com" on server.

htdung83's avatar

@richard If the problem is from Gmail, you can not send email in localhost. My app can send from localhost but it can't after I upload onto server. I think the problem is from server.

I got that problem till now :(

Next

Please or to participate in this conversation.