I want to use send_mail in django, however it doesn't send any email. Here is my settings:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'MY-GMAIL-PASSWORD'
EMAIL_PORT = 465
EMAIL_USE_TLS = False
EMAIL_USE_SSL = True
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
SERVER_EMAIL = DEFAULT_FROM_EMAIL
Then, I run python manage.py shell
:
from django.conf import settings
from django.core.mail import send_mail
subject = 'Test Subject'
message = 'Test Message'
email_from = settings.EMAIL_HOST_USER
recipient_list = ['[email protected]']
send_mail(subject, message, email_from, recipient_list, fail_silently=False)
It doesn't send the email and it prints:
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Test Subject
From: [email protected]
To: [email protected]
Date: Mon, 09 Dec 2019 21:02:16 -0000
Message-ID: <157592533640.18842.5494330274157836181@thinkpad-e560>
Test Message
-------------------------------------------------------------------------------
1
which seems to have no errors. Why doesn't it send the email? What is wrong? Why doesn't it log any errors?
Following this page, I've tried a pure python way and it works fine:
import smtplib, ssl
smtp_server = "smtp.gmail.com"
port = 465
sender_email = "[email protected]"
password = 'MY-GMAIL-PASSWORD'
receiver_email = '[email protected]'
context = ssl.create_default_context()
server = smtplib.SMTP_SSL("smtp.gmail.com", port, context=context)
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, 'Test Message')
As I mentioned, this pure python way works fine! I'm confused.
What did I do wrong?
fail_silently=True
, see the docs forsend_mail
. – Daniel Rosemansend_mail
is outputting to the console then you probably have your email backend set to'django.core.mail.backends.console.EmailBackend'
. Are you certain you have the smtp backend in your settings? – Iain Shelvingtonconsole.EmailBackend
configured. Print the value ofsettings.EMAIL_BACKEND
just before callingsend_mail
– ivissani