Amazon SES SMTP with Django

て烟熏妆下的殇ゞ 提交于 2019-12-02 16:38:28

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

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

vsvasya

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)

http://aws.amazon.com/articles/2405502737055650

core python functionality sample

William Borges

I took like 3 hrs breaking my head over it. Your solution about the smtplib with s.starttls() and then s.login() is good with a python program with all the email credentials in the same file. But I don't think it is a clean way to do it in Django. So I finally figured it out. Irrespective of whether your machine is a 32 or a 64 bit. Just do the following steps:

  1. Install boto

    pip install --upgrade boto

  2. Install django-ses

    pip install django-ses

  3. In your djando settings.py file update the following info.

    EMAIL_BACKEND = 'django_ses.SESBackend'
    AWS_ACCESS_KEY_ID = 'your_username'
    AWS_SECRET_ACCESS_KEY = 'your_password'

  4. In your django file where you want to send an email

    from django.core.mail import send_mail
    send_mail('Test subject', 'This is the body', 'info@abc.com',['hello@abc.com'],fail_silently=False)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!