How to configure Sendgrid SMTP in Laravel

Recently while working on Laravel mail functionalities, I have got some issues in sending email over SMTP server. I was using Sendgrid mail server to send email. I have done the same configuration as I have already done in my one of old project. You have already seen that doing same configuration on different projects sometimes not work. Same was happened with me.

So I have dig deep into that. While checking its official Laravel documentation, I have seen in the .env file, MAIL_USERNAME and MAIL_PASSWORD was changed to API Key. Before that, it was working with only your Sendgrid username and password.

So what is changed?

While login to Sendgrid account, first in the API Key menu under Settings section, create the API key and copy.

Now your Laravel .env file, change MAIL_USERNAME=apikey and MAIL_PASSWORD=<API Key>. This is full Mail configuration in your .env file.

MAIL_MAILER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=465
MAIL_USERNAME=apikey
MAIL_PASSWORD=SG.YPAMv...........V_1yoO78
MAIL_ENCRYPTION=ssl
MAIL_FROM_NAME="HackTheStuff"
MAIL_FROM_ADDRESS=hackthestuffinfo.com

If your website is not ssl certified, use MAIL_PORT=587 and MAIL_ENCRYPTION=tls.

You may have got error if you have not verified sender authenticate.

Expected response code 250 but got code "550", with message "550 The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements

To authenticate your sender authenticate, go to Sender Authenticate menu under the Settings section. Use Domain Authentication or Single Sender Verification option to verify.

The error sould have resolved now.

Tags: