Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading