I'm trying to send email in Django using a distribution group as a the DEFAULT_FROM_EMAIL setting. I am using Office 365 to send it. I have wrestled with the package called django-o365mail and python-o365to get it to a functional state. The documentation for that was pathetic enough. I'm not interested in learning miniscule details to send an email message via Office 365 or any other third party platform that believes it has all the answers. It ought to be straightforward, and we have chosen as an industry to make it hard. [Rant over.]
Here are my appropriate settings:
DEFAULT_FROM_EMAIL = "[email protected]"
EMAIL_BACKEND = 'django_o365mail.EmailBackend'
O365_MAIL_CLIENT_ID = env('O365_MAIL_CLIENT_ID')
O365_MAIL_CLIENT_SECRET = env('O365_MAIL_CLIENT_SECRET')
O365_MAIL_TENANT_ID = env('O365_MAIL_TENANT_ID')
O365_ACTUALLY_SEND_IN_DEBUG = True
O365_MAIL_MAILBOX_KWARGS = {'resource': '[email protected]'}
I have tried various ways of achieving my goal, and here are some error messages to guide your thought processes on an incorrect path that you may believe to be the solution to everything. Remember, I just want to send an email to a distribution group from that very group.
404 Client Error: Not Found for url: https://graph.microsoft.com/v1.0/users/[email protected]/sendMail | Error Message: The requested user '[email protected]' is invalid.
I received this error when setting DEFAULT_FROM_EMAIL to '[email protected]' and the O365_MAIL_MAILBOX_KWARGS as '[email protected]'.
403 Client Error: Forbidden for url: https://graph.microsoft.com/v1.0/users/[email protected]/sendMail | Error Message: The user account which was used to submit this request does not have the right to send mail on behalf of the specified sending account., Cannot submit message.
Now, as I write this, I see that the issue is probably with the URL having users instead of groups. I cannot locate anywhere in any documentation of either project how to modify that. I greatly appreciate your assistance.
This has be set to a Mailbox that has sendAs rights on the Distribution list, its a little bit tricky to understand when your using a Service Principal for authentication that is impersonating a Mailbox (what you have set inO 365_MAIL_MAILBOX_KWARGS) so the permissions that apply to the Mailbox (eg can it send as the distribution) will affect whether you can send as that DL if no send As permission have been granted on the DL that it won't work. The rules that apply in https://learn.microsoft.com/en-us/graph/outlook-send-mail-from-other-user are also important so the from address to the DL if your doing a SendAS or From to the DL and Sender to the Mailbox if you doing Send on Behalf.