I'm trying to send an email with a larger pdf attachment, but I'm getting an error: Request Entity Too Large.
I believe this is because it's sending through the standard resource URI
POST /gmail/v1/users/userId/messages/send
Instead of the URI for media:
POST /upload/gmail/v1/users/userId/messages/send.
How can I get the gmail.users.messages.send method to use the media URI? Is there a specific parameter flag?
Any help is much appreciated.
@muratgozel You wouldn't happen to know this by any chance?
Just saw that you had some experience with the gmail api from #1327
Ah yes but I haven't ever tried to send large files. I would first try to use the google drive api to upload the file and get a link (or id) then attach the file id (or link) to the email as an attachment with gmail api. This was the first thing that comes to my mind because gmail doesn't support sending files over 25MB.
If the size of your file is under the 25MB then it should work with standard way as you referenced from #1327
馃
Thanks for the response @muratgozel!
I figured out my issue, most of the guides online I found said to send the message like this:
gmail.users.messages.send({
auth: oAuth2Client,
userId: 'me',
uploadType: 'multipart',
resource: {
raw: raw
}
})
Where raw is a base64 encoded MIME messages. This worked for most of the emails except for attachment files that were over a few MBs. Changing it to the following has been working:
gmail.users.messages.send({
auth: oAuth2Client,
userId: 'me',
uploadType: 'multipart',
media: {
mimeType: 'message/rfc822',
body: emailMIME
}
})
Where emailMIME is not base64 encoded. This also must change the URI endpoint to the media one.
Thank you @jackrvaughan . This works when attachments files are over 5MBs or so. Otherwise you would receive the typical: Error 413: Request Entity Too Large. if your attachments are sent through raw (base64 encoded MIME message).
ishwarrimal
If anyone is looking how to get the emailMIME for multiple attachments, even though the answer is on SO (or on this repo's issues), I took @ishwarrimal answer from another issue and modified it for multiple attachments:
function makeBody(mail, attachments) {
const boundary = yourBoundary__";
const nl = "\n";
let str = [
"MIME-Version: 1.0",
"From: " + mail.from,
"To: " + mail.to,
"Subject: " + mail.subject,
"Content-Type: multipart/related; boundary=" + boundary + nl,
"--" + boundary,
"Content-Type: text/html; charset=UTF-8",
"Content-Transfer-Encoding: 7bit" + nl,
mail.message
]
if(Array.isArray(attachments)){
for (let i = 0; i < attachments.length; i++) {
if(i < attachments.length - 1){
str[str.length - 1] = str[str.length - 1] + nl;
}
str.push(
"--" + boundary,
"--" + boundary,
"Content-Type: " + attachments[i].type + "; name=" + attachments[i].filename,
"Content-Disposition: attachment; filename=" + attachments[i].filename,
"Content-Transfer-Encoding: base64" + nl,
attachments[i].content,
);
if(i === attachments.length - 1){
str.push("--" + boundary + "--" );
}
}
}
return str.join(nl);
}
Even though I tried with different Content-Types (multipart/alternative or multipart/mixed), personally I prefer the one above (multipart/related).
I strongly suggest you to take a look at the original message in any mail ("Show original" option).
Most helpful comment
Thanks for the response @muratgozel!
I figured out my issue, most of the guides online I found said to send the message like this:
Where
rawis a base64 encoded MIME messages. This worked for most of the emails except for attachment files that were over a few MBs. Changing it to the following has been working:Where
emailMIMEis not base64 encoded. This also must change the URI endpoint to the media one.