evyd13 / django-o365mail

Office 365 (O365) email backend for Django
MIT License
21 stars 5 forks source link

400 Client Error: Bad Request Error Message: Resource not found for the segment 'sendMail'. #8

Open technqvi opened 1 year ago

technqvi commented 1 year ago

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"}}}

image

Below are my code and configuration and package installation.

image

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='smartapp-service@xxxxxgroup.com',
            recipient_list=['Taned.Sa@xxxxx.com'])
    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. image

Thank you for your advice

dejaeghered commented 1 year ago

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': 'o365mailbox@domain.com'}

technqvi commented 1 year ago

I got it done. One thing I missed is to add O365_MAIL_MAILBOX_KWARGS = {'resource': 'aaaaa@xxxxxgroup.com'} on settings.py

Thank you for supporting me.

technqvi commented 1 year ago

Ok

ijay99 commented 1 year ago

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.