shape
shape

Reputation: 95

Failed to authenticate on SMTP server with username "apikey"

i am getting this error when i try to send mails via sendgrid in laravel 6:

Failed to authenticate on SMTP server with username "apikey" using 2 possible authenticators. Authenticator LOGIN returned Expected response code 250

my .env:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=***** (my key copied from sendgrid)
MAIL_ENCRYPTION=tls

Upvotes: 8

Views: 12216

Answers (6)

Nisal Gunawardana
Nisal Gunawardana

Reputation: 1455

Always follow latest SendGrid article for configuration: https://docs.sendgrid.com/for-developers/sending-email/laravel

I faced a similar issue after a Laravel upgrade. According to the above article, they have changed the configuration for Laravel 7:

MAIL_MAILER=smtp
# MAIL_DRIVER=smtp # for laravel < 7
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=sendgrid_api_key
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="John Smith"
[email protected]

Also MAIL_USERNAME should be 'apikey', not the name you gave to the API key when creating.

Upvotes: 0

Khushal Bhalsod
Khushal Bhalsod

Reputation: 187

I had this same problem and I found issue.

Mailgun temporarily disabled my account due to pending monthly invoice payment.that's why i was receiving that error.

So, you should check once your mail service/account status.

Upvotes: 0

Opeoluwa Iyi-Kuyoro
Opeoluwa Iyi-Kuyoro

Reputation: 103

Whitelisting my server's public IP address on SendGrid was all I needed in my case.

Upvotes: 2

Dale Ryan
Dale Ryan

Reputation: 843

If you just recently edited your .env file, changes won't be applied to the config.

When running on php artisan serve, exit the server and restart it. The config should updated automatically.

I'm running xampp server on development, in my case I need to execute the following commands in the console to apply changes to config:

php artisan config:clear
php artisan config:cache

No need to change ports, my .env setup was the same:

MAIL_MAILER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=<your api key from sendgrid>
MAIL_ENCRYPTION=tls

I've omitted MAIL_FROM_ADDRESS and MAIL_FROM_NAME since my purpose is for our company's website contact form and I wanted the mail sender and email to be the guest or client email

If you are expecting to send from the same email all the time then populate those variables.

Here is an official documentation from sendgrid using Laravel mailables https://sendgrid.com/docs/for-developers/sending-email/laravel/

Upvotes: 1

After spending 28 hours on this issue the solution is changing the port to 2525 and it's working now. You don't need to change or encode base64.

Using the following settings.

MAIL_MAILER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=2525
MAIL_USERNAME=apikey
MAIL_PASSWORD=sendgrid_apikey
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"

Upvotes: 0

Ever CR
Ever CR

Reputation: 351

I had this same problem and I solved it by encoding the APIKEY in Base64.

Here's a little more help: https://sendgrid.com/docs/API_Reference/SMTP_API/getting_started_smtp.html#-Sending-a-test-SMTP-email-with-Telnet

Upvotes: 2

Related Questions