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
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
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/
Aug, 2022 Update with Django-3.2.4:
No libraries needed such as django-ses or django-amazon-ses !!
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'email-smtp.ap-northeast-1.amazonaws.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'my_smtp_username' # Must create SMTP Credentials
EMAIL_HOST_PASSWORD = 'my_smtp_password' # Must create SMTP Credentials
DEFAULT_FROM_EMAIL = 'myses#sender.com' # If don't need, comment out!!
I added DEFAULT_FROM_EMAIL.
DEFAULT_FROM_EMAIL = 'myses#sender.com'
If you don't need it, comment it out!!
# DEFAULT_FROM_EMAIL = 'myses#sender.com'
Moreover, for DEFAULT_FROM_EMAIL, put one verified domain or email address whether or not your account is in the sandbox.
So for the verified domain sender.com below,
Three of them below are valid: (Use only one of three)
DEFAULT_FROM_EMAIL = 'abc#sender.com' # OR
DEFAULT_FROM_EMAIL = 'test#sender.com' # OR
DEFAULT_FROM_EMAIL = 'myses#sender.com'
But these two below are not valid: (These give you error)
*The format must be something#sender.com !!
DEFAULT_FROM_EMAIL = 'sender.com'
DEFAULT_FROM_EMAIL = '#sender.com'
Then, for the verified 2 email addresses below,
Just use only one of two below:
DEFAULT_FROM_EMAIL = 'hello#gmail.com' # OR
DEFAULT_FROM_EMAIL = 'world#outlook.com'
Finally, for EMAIL_HOST_USER and EMAIL_HOST_PASSWORD, you must create SMTP Credentials.
Choose SMTP Settings:
Press Create My SMTP Credentials Button:
Given SMTP Credentials:
Then, put the SMTP Credentials as below:
EMAIL_HOST_USER = 'AKIAWP3TMGZN4OZH5H37'
EMAIL_HOST_PASSWORD = 'BB6dufiw96jJHUTrowXI8R4gcyOI+t1+Skbi51cdHYhV'
*(I've already deleted these SMTP Credentials)
Since Django 1.7, you can send email with SSL natively without third party library.
EMAIL_USE_SSL = True
Docs
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
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:
Install boto
pip install --upgrade boto
Install django-ses
pip install django-ses
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'
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
Related
This is very much potentially a duplicate question, but none of the other obvious duplicates have resolved the issue for me:
This is an inherited project.
My settings.py includes:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'my_username#gmail.com'
EMAIL_HOST_PASSWORD = 'my_password'
EMAIL_PORT = '587'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'my_username#gmail.com'
DEFAULT_FEEDBACK_EMAIL = 'my_username#gmail.com'
SERVER_EMAIL = 'my_username#gmail.com'
ACCOUNT_EMAIL_VERIFICATION = 'none'
The code that I'm trying to run is:
subject = 'Subject'
template = get_template('accounts/email-templates/email-activation.html').render(Context(ctx))
email = EmailMessage(subject, template, to=[send_to])
email.content_subtype = "html"
try:
email.send()
My error when trying repeatedly with python manage.py shell is:
gaierror: [Errno 8] nodename nor servname provided, or not known
My dns seems fine, sudo killall -HUP mDNSResponder and dscacheutil -flushcache have been run without success, but I'm hardly an expert on dns settings.
My hosts file is:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
Advice appreciated!
You might need to generate an app password in Gmail or change the settings to allow less secure access. I don't think Gmail will allow you to just use your regular password in - what Google would consider - an insecure manner.
I had similar problem working with Gmail to send mails.
Try only the following codes to send emails. Hope it helps:
In settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = "sender_email#gmail.com"
EMAIL_HOST_PASSWORD = "email_password"
EMAIL_PORT = 0
EMAIL_USE_TLS = True
Codes to send emails:
# Imports for sending emails:
from django.conf import settings
from django.core.mail import EmailMultiAlternatives, send_mail
emailFrom = [settings.EMAIL_HOST_USER]
emailTo = email_here
text_content = 'Your content here'
email_subject = "Subject here"
msg = EmailMultiAlternatives(email_subject, text_content, emailFrom, [emailTo], )
msg.send()
Also change the settings in the 'sender gmail account' to create a passage for the mail to be send from the sender to the receiver. Hope this helps.
I set my settings :
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'email-smtp.us-west-2.amazonaws.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'I got something from Amazon SES SMTP Settings Create My SMTP Credentials'
EMAIL_HOST_PASSWORD = 'I got something from Amazon SES SMTP Settings Create My SMTP Credentials'
EMAIL_USE_TLS = True
But When i use this to my django-allauth email verification,
There comes
SMTPDataError at /accounts/email/
(554, b'Message rejected: Email address is not verified. The following identities failed the check in region US-WEST-2: webmaster#localhost')
How can i solve this?
You need to set DEFAULT_FROM_EMAIL to one of the addresses verified in SES.
Or alternatively you can pass from_email when calling send_mail() (and related).
I am trying to send email using my django app. But after setting Zoho account and adding necessary lines in settings.py I am still not able to send email and it keep giving SMTPAuthenticationError (535, b'Authentication Failed').
#settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_HOST_USER = 'administrator#technovate-iiitnr.org'
EMAIL_HOST_PASSWORD = 'mypass'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
#views.py
html = render_to_string('email/code_email.html',{'code':code})
send_mail('Your Code',
'Hello',
'administrator#technovate-iiitnr.org',
['example#gmail.com'],
html_message=html
)
return render(request,'index.html')
One of the reason could be 2-factor authentication being used in the zoho account.
This prevents you from using account password, instead you need to generate application specific password and use the same.
Read more here
This is what I ran in the django python shell
from django.core.mail import EmailMessage
email = EmailMessage('Hello', 'Test<br>break', 'sender#mysite.com',['receiver#gmail.com'])
email.content_subtype = "html"
email.send()
email.send() returned 1 in python shell, When I checked my sender email I have not received any mail
I am not sure what that 1 means
Here is my settings.py file mail settings
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_HOST_USER = 'sender#mysite.com'
EMAIL_HOST_PASSWORD = 'mypass'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
So email.send() returned 1 meanings it's a successful mail.
The mail went to Spam box in Gmail
When I wrote actual content that I want to send it came to my inbox.
I'm trying to send an email through Django's wrapper.
Here are my relevant settings.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
The Email, which I am trying to isolate in the most basic form of a view:
from django.core.mail import send_mail
def index(request):
subject = 'Subject'
message = 'message'
from_email = settings.DEFAULT_FROM_EMAIL
send_mail(subject, message, from_email, ['email#example.com'])
return render(request, "index.html")
All of the emails and password are legitimate. When I execute the code, I get thrown an error message:
SMTPAuthenticationError at /....*Link to sign into my account*
Please log in via your web browser and\n5.7.14 then try again
I did that but continue to get the same message. The password I give in the app is correct. Is there anything I need to configure in my gmail account?
Change EMAIL_HOST = 'smtp.gmail.com ' toEMAIL_HOST = 'smtp.gmail.com' and I bet your problem will disappear :)
EDIT #1
You are running into authentication issues as EMAIL_USE_TLS is True and Gmail only requires TLS connections for SMTP on port 587. Change to EMAIL_PORT = 587 and you should bypass the issue.
EDIT #2
The error you see can be fixed through your Gmail settings. See -
Django SMTPAuthenticationError