django-o365mail
django-o365mail copied to clipboard
400 Client Error: Bad Request Error Message: Resource not found for the segment 'sendMail'.
Hi
I try to setup up the package as your advice as a prior question. this link https://github.com/evyd13/django-o365mail/issues/7
400 Client Error: Bad Request for url: https://graph.microsoft.com/v1.0/sendMail | Error Message: Resource not found for the segment 'sendMail'.
I click to https://graph.microsoft.com/v1.0/sendMail in order to dig deeper into additional error
{"error":{"code":"InvalidAuthenticationToken","message":"Access token is empty.","innerError":{"date":"2022-11-24T13:45:25","request-id":"4b533efc-d806-4829-a506-ba03599ebd69","client-request-id":"4b533efc-d806-4829-a506-ba03599ebd69"}}}

Below are my code and configuration and package installation.
settings.py
EMAIL_BACKEND = 'django_o365mail.EmailBackend'
O365_MAIL_CLIENT_ID ='xxxxxxxxxxxxxxx'
O365_MAIL_CLIENT_SECRET ='yyyyzzzyyyyy'
O365_MAIL_TENANT_ID ='zzzzzzzzz'
O365_ACTUALLY_SEND_IN_DEBUG=True
views.py
from django.core.mail import BadHeaderError, send_mail
from django.http import HttpResponse
def send_mail_office365(request):
try:
send_mail(
subject='SmartApp - Test Sending Email',
message='Hi, ok',
from_email='[email protected]',
recipient_list=['[email protected]'])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return render(request,
'app/pm_doc_gen_pdf.html',
{})
The last picture is what the azure administrator of my company set up.
Thank you for your advice
The error message is related to the Office 365 and not to this package. I assume somewhere your permissions or authentication is not correct. Check the graphql documentation from Microsoft on this specific error
Also dont forget to set the mailbox! O365_MAIL_MAILBOX_KWARGS = {'resource': '[email protected]'}
I got it done. One thing I missed is to add O365_MAIL_MAILBOX_KWARGS = {'resource': '[email protected]'} on settings.py
Thank you for supporting me.
Ok
I think the need to add the O365_MAIL_MAILBOX_KWARGS should be added to the documentation as it is not obvious that this is required to someone using the package for the first time. I had to also add to the Django app settings.py the e-mail for the DEFAULT_FROM_EMAIL and the SERVER_MAIL setting using the format DEFAULT_FROM_EMAIL='your email here'. Without this I was getting an error that the user account which submitted the request didn't have the right to send the email on behalf of the specified sending account. The e-mail in these two settings is the same e-mail I specified in the O365_MAIL_MAILBOX_KWARGS.