Django send_mail not working - No email delivered
Asked Answered
T

2

16

When the view that sends the email is used nothing happens, i then entered send_mail(...) into the python shell and it returned 1 but i didn't receive any emails.

This is my settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'Password1234!'
EMAIL_USE_TLS = True

This is the view:

def send_email(request):
    send_mail('Request Callback', 'Here is the message.', '[email protected]',
        ['[email protected]'], fail_silently=False)
    return HttpResponseRedirect('/')
Tighe answered 30/4, 2012 at 13:39 Comment(1)
Did you check your SPAM inbox ? Did you create the SPF record ? support.google.com/a/bin/answer.py?hl=en&answer=33786Arleyne
P
15

Adjust your settings thus:

DEFAULT_FROM_EMAIL = '[email protected]'
SERVER_EMAIL = '[email protected]'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'P@ssw0rd5'

Adjust your code:

from django.core.mail import EmailMessage

def send_email(request):
    msg = EmailMessage('Request Callback',
                       'Here is the message.', to=['[email protected]'])
    msg.send()
    return HttpResponseRedirect('/')
Pharisaism answered 30/4, 2012 at 14:32 Comment(1)
I only added DEFAULT_FROM_EMAIL and SERVER_EMAIL settings and it worked for me. What is wrong with send_mail() method?Lixivium
E
2

Google now provides a method to generate a password that you can use for applications that need to relay mail. its different from the password that you would use to login through webmail.

Sign in to Google and start using App Passwords. This allows you to use a 16 digit password to access google services including ability to send out email. Refer below

https://support.google.com/accounts/answer/185833?hl=en

Enginery answered 24/3, 2019 at 4:33 Comment(1)
Where does it say this is related to relaying emails? This is only in case of 2fa.Antepast

© 2022 - 2024 — McMap. All rights reserved.