Gmail API 发件人未发送多个附件

Gmail API sender not sending multiple attachments

我正在使用 Gmail API 在 JavaScript 中发送电子邮件。它适用于文本加一个附件。但是当我尝试发送两个附件时,只有第一个被附加,而另一个没有。我构建消息的代码是:

  var nl = '\n';
  var boundary = "__myapp__";

const messageParts = [
        'MIME-Version: 1.0',
        'Content-Transfer-Encoding: 7bit',
        'From: XXXX Support <XXXXX@XXXXX.XXXXX>',
        'To: Moin <' + event.email + '>',
        'subject: ' + utf8Subject,
        'Content-Type: multipart/mixed; boundary=' + boundary + nl,
        '--' + boundary,
        'Content-Type: text/plain; charset=UTF-8',
        'Content-Transfer-Encoding: 7bit' + nl,
        messageBody+ nl,
        '--' + boundary,
        'Content-Type: Application/pdf; name=' + testFileName,
        'Content-Disposition: attachment; filename=' + testFileName,
        'Content-Transfer-Encoding: base64' + nl,
        testFile.Body.toString('base64'),
        '--' + boundary,
        'Content-Type: Application/pdf; name=' + testFileName,
        'Content-Disposition: attachment; filename=' + testFileName,
        'Content-Transfer-Encoding: base64',
        testFile.Body.toString('base64'),
        '--' + boundary + '--'
      ]

在此之后,我从数组中创建了一个字符串。上面的代码只是通过两次附加相同的 6k 小附件进行测试,以避免与限制有关。我认为我以某种方式构建消息的方式有误,但无法确定位置。

在您的第一个附件中:

 'Content-Type: Application/pdf; name=' + testFileName,
    'Content-Disposition: attachment; filename=' + testFileName,
    'Content-Transfer-Encoding: base64' + nl,
    testFile.Body.toString('base64'),
    '--' + boundary,

在你的第二个附件中:

    'Content-Type: Application/pdf; name=' + testFileName,
    'Content-Disposition: attachment; filename=' + testFileName,
    'Content-Transfer-Encoding: base64',
    testFile.Body.toString('base64'),

您缺少 "content-transfer-encoding" header 项的结尾换行符。

我强烈建议使用现有的库来编写 MIME 消息,这样您就不必担心这些细节。参见:https://www.npmjs.com/package/mimemessage