pythonpython-3.xdjangoemaildjango-email

Django is unable to send email out even though conf is correct


Django is unable to send email out.

But https://www.smtper.net/ was able to send a test email with same exact settings, user, pass.

What do I need do more in django to send email out?

settings.py

## #environment variables from .env.
from dotenv import load_dotenv
load_dotenv()
NOREPLYEMAIL = os.getenv('NOREPLYEMAIL') 
NOREPLYEMAILPASS = os.getenv('NOREPLYEMAILPASS')

### Email config
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST =  'smtppro.zoho.com' 
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = NOREPLYEMAIL
EMAIL_HOST_PASSWORD = NOREPLYEMAILPASS
DEFAULT_FROM_EMAIL = NOREPLYEMAIL

view.py

# using https://docs.djangoproject.com/en/5.0/topics/email/

@csrf_protect
def test(request):
    testemail = EmailMessage(
    "Hello", #subject
    "Body goes here", #message body
    NOREPLYEMAIL, #from
    ["mygmail@gmail.com",], #to
    reply_to=[NOREPLYEMAIL], 
    headers={"Message-ID": "test"},
    )
    testemail.send()

console

Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Hello
From: noreply@mydomain.com
To: mygmail@gmail.com
Reply-To: noreply@mydomain.com
Date: Tue, 02 Jul 2024 11:38:16 -0000
Message-ID: test

Body goes here
-------------------------------------------------------------------------------
[02/Jul/2024 17:08:16] "GET /test/ HTTP/1.1" 200 13102

Solution

  • You are using a console.EmailBackend backend which is used in dev mode for testing purposes.

    To send emails via SMTP server you need to set the email backend to

    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    

    read more in docs