Email with attachment is not going properly with gmail api

*爱你&永不变心* 提交于 2019-12-24 06:04:34

问题


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


回答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:

  • https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
  • http://qcode.co.uk/post/70

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--',


来源:https://stackoverflow.com/questions/53886947/email-with-attachment-is-not-going-properly-with-gmail-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!