【问题标题】:Cannot use AWS SES to send email by Django application sit in AWS EC2无法使用 AWS SES 通过位于 AWS EC2 中的 Django 应用程序发送电子邮件
【发布时间】:2020-03-25 00:21:23
【问题描述】:

我有一个位于 AWS EC2 中的 Django 应用程序,我想连接到 AWS SES 以发送电子邮件,所以我遵循了这个教程: https://kholinlabs.com/the-easiest-way-to-send-emails-with-django

首先,我在ap-southeast-1a可用区创建了一个EC2实例,入站和出站规则如下:

入站规则:

HTTP    TCP 80  0.0.0.0/0
All traffic All All 0.0.0.0/0
All traffic All All ::/0
SSH TCP 22  0.0.0.0/0
SMTPS   TCP 465 0.0.0.0/0
SMTPS   TCP 465 ::/0
HTTPS   TCP 443 0.0.0.0/0

出站规则:

All traffic All All 0.0.0.0/0
All traffic All All ::/0

然后我在服务器内部创建了 Django 应用程序:

django-admin startproject mysite
python3 manage.py makemigrations
python3 manage.py migrate

在设置文件中添加以下行:

EMAIL_BACKEND = 'django_ses.SESBackend'
AWS_ACCESS_KEY_ID = 'secret'
AWS_SECRET_ACCESS_KEY = 'secret'
AWS_SES_REGION_NAME = 'ap-southeast-2'
AWS_SES_REGION_ENDPOINT = 'email-smtp.ap-southeast-2.amazonaws.com'

然后我在 ap-southeast-2 avalibility zone 中创建了一个 SES 实例。

在 EC2 服务器中,我按照教程输入命令:

python3 manage.py shell
from django.core.mail import send_mail
send_mail(
    'Subject here',
    'Here is the message.',
    'from@example.com',
    ['to@example.com']
)

但它卡了很长时间(大约 10 分钟),然后显示超时消息。

有没有我遗漏的步骤?


其他信息: 远程登录:

 telnet email-smtp.ap-southeast-2.amazonaws.com 465
Trying 3.24.207.252...
Connected to email-smtp.ap-southeast-2.amazonaws.com.
Escape character is '^]'.

465端口可以工作

 telnet email-smtp.ap-southeast-2.amazonaws.com 25
Trying 13.238.20.185...

它卡在 25 端口

telnet email-smtp.ap-southeast-2.amazonaws.com 587
Trying 3.24.207.252...
Connected to email-smtp.ap-southeast-2.amazonaws.com.
Escape character is '^]'.
220 email-smtp.amazonaws.com ESMTP SimpleEmailService-d-EHZHT2N2F i9ibRsZcZvQJA54s7GtJ
451 4.4.2 Timeout waiting for data from client.
Connection closed by foreign host.

【问题讨论】:

    标签: django amazon-ec2 amazon-ses


    【解决方案1】:

    您似乎正试图从from@example.com 发送邮件。这行不通。将发件人和收件人地址更新为您已在 SES 上验证的电子邮件。请参阅下文了解如何验证地址。

    还要确保您已替换 'secret'

    AWS_ACCESS_KEY_ID = 'secret'
    AWS_SECRET_ACCESS_KEY = 'secret'
    

    使用您从 aws 控制台获得的实际访问密钥 ID 和密钥。

    您可以在此处查看如何验证电子邮件地址:

    https://docs.aws.amazon.com/ses/latest/DeveloperGuide/verify-email-addresses.html

    以及如何在这里摆脱沙盒:

    https://docs.aws.amazon.com/ses/latest/DeveloperGuide/request-production-access.html

    【讨论】:

    • 嗨,David Stein,非常感谢,但我验证了我的电子邮件地址,并使用该电子邮件地址发送到我的电子邮件地址 `` send_mail( 'Subject here', 'Here is the message.', 'dnaqazxsw@gmail.com', ['dnaqazxsw@gmail.com'] ) ``` 我使用自己的 AWS_ACCESS_KEY_ID 和 AWS_SECRET_ACCESS_KEY,因为安全问题,所以我在这里将它们替换为 'secret',但是实际上我正在使用我自己的凭证。但是现在执行命令还是卡住了,你还有什么想法吗?
    • >>> from django.core.mail import send_mail >>> send_mail( ... '主题在这里', ... '这是消息。', ... 'dnadnadnareal@gmail .com', ... ['dnadnadnareal@gmail.com'] ...)(卡住)
    • 添加评论时无法格式化内容,抱歉
    • 我明白了,看看这个以帮助您解决端口 25 问题aws.amazon.com/premiumsupport/knowledge-center/…
    • 我可以只使用端口 465 发送电子邮件吗?我可以telent到SES服务器的465端口,然后我在settings.py中添加EMAIL_PORT = 465,但它也不起作用,这是为什么呢?
    猜你喜欢
    • 2020-11-25
    • 2021-05-03
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 2018-07-18
    • 2015-12-13
    相关资源
    最近更新 更多