Search code examples
pythondjangoamazon-simple-email-servicedjango-ses

How to use AWS Simple Email Service (SES) in Django?


I'm trying to use this library to integrate my Django project with 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'

It throws the following error

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: [email protected], webmaster@localhost</Message>
  </Error>
  <RequestId>0220c0a0-741b-11e8-a153-475b5dfc6545</RequestId>
</ErrorResponse>

I can't even guess why is wrong on my codes. But, one thing might be a problem is send_mail(). I'm using trying to send an email to a user for sign-up confirmation. I put the codes for sending email below.

SMTP settings

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'my_google_email_password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'My Team Name <[email protected]>'

Update

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})

Solution

  • The relevant part of the error is "Email address is not verified". By default SES is in sandbox mode where it won't let you use From or To addresses that you have not previously verified. You need to verify the addresses in the SES console or open a support request to leave sandbox.

    Verifying Email Addresses in Amazon SES

    To verify an address go to the SES console. On the left side select Email Addresses and then click Verify New Email Address. You will need to have access to the email address so you can click the link that will be sent to it.

    Moving Out of the Amazon SES Sandbox

    To move out of the sandbox simply open a support request, describe your use case and wait a few days.