Search code examples
djangoemailherokusendgrid

Setting up email with Sendgrid in Heroku for a Django App


I am deploying a Django app on Heroku, and using the Sendgrid addon to send out validation email when a user registers on the site.

I followed the instructions here and pasted the following into settings.py:

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'sendgrid_username'
EMAIL_HOST_PASSWORD = 'sendgrid_password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

However, my app is crashing after registration.

What exactly am I supposed to put for EMAIL_HOST_USER and EMAIL_HOST_PASSWORD?

Under the developer's tab in the sendgrid addon in heroku, it gives me the username app*******@heroku.com, and for password it just says "Your Password". Is the password my Heroku password?

Also, do I need to include DEFAULT_FROM_EMAIL in my settings.py file? And where do I tell Sendgrid what it is?

EDIT: I've set DEBUG = True, and it looks like the error is:

SMTPSenderRefused

(550, 'Cannot receive from specified address <[email protected]>: Unauthenticated senders not allowed', '[email protected]')

it looks like the problem is happening before Sendgrid does its thing. Do I need to authenticate the email address with Heroku somehow?


Solution

  • Within your settings.py include:

    import os
    EMAIL_HOST_USER = os.environ['SENDGRID_USERNAME']
    EMAIL_HOST= 'smtp.sendgrid.net'
    EMAIL_PORT = 587
    EMAIL_USE_TLS = True
    EMAIL_HOST_PASSWORD = os.environ['SENDGRID_PASSWORD']
    

    Edit: changed EMAIL_PASSWORD to EMAIL_HOST_PASSWORD as that's the correct spelling.