Django& Amazon SES SMTP.无法发送电子邮件 [英] Django & Amazon SES SMTP. Cannot send email

查看:162
本文介绍了Django& Amazon SES SMTP.无法发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的settings.py:

this is my settings.py :

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'email-smtp.us-east-1.amazonaws.com'
EMAIL_PORT = '465'
EMAIL_HOST_USER = config_data['EMAIL_HOST_USER']
EMAIL_HOST_PASSWORD = config_data['EMAIL_HOST_PASSWORD']
EMAIL_USE_SSL = True

我已经验证了电子邮件地址,还生成了SMTP凭据,我下载了包含IAM用户名,Smtp用户名和Smtp密码的凭据.我将smtp用户名用于EMAIL_HOST_USER,将smtp密码用于EMAIL_HOST_PASSWORD.

I have verified the email addresses and have also generated SMTP credentials which I downloaded the credentials containing the IAM username, Smtp username, Smtp password. I used the smtp username for EMAIL_HOST_USER and smtp password for EMAIL_HOST_PASSWORD.

在django中,我发送了以下一行电子邮件(admin@admin.com已替换为已验证电子邮件列表中的gmail帐户):

In django, I sent an email with this line (admin@admin.com is replaced with a gmail account that is in the verified email list) :

send_mail("Subject", "content", "admin@admin.com", [email], fail_silently=False)

那没有用.但是,可以从SES控制台发送测试电子邮件(在已验证的电子邮件中选择一封电子邮件,然后单击发送测试电子邮件"按钮).

That did not work. Sending a test email from the SES console (select one of the email in verified emails and click send a test email button) works though.

文档表示我可以使用openssl命令通过命令行发送电子邮件.因此,我将SSH切换到EC2机器,并使用:

This document says that I can send email via command line with openssl command. So, i did ssh to the EC2 machine, and use :

openssl s_client -crlf -quiet -starttls smtp -connect email-smtp.us-east-1.amazonaws.com:25

请注意,我在这里使用25,当我使用端口465时,它输出:

Notice that i used 25 here, when i used port 465, it outputed :

didn't found starttls in server response, try anyway...
write:errno=32

所以我改为尝试使用端口25,它已连接(250 OK),然后键入:

So instead I tried port 25, It connected (250 OK) and then I typed :

AUTH LOGIN
334 VXNlcm5hbWU6
SMTP_USER
334 UGFzc3dvcmQ6
SMTP_PASSWORD

,但输出:535身份验证凭据无效.可能是我使用了错误的凭证吗?但是我直接从smtp用户和密码生成页面复制用户名和密码.

but it outputted : 535 Authentication Credentials Invalid. Could it be I used a wrong credentials? but I copy the username and password straight from the smtp user and password generation page.

我还尝试将AmazonSESFullAccess策略附加到所有IAM用户,但没有任何反应.

I also tried to attach AmazonSESFullAccess policy to all of the IAM users but nothing happened.

我使用Django 1.8.2.尝试了Django-ses,但也无法正常运行(运行测试时出现语法错误,它不支持python 3?)

I use the Django 1.8.2. Tried Django-ses but it did not work too (syntax error when running the tests, it does not support python 3?)

更新

我将端口更改为587,并使用USE_TLS True,现在可以从django shell发送send_mail了.我将该函数放在我的应用程序views.py中:

I changed the port to 587 and use USE_TLS True and now I can send_mail from django shell. I put the function in my app views.py :

def test_email(request):
  send_mail("title", "content", "abc@gmail.com", ["def@gmail.com"], fail_silently=False)
  return get_return_object(request, status_code=1)

,在urls.py中,我将其放在urlpatterns中:

and in urls.py I put this inside the urlpatterns :

url(r'^test_email/$', views.test_email, name="test_email"),

,当我访问URL时,什么也没发生.我从外壳程序调用了相同的函数test_email,并且可以正常工作.

and when I visited the URL, nothing happens. I call the same function test_email from the shell and it works.

更新

事实证明,我还可以使用端口465和USE_SSL True,并能够从django shell发送send_mail.但仍然无法在views.py

It turns out that I can also use port 465 and USE_SSL True and be able to send_mail from django shell. but still it does not work in views.py

推荐答案

如果使用端口465: 使用pip安装django-smtp-ssl

If you use port 465: Install django-smtp-ssl using pip

pip install django-smtp-ssl

然后按如下所示在settings.py中更改EMAIL_BACKEND:

then change EMAIL_BACKEND in settings.py as follows:

EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'

如果使用端口587:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

这篇关于Django& Amazon SES SMTP.无法发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆