Django & Amazon SES SMTP. Cannot send email

為{幸葍}努か 提交于 2020-01-24 18:24:26

问题


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 (admin@admin.com is replaced with a gmail account that is in the verified email list) :

send_mail("Subject", "content", "admin@admin.com", [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", "abc@gmail.com", ["def@gmail.com"], 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


回答1:


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'


来源:https://stackoverflow.com/questions/30741677/django-amazon-ses-smtp-cannot-send-email

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