[Python] How to send email via Django?
I use Gmail as my SMTP server for Django. Much easier than dealing with postfix or whatever other server. I'm not in the business of managing email servers.
EMAIL_USE_TLS = True EMAIL_HOST = 'smtp.gmail.com' EMAIL_PORT = 587 EMAIL_HOST_USER = 'email@example.com' EMAIL_HOST_PASSWORD = 'password'
NOTE: In 2016 Gmail is not allowing this anymore by default. You can either use an external service like Sendgrid, or you can follow this tutorial from Google to reduce security but allow this option: https://support.google.com/accounts/answer/6010255
settings.py, I have the following:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' # Host for sending e-mail. EMAIL_HOST = 'localhost' # Port for sending e-mail. EMAIL_PORT = 1025 # Optional SMTP authentication information for EMAIL_HOST. EMAIL_HOST_USER = '' EMAIL_HOST_PASSWORD = '' EMAIL_USE_TLS = False
My email code:
from django.core.mail import EmailMessage email = EmailMessage('Hello', 'World', to=['firstname.lastname@example.org']) email.send()
Of course, if I setup a debugging server via
python -m smtpd -n -c DebuggingServer localhost:1025, I can see the email in my terminal.
However, how do I actually send the email not to the debugging server but to email@example.com?
After reading your answers, let me get something straight:
Can't you use localhost(simple ubuntu pc) to send e-mails?
I thought in django 1.3
send_mail()is somewhat deprecated and
EmailMessage.send()is used instead?
For Django version 1.7, if above solutions dont work then try the following
in settings.py add
#For email EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_USE_TLS = True EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = 'firstname.lastname@example.org' #Must generate specific password for your app in [gmail settings] EMAIL_HOST_PASSWORD = 'app_specific_password' EMAIL_PORT = 587 #This did the trick DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
The last line did the trick for django 1.7
You could use "Test Mail Server Tool" to test email sending on your machine or localhost. Google and Download "Test Mail Server Tool" and set it up.
Then in your settings.py:
EMAIL_BACKEND= 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'localhost' EMAIL_PORT = 25
from django.core.mail import send_mail send_mail('subject','message','sender email',['receipient email'], fail_silently=False)
In addition to the
DEFAULT_FROM_EMAIL fix others have mentioned, and allowing less-secure apps to access the account, I had to navigate to https://accounts.google.com/DisplayUnlockCaptcha while signed in as the account in question to get Django to finally authenticate.
I went to that URL through a SSH tunnel to the web server to make sure the IP address was the same; I'm not totally sure if that's necessary but it can't hurt. You can do that like so:
ssh -D 8080 -fN <username>@<host>, then set your web browser to use
localhost:8080 as a SOCKS proxy.
You need to use smtp as backend in settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
If you use backend as console, you will receive output in console
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
And also below settings in addition
EMAIL_USE_TLS = True EMAIL_HOST = 'smtp.gmail.com' EMAIL_PORT = 587 EMAIL_HOST_USER = 'email@example.com' EMAIL_HOST_PASSWORD = 'password'
If you are using gmail for this, setup 2-step verification and Application specific password and copy and paste that password in above EMAIL_HOST_PASSWORD value.