0

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/xyz@gmail.com/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
}
Naor Levi
  • 1,713
  • 1
  • 13
  • 27
A J
  • 11
  • 1
  • 4
  • Can I ask you about your issue? And can you explain about `huge attachments`? – Tanaike Jan 28 '20 at 01:20
  • It is giving me Invalid multipart request with 0 mime parts. @Tanaike – A J Jan 28 '20 at 03:21
  • Thank you for replying. Can you provide the whole script for replicating your issue? Of course, please remove your personal information. By the way, can you explain about `huge attachments`? – Tanaike Jan 28 '20 at 04:41
  • Thanks for the reply, yes I will explain to you, 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, I have updated the description @Tanaike – A J Jan 28 '20 at 06:54
  • Thank you for replying. Are these information useful for your situation? https://stackoverflow.com/search?q=gmail+25+MB+attachment – Tanaike Jan 28 '20 at 07:07
  • I have referred this link https://stackoverflow.com/questions/55639575/send-large-attachments-5-mb-with-the-gmail-api-node-js-client But I am not sure what is the format of the mimemessage, as I am getting error as "Recipient address required" Not sure @Tanaike – A J Jan 28 '20 at 07:10
  • Thank you for replying. I apologize that my comments were not useful for your situation. I would like to resolve your issue. But I cannot understand about your script for replicating your issue. So when I could correctly understand your script, I would like to think of the issue and solution. This is due to my poor skill. I deeply apologize for this. – Tanaike Jan 28 '20 at 07:15

1 Answers1

1

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': 'user@example.com'
  }, {
    '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:

Community
  • 1
  • 1
ale13
  • 5,679
  • 3
  • 10
  • 25