Django EmailMessage not sending / timeout

im trying to use django.core.mail to send emails using the default backend and doesn't seem to work. I set the email credentials, server, and port number in the settings file, but whenever I try to run the send () method for an email message, the command freezes indefinitely.

+3
source share
2 answers

views.py

from django.core.mail import send_mail

def sending_email(request):
    message = ""
    subject = ""
    send_mail(subject, message, from_email, ['to_email',])

Add this to settings.py

# Sending mail
EMAIL_USE_TLS = True
EMAIL_HOST='smtp.gmail.com'
EMAIL_PORT=587
EMAIL_HOST_USER='your gmail account'
EMAIL_HOST_PASSWORD='your gmail password'
+3
source

I had the same problem when trying to send via smtp.gmail.com using use_tls = True. Turns out I had the wrong set of ports. Here is what I am doing now and it works:

from django.core.mail import get_connection
from django.core.mail.message import EmailMessage

connection = get_connection(use_tls=True, host='smtp.gmail.com', port=587,username='YourEmail@gmail.com', password='YourPassword')
EmailMessage('test', 'test', 'addr@from.com', ['addr@to.com'], connection=connection).send()
+2
source

All Articles