Django & Amazon SES SMTP. Cannot send email

4.3k views Asked by At

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

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.

In django, I sent an email with this line ([email protected] is replaced with a gmail account that is in the verified email list) :

send_mail("Subject", "content", "[email protected]", [email], fail_silently=False)

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.

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

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

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

AUTH LOGIN
334 VXNlcm5hbWU6
SMTP_USER
334 UGFzc3dvcmQ6
SMTP_PASSWORD

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.

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

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

UPDATE

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", "[email protected]", ["[email protected]"], fail_silently=False)
  return get_return_object(request, status_code=1)

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

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

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

UPDATE

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

2

There are 2 answers

0
Samuel Urias On

2020 update

As the documentation says here: Django Email Documentation and Email_use_ssl

I am using Django 3.0 at this moment, and the documentation says that you can use

EMAIL_USE_SSL = True

... It is generally used on port 465

I make my think works and send emails using AWS SES and port 465 service using that line instead EMAIL_USE_TLS = True :)

0
Dawn T Cherian On

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

pip install django-smtp-ssl

then change EMAIL_BACKEND in settings.py as follows:

EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'

If you use port 587:

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