A J
A J

Reputation: 11

Send multipart gmail email which is huge attachments

I am trying to send an email which has attachments in it, using Gmail APIs, I am using NodeJS, I have read this document

https://developers.google.com/gmail/api/guides/uploads#multipart

But I am not sure what I am missing in headers, the following is the code

I have to send an email which has an attachment which is more than 25 MB so for that, I am using multipart of Gmail, following is the code

const options = {
    uri: 'https://www.googleapis.com/upload/gmail/v1/users/[email protected]/messages/send?uploadType=multipart',
    method: apiConfig.method,
    media: {mimeType: 'message/rfc822', body: requestParameter.dataObj.raw},
    headers: {
        Authorization: 'Bearer ' + token,
        'Content-type': 'message/rfc822'
    },
    json: true
}

Upvotes: 0

Views: 602

Answers (1)

ale13
ale13

Reputation: 6072

According to the Send attachments with your Gmail documentation:

You can send up to 25 MB in attachments. If you have more than one attachment, they can't add up to more than 25 MB.

If your file is greater than 25 MB, Gmail automatically adds a Google Drive link in the email instead of including it as an attachment.

But the last sentence refers to the Gmail UI, and not if you are using the API.

So essentially you cannot upload directly the attachment to the Gmail - you will have to upload it to Google Drive first and afterwards send it in the e-mail.

A possible solution:

  1. Upload the file which you want to send to Google Drive by using the Google Drive API v3. Since you want to upload a file bigger than 25 mb you should use the resumable upload. The resumable upload is a more reliable type of transfer and especially important with large files.
var options = {
        url: 'https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable',
        headers: {
            'Authorization': 'Bearer ' + token,
            'Content-Type': 'application/json',
        },
        //other options
}

  1. Retrieve the file from Drive by using the Drive API and set the permissions necessary to be able to share the file. Afterwards you have to use the NPM module async to synchronize the changes in the permissions.
var fileId = 'ID_OF_THE_FILE';
var permissions = [
  {
    'type': 'user',
    'role': 'writer',
    'emailAddress': '[email protected]'
  }, {
    'type': 'domain',
    'role': 'writer',
    'domain': 'example.com'
  }
];
  1. Send the e-mail with the link for the file wanted in the body.

Note: you also have to authorize the necessary scopes both for Drive and Gmail.

Moreover, I suggest you check the following links since they might be of help:

Upvotes: 1

Related Questions