Amazon SES SMTP with Django

2020-05-13 20:47发布

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', 'info@abc.com',['hello@abc.com'], 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

Any thoughts?

Thanks, G

7条回答
做个烂人
2楼-- · 2020-05-13 20:47

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/

查看更多
爷的心禁止访问
3楼-- · 2020-05-13 20:49

After long long searching and trying I found:

Instead using:

 s = smtplib.SMTP(host, port)
 s.starttls()
 s.login(user, password)

For AmazonSES SMTP must be:

 s = smtplib.SMTP_SSL(host, port)
 s.login(user, password)

So, I think, for django you can either fix django code, or write you own simple email backend [based on default django email backend].

UPD:

I found another solution (but not tested it by myself): use SSLEmailBackend from link below

// settings.py
EMAIL_BACKEND = 'backends.smtp.SSLEmailBackend'

(From here: Mysterious issue with Django + uWSGI + send email )

UPD2:

AmazonSES supports STARTTLS from now :)

Amazon SES supports expanded attachment types, VERP, and STARTTLS for SMTP

(from Amazon Newsletter)

查看更多
Luminary・发光体
4楼-- · 2020-05-13 20:50

Thanks everyone for the recommendations but I finally 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

Nice.

G

查看更多
时光不老,我们不散
5楼-- · 2020-05-13 20:51

I have tried smtp settings in order to @Givp(who answered above), I want to give complete AWS SMTP settings in django.

DEFAULT_FROM_EMAIL = 'admin@domain.com'

ADMINS = [('name', 'name@domain.com')]
MANAGERS = ADMINS

SERVER_EMAIL = 'admin@domain.com' # this is for to send 500 mail to admins

EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
MAIL_HOST = 'email-smtp.us-east-1.amazonaws.com' 
EMAIL_PORT = 465
EMAIL_HOST_USER = 'Accesskeyofsmtp'
EMAIL_HOST_PASSWORD = 'secretkeyofsmtp'
EMAIL_USE_TLS = True

here we have to verify all the mail-ids before sending email.then everything would work as our expectation

查看更多
We Are One
6楼-- · 2020-05-13 20:52
萌系小妹纸
7楼-- · 2020-05-13 21:04

In Django 1.7, your can send email with SSL natively without third party library.

EMAIL_USE_SSL = True

https://docs.djangoproject.com/en/1.7/ref/settings/#std:setting-EMAIL_USE_SSL

查看更多
登录 后发表回答