Search code examples
pythondjangosendgriddjango-email

Not able to pass SendGrid verification with Django


I'm currently reading a book on Django (for beginners) and I reached the point where I need to implement a password reset feature to the test website using SendGrid SMTP Relay.

After creating an account and starting to create the SMTP relay I greeted with the following screen: SendGrid SMTP instruction screen Based on this page I added the following lines of code to my setting.py file:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = 'xxxxx' #the string which is partialy hidden under the pink square 
EMAIL_PORT = 587
EMAIL_USE_TLS = True

After running the website and trying to reset my password (the password of the superuser) I get the desired message with the reset link in my console but sadly nothing comes to my email. Thus I get the following error message when trying to verify integration. Error message


What I tried so far:

  • I tried creating multiple different API keys to make sure nothing was wrong with the API key
  • I created a new SendGrid account
  • I tried removing EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' from the settings.py file (this only made things worst)

I've been trying to solve this issue for over a day now, any help would be greatly appreciated!


Solution

  • Your email backend setting is set to use the console. If you're just debugging that's fine, you can see how the emails would look like in the console and copy your password reset link from there.

    If you really want to send an email, use the SMTP backend: Set EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'.

    But beware, don't send emails to fake addresses using that, you'll get in trouble if you have too many bounces on your sendgrid account.

    Also if you're going to use SendGrid in production, use the API instead of SMTP. django-anymail (but there are also other packages) provides a backend to use the API.

    Update June 2021 Sendgrid doesn't allow simple username/password authentication anymore (and forces you to login with 2FA). You should use the API, or create an app password specifically for your server and use that instead of your normal password.