Mat*_*arp 3 python email django smtp
我在 Heroku 上部署了一个 Django 应用程序。在其中一节中,我使用 SMTP Gmail 设置向用户发送电子邮件。当我在本地运行项目时,电子邮件发送成功,但在 Heroku 上部署的项目上却发送失败。
我在 Stackoverflow 上看到了许多其他答案,但没有一个能解决我的问题。我已在我的 Google 帐户上启用了 2FA,并生成了一个应用程序密码,并在我的设置文件中使用该密码。其他开发者不建议打开allow_less_secure_app选项
我的settings.py文件电子邮件设置-
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('EMAIL_USER2')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASS2')
Run Code Online (Sandbox Code Playgroud)
我views.py对处理邮件的看法-
def index(request)
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
message = form.cleaned_data['message']
email = form.cleaned_data['email']
subject = "You got a message"
thoughts = "{} by {}".format(message,email)
recipients = ['xyz@gmail.com']
sender = 'abc@gmail.com'
send_mail(subject, thoughts, sender ,recipients,fail_silently=False)
return HttpResponse()
else:
form = MyForm()
return render(request,'my_webapp/index.html',{'form':form})
Run Code Online (Sandbox Code Playgroud)
我在 Heroku 日志中遇到的错误是-
raise SMTPAuthenticationError(code, resp)
2019-10-07T18:22:12.174365+00:00 app[web.1]: smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials w2sm9789664qtc.59 - gsmtp')
Run Code Online (Sandbox Code Playgroud)