1

This is the following piece of code I have written in node.js for sending email with attachment using gmail api:

And I am using request-promise module of node js to send request to that api.

let user = await db.model('User').findOne({ _id: userId });
    let filepath = fs.readFileSync(req.file.path).toString('base64');
    // let filepath = fs.readFileSync(req.file.path);
    let from = user.crmOptions.email;
    let raw = [
      'Content-Type: multipart/mixed; boundary="boundary_mail"\r\n',
      'MIME-Version: 1.0\r\n',
      "to: ", req.body.to, "\n",
      "from: ", from, "\n",
      "cc: ", req.body.cc ? req.body.cc : '', "\n",
      "bcc: ", req.body.bcc ? req.body.bcc : '', "\n",
      "subject: ", req.body.subject, "\n\n",

      '--boundary_mail\r\n',
      "Content-Type: text/html; charset=\"UTF-8\"\n",
      'MIME-Version: 1.0\r\n',
      req.body.message,

      '--boundary_mail\r\n',
      `Content-Type: ${req.file.mimetype}\r\n`,
      'MIME-Version: 1.0\r\n',
      `Content-Disposition: attachment; filename="${req.file.filename}"\r\n\r\n`,

      filepath, '\r\n\r\n',

      '--boundary_mail--'
    ].join('');
    const id = 'me';
    let options = {
      url: "https://www.googleapis.com/upload/gmail/v1/users/" + id + "/messages/send?uploadType=multipart",
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${user.crmOptions.access_token}`,
        'Content-Type': 'message/rfc822'
      },
      body: raw
    };


await request(options).then(async body => {
  console.log("Body: ", body);
}).catch(err => {
  console.log("Error: ", err);
})

Mail content is going like this after sending mail

enter image description here

1 Answers1

1

When html mail and attachment file are sent, it uses multipart/mixed and multipart/alternative. The structure of request body is as follows.

  • multipart/mixed
    • multipart/alternative
      • html message
    • Attachment file

At that time, 2 boundaries are used in the request body.

Modified script:

Please modify raw as follows.

let raw = [
  'MIME-Version: 1.0\n',
  "to: ", req.body.to, "\n",
  "from: ", from, "\n",
  "cc: ", req.body.cc ? req.body.cc : '', "\n",
  "bcc: ", req.body.bcc ? req.body.bcc : '', "\n",
  "subject: ", req.body.subject, "\n",
  "Content-Type: multipart/mixed; boundary=boundary_mail1\n\n",

  "--boundary_mail1\n",
  "Content-Type: multipart/alternative; boundary=boundary_mail2\n\n",

  "--boundary_mail2\n",
  "Content-Type: text/html; charset=UTF-8\n",
  "Content-Transfer-Encoding: quoted-printable\n\n",
  req.body.message, "\n\n",
  "--boundary_mail2--\n",

  '--boundary_mail1\n',
  `Content-Type: ${req.file.mimetype}\n`,
  `Content-Disposition: attachment; filename="${req.file.filename}"\n`,
  "Content-Transfer-Encoding: base64\n\n",

  filepath, '\n',
  '--boundary_mail1--',
].join('');

Note:

  • In this modified script, it supposes that your current script can send email using Gmail API.

References:

Edit:

For example, when 2 attachment files are included in the request body, please modify from --boundary_mail1 to --boundary_mail1-- as follows. Please be careful whether there are no duplicated filenames.

From:

'--boundary_mail1\n',
`Content-Type: ${req.file.mimetype}\n`,
`Content-Disposition: attachment; filename="${req.file.filename}"\n`,
"Content-Transfer-Encoding: base64\n\n",

filepath, '\n',
'--boundary_mail1--',

To:

'--boundary_mail1\n',
`Content-Type: mimetype\n`, // mimetype
`Content-Disposition: attachment; filename="### filename1 ###"\n`, // filename1
"Content-Transfer-Encoding: base64\n\n",

filepath1, '\n', // filepath1
'--boundary_mail1\n',
`Content-Type: mimetype\n`, // mimetype
`Content-Disposition: attachment; filename="### filename2 ###"\n`, // filename2
"Content-Transfer-Encoding: base64\n\n",

filepath2, '\n', // filepath2
'--boundary_mail1--',
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Tanaike what is the solution for email with multiple attachments – Dinesh Sathrasala Dec 26 '18 at 03:38
  • @Dinesh Sathrasala I'm really sorry for my incomplete answer. I updated my answer. Could you please confirm it? – Tanaike Dec 26 '18 at 07:53
  • Tanaike when a mail is sent with html table along with attachment, table is not getting highlighted in mail but when I inspect mail body table is present. Problem is that its not shown in table format. How to fix it. – Dinesh Sathrasala Jan 02 '19 at 11:46
  • @Dinesh Sathrasala I'm sorry. In order to understand correctly your new question, can you post it as new question including more information? When you post it, it will help users think of about your new question. – Tanaike Jan 03 '19 at 00:55
  • Tanaike please check my question in new post https://stackoverflow.com/questions/54018048/gmail-sent-with-html-table-is-not-getting-highlighted-in-mail-when-attachment-is – Dinesh Sathrasala Jan 03 '19 at 07:28
  • @Dinesh Sathrasala Thank you for replying. I'm really sorry for my poor English skill. I couldn't understand about your situation from your question. But other users might be able to understand your question and resolve your issue. – Tanaike Jan 03 '19 at 08:30
  • Tanaike My question is simple when I am sending a html table in email body along with any attachment like image, table is not displayed in mail, but table content is displayed. But when i try sending mail with only html table, table is displayed in mail properly. You can check images that I have attached in my post in stackoverflow. – Dinesh Sathrasala Jan 03 '19 at 09:41
  • @Dinesh Sathrasala Thank you for replying. But I cannot see the vision of your situation from your reply and question yet. I'm sorry. – Tanaike Jan 04 '19 at 01:09
  • @Dinesh Sathrasala You want to send email with HTML body? In your script, your situation cannot be replicated. Can you provide a sample script for replicating your situation? In your case, are 2 sample scripts required? – Tanaike Jan 04 '19 at 08:29
  • Tanaike Script is same. I am using some module for text editor in front-end. In that module we can select table and fill data in table. – Dinesh Sathrasala Jan 04 '19 at 09:40
  • @Dinesh Sathrasala Thank you for replying. If I found the method for replicating your situation, I think that I can think of your issue. – Tanaike Jan 04 '19 at 11:48
  • Taniake Thank you for replying. Have you found any solution. – Dinesh Sathrasala Jan 07 '19 at 04:02
  • @Dinesh Sathrasala I cannot have the solution yet. I'm really sorry for my poor skill. – Tanaike Jan 07 '19 at 05:49
  • Tanaike Have you understood my issue or not. If not I will elaboarate my question in easy way. – Dinesh Sathrasala Jan 07 '19 at 06:44
  • Tanaike Now I have put two pictures in the post one picture is my expected output and other is what i am getting. please have a look at this post: https://stackoverflow.com/questions/54018048/gmail-with-html-table-in-body-is-not-getting-displayed-in-mail-but-table-data-is – Dinesh Sathrasala Jan 07 '19 at 07:39
  • Tanaike could you please have a look on the issue: https://stackoverflow.com/questions/54085023/gmail-with-multiple-attachments-not-working – Dinesh Sathrasala Jan 08 '19 at 06:52
  • Tanaike please have a look on the issue: https://stackoverflow.com/questions/54555904/reply-to-mailthread-with-attachment-through-gmail-api-not-working – Dinesh Sathrasala Feb 07 '19 at 06:13