Upon using the Gmail API in Javascript to send a message with an HTML body and a ~100KB PDF attachment, the attachment correctly appears attached to the message in the sender's Gmail Sent folder, but does not appear on the message for the recipient.
The API call is a POST
to:
https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=media
The request body sent to the API is:
{
"headers": {
"Authorization": "Bearer authToken-removedForThisPost"
},
"method": "POST",
"contentType": "message/rfc822",
"contentLength": 134044,
"payload": "exampleBelow",
"muteHttpExceptions": true
}
This is what the payload looks like:
MIME-Version: 1.0
To: =?utf-8?B?TWlrZSBD?=<[email protected]>
CC: =?utf-8?B?TWlrZSBD?=<[email protected]>
BCC: =?utf-8?B??=<[email protected]>
From: =?utf-8?B?TWlrZSBxWXsd2lr?=<[email protected]>
Subject: =?utf-8?B?subjectLine-removedForThisPost?=
Content-Type: multipart/alternative; boundary=__boundary__
--__boundary__
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64
base64EncodedStringHere-removedForThisPost
--__boundary__
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: base64
base64EncodedStringHere-removedForThisPost
--__boundary__
Content-Type: application/pdf; name="File Name.pdf"
Content-Disposition: attachment; filename="File Name.pdf"
Content-Transfer-Encoding: base64
base64EncodedStringHere-removedForThisPost
--__boundary__--
Note: The Gmail API Uploading Attachments documentation states that when uploading a simple attachment (under 5MB) Content-Length
is required. I made it so that my code produces an integer value of the total number of bytes of the PDF attachment. However, I noticed that Content-Length
is not included in the payload.
I tried changing the multipart/alternative
Content-Type for the message to multipart/mixed
- this made it so that the PDF attachment IS correctly attached to the recipient's message, but the HTML body of the message is rendered as plain text (the HTML tags are shown) and there is an additional attachment called noname.html which includes the HTML content rendered as HTML.
I need to make it so that the email in the recipient's message has both an HTML-rendered body AND the PDF attachment.
Update: I uploaded examples of the raw email messages here. The sent message is on the left, and the received message is on the right.
boundary="__boundary__"
) and using the final boudnary (--__boundary__--
)? Try something like this and see if it works. – Tholle--__boundary__--
, but it was cut off when I pasted it here because the console.log message was truncated due to the super long attachment base64 string. As for the double quotes - I added them to the firstContent-Type:
line but it didn't change the behavior at all - it works the same with or without them. – Employee