如何在Django中使用AWS Simple Email Service(SES)?

时间:2018-06-19 23:51:41

标签: python django amazon-simple-email-service

我正在尝试使用this library将Django项目与AWS SES集成。

settings.py

EMAIL_BACKEND = 'django_ses.SESBackend'

AWS_ACCESS_KEY_ID = 'my_aws_access_key'
AWS_SECRET_ACCESS_KEY = 'my_aws_secret_access_key'

AWS_SES_REGION_NAME = 'us-west-2'
AWS_SES_REGION_ENDPOINT = 'email.us-west-2.amazonaws.com'

它引发以下错误

SESAddressNotVerifiedError: 400 Email address is not verified.
<ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
  <Error>
    <Type>Sender</Type>
    <Code>MessageRejected</Code>
    <Message>Email address is not verified. The following identities failed the check in region US-WEST-2: jpark1320@gmail.com, webmaster@localhost</Message>
  </Error>
  <RequestId>0220c0a0-741b-11e8-a153-475b5dfc6545</RequestId>
</ErrorResponse>

我什至无法猜测为什么我的代码是错误的。但是,send_mail()可能是一个问题。我正在尝试向用户发送电子邮件以进行注册确认。我在下面放置了用于发送电子邮件的代码。

SMTP设置

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'my_google_email@gmail.com'
EMAIL_HOST_PASSWORD = 'my_google_email_password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'My Team Name <noreply@gmail.com>'

更新

views.py

def signup(request):
    if request.method == 'POST':
        form = SignupForm(request.POST)
        if form.is_valid():
            # Create a user object to set email to be username before passing it to db
            user = form.save(commit=False)

            user.is_active = False
            user.email = form.cleaned_data['username']
            user.save()

            current_site = get_current_site(request)
            mail_subject = "[Modvisor] Please verify your email address."
            message = render_to_string('accounts/account_active_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user),
            })
            to_email = user.email
            email = EmailMessage(
                        mail_subject, message, to=[to_email]
            )
            email.send()

            return redirect('signup_confirm')

    else:
        form = SignupForm()
    return render(request, 'accounts/register.html', {'form': form})

1 个答案:

答案 0 :(得分:1)

错误的相关部分是“电子邮件地址未验证”。默认情况下,SES处于沙盒模式,在该模式下,SES不允许您使用以前未验证的发件人或收件人地址。您需要在SES控制台中验证地址或打开支持请求以退出沙箱。

Verifying Email Addresses in Amazon SES

要验证地址,请转到SES控制台。在左侧选择“电子邮件地址”,然后单击“验证新电子邮件地址”。您将需要有权访问该电子邮件地址,以便单击将发送给该​​电子邮件地址的链接。

Moving Out of the Amazon SES Sandbox

要简单地open a support request移出沙箱,请描述您的用例并等待几天。