Gmail API发件人未发送多个附件 [英] Gmail API sender not sending multiple attachments

查看:84
本文介绍了Gmail API发件人未发送多个附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Gmail API通过JavaScript发送电子邮件。文字加上一个附件可以正常工作。但是,当我尝试发送两个附件时,只有第一个附件被附加,而另一个附件没有任何附件。我构建消息的代码是:

I am using Gmail API to send an email in JavaScript. It's working fine for text plus one attachment. But when I try to send two attachments, only the first one gets attached, and nothing for the other one. My code for building the message is:

  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小附件附加两次,以避免与限制有关。我认为在以某种方式构建消息方面存在错误,但是无法确定在哪里。

After this I create a string from the array. The code above is just testing with attaching the same small attachment of 6k twice, to avoid anything to do with limits. I think I have an error in how I've built the message somehow, but can't work out where.

推荐答案

在您的第一个附件:

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

You are missing the trailing newline for the "content-transfer-encoding" header item.

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

I strongly suggest using an existing library to compose MIME message, so you don't have to worry about these details. See: https://www.npmjs.com/package/mimemessage

这篇关于Gmail API发件人未发送多个附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆