python - 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
解决方案
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.
推荐阅读
- reactjs - 究竟 useselector 是如何工作的?
- snmp-trap - 为什么我将所有 snmp 陷阱都作为未知陷阱接收
- android - 使用 HttpURLConnection 发出 JSON POST 请求时出错
- machine-learning - 使用 K-Means 和凝聚聚类创建混合聚类方法
- wso2is - WSO2 IS 登录表单不显示
- reactjs - 如何修改表格行边框(antd)?
- python - 写入容器内的文件
- python - Pandas IndexError:处理多级文件时列表索引超出范围
- python - 使用 Autoscaling 组的 AWS Metric 的 AWS Lambda 函数
- javascript - Javascript新日期();输出