How to send email via Django?
Solution 1
Send the email to a real SMTP server. If you don't want to set up your own then you can find companies that will run one for you, such as Google themselves.
Solution 2
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.
In settings.py:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
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
Solution 3
- Create a project:
django-admin.py startproject gmail
-
Edit settings.py with code below:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_USE_TLS = True EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = '[email protected]' EMAIL_HOST_PASSWORD = 'email_password' EMAIL_PORT = 587
Run interactive mode:
python manage.py shell
-
Import the EmailMessage module:
from django.core.mail import EmailMessage
-
Send the email:
email = EmailMessage('Subject', 'Body', to=['[email protected]']) email.send()
For more informations, check send_mail
and EmailMessage
features in documents.
UPDATE for Gmail
Also if you have problems sending email via gmail remember to check this guides from google.
In your Google account settings, go to Security > Account permissions > Access for less secure apps
and enable this option.
Also create an App specific password for your gmail after you've turned on 2-step-verification for it.
Then you should use app specific password in settings. So change the following line:
EMAIL_HOST_PASSWORD = 'your_email_app_specific_password'
Also if you're interested to send HTML email, check this out.
Solution 4
My site is hosted on Godaddy
and I have a private email registered on the same.
These are the settings which worked for me:
In settings.py:
EMAIL_HOST = 'mail.domain.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'abcdef'
DEFAULT_FROM_EMAIL = '[email protected]'
SERVER_EMAIL = '[email protected]'
EMAIL_PORT = 25
EMAIL_USE_TLS = False
In shell:
from django.core.mail import EmailMessage
email = EmailMessage('Subject', 'Body', to=['[email protected]'])
email.send()
Then I got "1" as the O/P i.e. Success. And I received the mail too. :)
- What is the meaning of domain.com?
Solution 5
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 = '[email protected]'
#Must generate specific password for your app in [gmail settings][1]
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
Related videos on Youtube
Derek
Updated on October 03, 2021Comments
-
Derek over 2 years
In my
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=['[email protected]']) 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 protected]?
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 andEmailMessage.send()
is used instead?
-
Ignacio Vazquez-Abrams almost 13 years1. You can use localhost if you have a SMTP server running there. 2. The exact mechanism is unimportant. The important part is that you have a SMTP server.
-
Derek almost 13 yearsso if I install postfix, I can send/receive emails? How do you set up postfix to do this?
-
Ignacio Vazquez-Abrams almost 13 yearsThat question is beyond the scope of this site.
-
suhailvs about 10 years
-
Phlip over 8 yearsYet SEO takes us to this page, Ignacio.
-
J0ANMM about 7 yearsThis video explains very nicely all the steps needed.
-
tread over 6 yearsDon't think that using a third party service is the correct answer. It is not. You can install a MTA like
sendmail
which will allow you to send email locally.
-
Ignacio Vazquez-Abrams almost 13 yearsThere's no need to use
smtplib
directly; Django will handle that part for you. -
Srikar Appalaraju almost 13 yearsoh is it! how would that be? Here I entirely bypass the default django
send_mail
function & use my own... -
Ignacio Vazquez-Abrams almost 13 years
send_mail()
is how you would do it. You still need to assemble the message yourself, but you don't have to worry about the SMTP bits. -
user984003 over 11 yearsAwesome, I just myself an email! The above article mentioned by miku was perfect. Note the small typo correction in the comments of the article. (And I just used my regular computer/localhost. I had not set anything else up before hand.)
-
ritratt over 11 yearsis there an alternative to leaving your password as a plaintext?
-
Jordan about 11 yearsYou could use an email service like Mandrill that will let you use a passphrase instead, although I'm not sure that's any more helpful for you. You could also use an encryption key that's installed on your server, and make the line something like EMAIL_HOST_PASSWORD = my_decrypt('abi304hubaushl9rchy2y9fd29')
-
Drew Shafer about 11 yearsput it in an environment variable. Then, EMAIL_HOST_PASSWORD = os.environ['MY_PASSWORD_THAT_YOU_CANT_KNOW']
-
eugene almost 11 yearsI used the your code verbatim. My gmail account has been blocked after a few days. My server probably sent less than 20 emails per day. Had anyone had a similar issue with google?
-
Jordan over 10 yearsOn a new account, maybe. You could also try using a service like Mandrill. It's basically the same, if you follow their SMTP instructions.
-
Alex Stewart almost 9 yearsThis one didn't work for me for some reason. I kept getting back an error with the password being wrong. Which is strange because i got the credentials direct from Mailgun.
-
Thomas Gak-Deluen over 8 yearsThank you ! This saved me a lot of time @Jordan
-
François Constant over 8 yearsThanks, that's the easiest way for old projects < Django 1.4
-
Bob Stein over 8 yearsSMTPAuthenticationError and I get an email "Sign-in attempt prevented ... from an app that doesn't meet modern security standards". Looks like this. Workaround by "turning on access for less secure apps". And that worked.
-
Bob Stein over 8 yearsLimited FROM address (3rd parameter to
send_mail()
. It can only be an email associated with your gmail account. -
aquaman over 7 yearsIs there any alternative to Sendgrid. Please suggest as I'm using subdomain of pythonanywhere.com.
-
Jmills over 7 yearsIs there a statement from Google saying that SMTP is disabled for personal/free accounts? This answer says Gmail is no longer allowing SMTP in 2016, but I can confirm the SMTP works just fine with Google Apps for Business if you enable it in the Gmail App settings and use an app token for the password of the user you want to send from.
-
SYK over 7 yearsExcellent, but haven't able to get it to work using 'apikey' as username as per documentation, and SendGrid list three dozens permission configurations per apikey for a simple sendmail...
-
qg_java_17137 over 6 yearsI follow your steps, I can not send, the to email do not receive email/
-
Antu over 5 yearswhat will be EMAIL_HOST for outlookoffice365 ?
-
CGFoX almost 4 yearsWhich article? Could you link it again?
-
Display name over 3 yearsDocumentation and set up have changed since then but the link to the documentation still works and takes you to the correct config.
-
Raushan Kumar almost 3 yearshow come this is the useful answer and different than others
-
Tyler2P over 2 yearsWhile this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.