23

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 <info@myapp.com>: Unauthenticated senders not allowed', 'info@myapp.com')

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

Kristian Glass
  • 37,325
  • 7
  • 45
  • 73
WarAndPiece
  • 1,007
  • 1
  • 10
  • 10

2 Answers2

39

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.

WarAndPiece
  • 1,007
  • 1
  • 10
  • 10
CraigKerstiens
  • 5,906
  • 1
  • 25
  • 28
  • I've made the changes, but still getting the error. I set debug = true, and the error is SMTPSenderRefused, saying it cannot receive from specified address : Unauthenticated sender not allowed. Where would one authenticate email in Heroku? – WarAndPiece Mar 15 '12 at 21:30
  • 1
    Ok, actually this is the answer, but it should be "EMAIL_HOST_PASSWORD" and not "EMAIL_PASSWORD" – WarAndPiece Mar 17 '12 at 06:55
0

In the intervening 10 years, the above answer has become obsolete. Sendgrid now uses an API key.

https://docs.sendgrid.com/for-developers/sending-email/django

SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587
EMAIL_USE_TLS = True
Scott Stafford
  • 43,764
  • 28
  • 129
  • 177