首页 > 解决方案 > SMTP-Sendgrid Email in Django app: working locally, on Heroku error SMTPServerDisconnected

问题描述

On the Heroku-deployed version of my app, when testing the password reset function, I get the error SMTPServerDisconnected at /accounts/password/reset/ please run connect() first, this reset functionality works perfectly fine on the local version of my app - I receive the mails from sendgrid without any problems and the app behaves as expected:

my email settings are:

EMAIL_HOST=smtp.sendgrid.net
EMAIL_HOST_USER=apikey
EMAIL_HOST_PASSWORD=*****************************************
EMAIL_PORT=587
EMAIL_USE_TLS=True

my production setup is:

if ENVIRONMENT == 'production':
    SECURE_BROWSER_XSS_FILTER = True
    X_FRAME_OPTIONS = 'DENY'
    SECURE_SSL_REDIRECT = True
    SECURE_HSTS_SECONDS = 3600
    SECURE_HSTS_INCLUDE_SUBDOMAINS = True
    SECURE_HSTS_PRELOAD = True
    SECURE_CONTENT_TYPE_NOSNIFF = True
    SESSION_COOKIE_SECURE = True
    CSRF_COOKIE_SECURE = True
    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

I tried the following (not successfull) solutions: 1.) Setting the SENDGRID_API_KEY at heroku's config variables as suggested here Sending SMTP email with Django and Sendgrid on Heroku

2.) Setting the EMAIL_PORT=456 as according to sendgrid this is the port for SSL connections and I think i have defined that in my production settings

Would be great if anyone has an idea, what to do

标签: pythondjangoherokusendgridemail

解决方案


Ok, I figured it out. Anyone having the same issue, here the solution that worked for me: 1.) Make sure you have the heroku sendgrid addon actually installed. You can install the free tier via the command line:

heroku addons:create sendgrid:starter -a <your-heroku-app-name>

2.) After successfull installation go to the 'config vars' section on the 'settings' tab of your app on heroku. Press 'reveal convig vars' and add as new keys 'EMAIL_HOST', 'EMAIL_HOST_USER', 'EMAIL_HOST_PASSOWORD', 'EMAIL_PORT' and 'EMAIL_USE_TLS'. For the values just take the same values you had set up on your local app.

Now everything works and I can send emails from my deployed app as well.


推荐阅读