Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon SES SMTP with Django

I'm trying to use Amazon's new SMTP service for SES with Django 1.3.1 but I'm not having much luck.

I've created my SES SMTP credentials and have this in my settings:

EMAIL_USE_TLS = True EMAIL_HOST = 'email-smtp.us-east-1.amazonaws.com' EMAIL_HOST_USER = 'my-smtp-user' EMAIL_HOST_PASSWORD = 'my-smtp-password' EMAIL_PORT = 465 

Then I try sending a test email (from and to verified email addresses):

from django.core.mail import send_mail  send_mail('Test subject', 'This is the body', '[email protected]',['[email protected]'], fail_silently=False) 

But I get the following error:

SMTPServerDisconnected: Connection unexpectedly closed 

I can telnet to the server:

telnet email-smtp.us-east-1.amazonaws.com 465 
like image 470
GivP Avatar asked Dec 20 '11 19:12

GivP


People also ask

Does Amazon SES use SMTP?

To send production email through Amazon SES, you can use the Simple Mail Transfer Protocol (SMTP) interface or the Amazon SES API.

How do I send an email in Django?

In most cases, you can send email using django.core.mail.send_mail() . The subject , message , from_email and recipient_list parameters are required.


2 Answers

I found a much simpler solution that would allow me to use Django's built-in mail classes so I can still get my admin error email reports etc.

Thanks to this little beauty I was able to use SES SMTP without any problems:

https://github.com/bancek/django-smtp-ssl

Download and install (python setup.py install)

Then just change your settings to use this new email backend:

EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend' 

The rest of the settings are as per normal:

EMAIL_HOST = 'email-smtp.us-east-1.amazonaws.com' EMAIL_PORT = 465 EMAIL_HOST_USER = 'my_smtp_username' EMAIL_HOST_PASSWORD = 'my_smtp_password' EMAIL_USE_TLS = True 
like image 119
GivP Avatar answered Oct 08 '22 18:10

GivP


2019 Update: Django 2.2.1

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'  EMAIL_HOST = 'email-smtp.us-east-1.amazonaws.com' EMAIL_PORT = 587 EMAIL_HOST_USER = 'my_smtp_username' EMAIL_HOST_PASSWORD = 'my_smtp_password' EMAIL_USE_TLS = True 

No library needed.

Credits : https://stackoverflow.com/a/32476190/5647272

Reference : https://docs.djangoproject.com/en/2.2/topics/email/

like image 34
Umair Mohammad Avatar answered Oct 08 '22 17:10

Umair Mohammad