65👍
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.
183👍
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 = 'me@gmail.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
- [Django]-How to set a value of a variable inside a template code?
- [Django]-Django-social-auth django-registration and django-profiles — together
- [Django]-Pytest.mark.parametrize with django.test.SimpleTestCase
55👍
- 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 = 'youremail@gmail.com' 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=['your@email.com']) 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.
- [Django]-How to add multiple objects to ManyToMany relationship at once in Django ?
- [Django]-Can I access constants in settings.py from templates in Django?
- [Django]-How to get getting base_url in django template
18👍
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 = 'abc@domain.com'
EMAIL_HOST_PASSWORD = 'abcdef'
DEFAULT_FROM_EMAIL = 'abc@domain.com'
SERVER_EMAIL = 'abc@domain.com'
EMAIL_PORT = 25
EMAIL_USE_TLS = False
In shell:
from django.core.mail import EmailMessage
email = EmailMessage('Subject', 'Body', to=['def@domain.com'])
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?
- [Django]-What's the difference between CharField and TextField in Django?
- [Django]-Visual Editor for Django Templates?
- [Django]-Testing nginx without domain name
18👍
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 = 'sender@gmail.com'
#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
- [Django]-Stack trace from manage.py runserver not appearing
- [Django]-How to deal with "SubfieldBase has been deprecated. Use Field.from_db_value instead."
- [Django]-Django-allauth: Linking multiple social accounts to a single user
11👍
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 = 'urusername@gmail.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.
- [Django]-">", "<", ">=" and "<=" don't work with "filter()" in Django
- [Django]-In django, how do I sort a model on a field and then get the last item?
- [Django]-Django Background Task
5👍
I found using SendGrid to be the easiest way to set up sending email with Django. Here’s how it works:
- Create a SendGrid account (and verify your email)
- Add the following to your
settings.py
:
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = '<your sendgrid username>'
EMAIL_HOST_PASSWORD = '<your sendgrid password>'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
And you’re all set!
To send email:
from django.core.mail import send_mail
send_mail('<Your subject>', '<Your message>', 'from@example.com', ['to@example.com'])
If you want Django to email you whenever there’s a 500 internal server error, add the following to your settings.py
:
DEFAULT_FROM_EMAIL = 'your.email@example.com'
ADMINS = [('<Your name>', 'your.email@example.com')]
Sending email with SendGrid is free up to 12k emails per month.
- [Django]-Django Cache cache.set Not storing data
- [Django]-Django DRF with oAuth2 using DOT (django-oauth-toolkit)
- [Django]-What is the use of PYTHONUNBUFFERED in docker file?
4👍
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 shell:
from django.core.mail import send_mail
send_mail('subject','message','sender email',['receipient email'], fail_silently=False)
- [Django]-Http POST drops port in URL
- [Django]-How can I upgrade specific packages using pip and a requirements file?
- [Django]-Removing 'Sites' from Django admin page
3👍
I had actually done this from Django a while back. Open up a legitimate GMail account & enter the credentials here. Here’s my code –
from email import Encoders
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
def sendmail(to, subject, text, attach=[], mtype='html'):
ok = True
gmail_user = settings.EMAIL_HOST_USER
gmail_pwd = settings.EMAIL_HOST_PASSWORD
msg = MIMEMultipart('alternative')
msg['From'] = gmail_user
msg['To'] = to
msg['Cc'] = 'you@gmail.com'
msg['Subject'] = subject
msg.attach(MIMEText(text, mtype))
for a in attach:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition','attachment; filename="%s"' % os.path.basename(a))
msg.attach(part)
try:
mailServer = smtplib.SMTP("smtp.gmail.com", 687)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, [to,msg['Cc']], msg.as_string())
mailServer.close()
except:
ok = False
return ok
- [Django]-Django queryset filter – Q() | VS __in
- [Django]-How do I POST with jQuery/Ajax in Django?
- [Django]-Django Framework – Is there a shutdown event that can be subscribed to?
2👍
Late, but:
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.
- [Django]-Django's Double Underscore
- [Django]-Convert Django Model object to dict with all of the fields intact
- [Django]-How to show processing animation / spinner during ajax request?
1👍
For SendGrid – Django Specifically:
Set these variables in
settings.py
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'sendgrid_username'
EMAIL_HOST_PASSWORD = 'sendgrid_password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
in views.py
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', 'from@example.com', ['to@example.com'], fail_silently=False)
- [Django]-'pip' is not recognized as an internal or external command
- [Django]-How to serve media files on Django production environment?
- [Django]-Django: How to manage development and production settings?
1👍
below formate worked for me
EMAIL_BACKEND = ‘django.core.mail.backends.smtp.EmailBackend’
EMAIL_USE_TLS = True EMAIL_HOST = ‘mail.xxxxxxx.xxx’
EMAIL_PORT = 465
EMAIL_HOST_USER = ‘support@xxxxx.xxx’
EMAIL_HOST_PASSWORD = ‘xxxxxxx’
- [Django]-Django – how to unit test a post request using request.FILES
- [Django]-Combining Django F, Value and a dict to annotate a queryset
- [Django]-Creating email templates with Django
-1👍
In settings.py configure the email as following
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'user@gmail.com'
EMAIL_HOST_PASSWORD = 'password'
In most of the cases Gmail settings is missed out. Make sure that the less secure app access is turned on. You can also check the procedure here
Then send email within views
from django.core.mail import send_mail
def send(request):
send_mail(
‘Email Subject here’,
‘Email content’,
settings.EMAIL_HOST_USER,
[‘emailto@gmail.com’],
fail_silently=False)
- [Django]-Django proxy model and ForeignKey
- [Django]-Django-tables2: How to use accessor to bring in foreign columns?
- [Django]-Django override save for model only in some cases?