【问题标题】:Gmail API sender not sending multiple attachmentsGmail API 发件人未发送多个附件
【发布时间】:2019-03-08 18:44:15
【问题描述】:

我正在使用 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 小附件,以避免与限制有关。我认为我以某种方式构建消息的方式有误,但无法确定在哪里。

【问题讨论】:

    标签: email gmail-api email-attachments


    【解决方案1】:

    在您的第一个附件中:

     '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”标题项的尾随换行符。

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

    【讨论】:

    • 非常感谢,就是这样!我会试试你建议的 npm 包,因为是的,否则它非常繁琐
    猜你喜欢
    • 2017-11-19
    • 2014-10-14
    • 2018-08-27
    • 2020-12-09
    • 2020-03-28
    • 1970-01-01
    • 2018-11-21
    • 2016-07-19
    • 2016-05-29
    相关资源
    最近更新 更多