'Django: send email via Office365
What I have:
- A domain purchased in webs.com (I'll call it contoso.com);
- An Office 365 account connected to that domain (my email address is something like
[email protected]); - A Django application hosted in Azure;
What I want:
I would like to configure Django in order to send emails using my [email protected] address.
What I'v done:
I tried this Django configuration:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.office365.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'xxxxxx'
but it didn't work. So, just for a test, I tried the free Google SMTP server:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'yyyyyy'
This time it works but, obviously, the email is sent from [email protected] and not from [email protected].
What's wrong in the previous configuration? Or maybe I need to make some changes in my Office 365 account?
Update
So far I've tried to receive emails by setting Django's ADMINS variable in this way:
ADMINS = [('Admin Name', '[email protected]')]
And then I send the email using the logging object:
logging.error("Hello admin!!")
As said, this works only when I use smtp.gmail.com. Today I tried to use the send_mail function:
send_mail(
'Subject here',
'Here is the message.',
'[email protected]',
['[email protected]'],
fail_silently=False,
)
And it works both using smtp.gmail.com and smtp.office365.com configuration. Unfortunatelly this can't be a valid solution for me since I need to notify the admin in case of an error or an exception. So, why the send_mail function works with smtp.office365.com and the ADMIN setting don't?
Solution 1:[1]
To send email using the Office365 smtp server, the "from" user and the "host" user must be the same. So I added this setting in Django in order to send email to the admins:
SERVER_EMAIL = EMAIL_HOST_USER
Solution 2:[2]
It not Django, but just confirm that the SMTP relay is working using PowerShell. Make sure the From address is the same as the account used to authenticate. Also try using SSL and see.


Solution 3:[3]
After trying for couple of days , the only thing worked for me is :
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
Solution 4:[4]
A bit of self promotion but I hope it helps:
SMTP authentication is not preferred these days, so I've made an email backend for Django which authenticates through the Office 365 API. You can find it here: https://pypi.org/project/django-o365mail/
Solution 5:[5]
This post is an synthesis of proposals found here.
As per April 2022 this complete configuration works like a charm for native django email configuration:
EMAIL_HOST = os.getenv("APP_EMAIL_HOST", "smtp.office365.com")
EMAIL_PORT = os.getenv("APP_EMAIL_PORT", 587)
EMAIL_HOST_USER = os.getenv("APP_EMAIL_HOST_USER")
SERVER_EMAIL = EMAIL_HOST_USER
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
EMAIL_HOST_PASSWORD = os.getenv("APP_EMAIL_HOST_PASSWORD")
EMAIL_USE_TLS = os.getenv("APP_EMAIL_USE_TLS", True)
EMAIL_TIMEOUT = os.getenv("APP_EMAIL_TIMEOUT", 60)
Then it is just about provisionning the email and password to your application. Additionnaly avoid password with # in environment variable in Linux as they are notably hard to escape.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Hannel |
| Solution 3 | Vaibhav singh |
| Solution 4 | Dharman |
| Solution 5 | jlandercy |
