Cannot pass “In-Reply-To” parameter to Microsoft Graph sendMail

自闭症网瘾萝莉.ら 提交于 2020-08-06 06:05:39

问题


I allow users to send emails with their Outlook account by using Microsoft Graph API, but it seems to be creating multiple threads on the other side.

When using Mailgun API to send the user emails, I am able to pass the In-Reply-To message header that references the previous message Message-ID, and threads are clustered properly by clients on the other side (Outlook/Gmail etc)

But when using Microsoft Graph API I try to pass the In-Reply-To and it is not accepted by the API

graph_url = 'https://graph.microsoft.com/v1.0'

headers = {
    'User-Agent': 'api/1.0',
    'Authorization': f'Bearer {outlook_token}',
    'Accept': 'application/json',
    'Content-Type': 'application/json'
}

# Create recipient list in required format.
recipient_list = [{'emailAddress': {'name': name, 'address': address}} for name, address in recipients]
reply_to_list = [{'emailAddress': {'name': name, 'address': address}} for name, address in reply_to]

# Create email message in required format.
email_object = {
    'message': {
        'subject': subject,
        'body': {
            'contentType': content_type,
            'content': body
        },
        'toRecipients': recipient_list,
        'replyTo': reply_to_list,
        'attachments': [{
            '@odata.type': '#microsoft.graph.fileAttachment',
            'contentBytes': b64_content.decode('utf-8'),
            'contentType': mime_type,
            'name': file.name
        }],
        'internetMessageHeaders': [
            {
                "name": "In-Reply-To",
                "value": in_reply_to
            },
        ]
    },
    'saveToSentItems': 'true'
}

# Do a POST to Graph's sendMail API and return the response.
request_url = f'{graph_url}/me/microsoft.graph.sendMail'

response = requests.post(url=request_url, headers=headers, json=email_object)

https://docs.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0

I get the following response:

{
    "error": {
        "code": "InvalidInternetMessageHeader",
        "message": "The internet message header name 'in-Reply-To' should start with 'x-' or 'X-'.",
        "innerError": {
            "request-id": "7f82b9f5-c345-4744-9f21-7a0e9d75cb67",
            "date": "2019-05-03T04:09:43"
        }
    }
}

Is there any way of having the emails sent in the same thread for recipients clients?


回答1:


You can't manipulate standard message headers this way. The internetMessageHeaders collection will only accept "custom headers". These are message headers that begin with x- i.e. x-some-custom-header).

In order to reply to a message, you need to use the /createReply endpoint:

POST https://graph.microsoft.com/v1.0/me/messages/{id-of-message}/createReply

This will generate a message with the appropriate headers. You can then update this message to add additional content/attachments before you send it:

PATCH https://graph.microsoft.com/v1.0/me/messages/{id-of-reply}

{
  "body": {
    "contentType": "HTML",
    "content": body
  }
}

POST https://graph.microsoft.com/v1.0/me/messages/{id-of-reply}/send


来源:https://stackoverflow.com/questions/55945615/cannot-pass-in-reply-to-parameter-to-microsoft-graph-sendmail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!