【问题标题】:How to create draft message with attachment using gmail API in Node.JS?如何在 Node.JS 中使用 gmail API 创建带有附件的草稿消息?
【发布时间】:2020-04-24 01:28:22
【问题描述】:

我正在使用 Gmail API ,需要创建带有附件的新草稿,我正在关注官方文档:https://developers.google.com/gmail/api/v1/reference/users/drafts/create


    let response2 = await gmail.users.drafts.create({
        'userId': 'me',
        'resource': {
            'message': {
                'raw': payload
            }
        }
    });

此 sn-p 在 Gmail 中创建草稿邮件,但无法从我的本地计算机附加文件

我可以从本地找到 enter code hereattached 文件的 partId 和 Parts Array

有效载荷参考:https://www.any-api.com/googleapis_com/gmail/docs/Definitions/MessagePart

// -- -- payload for post request -- -- //
// ------------------------------------ //
{
  "body": {
    "attachmentId": "",
    "data": "",
    "size": 0
  },
  "filename": "",
  "headers": [
    {
      "name": "",
      "value": ""
    }
  ],
  "mimeType": "",
  "partId": "",
  "parts": [
    {
      "body": {},
      "filename": "",
      "headers": [
        null
      ],
      "mimeType": "",
      "partId": "",
      "parts": [
        null
      ]
    }
  ]
}

【问题讨论】:

    标签: javascript node.js google-api gmail-api


    【解决方案1】:

    经过一番研究,我找到了一个在使用 Gmail API 创建草稿时附加图片的解决方案,我从这个来源得到了提示 Sending mail with attachment

    这是完整的工作示例:

    第 1 步:

    function makeBody(subject, message, receiverId) {
        var boundary = "__myapp__";
        var nl = "\n";
        var attach = new Buffer(fs.readFileSync(__dirname + "/../" + fileName)).toString("base64");
        // console.dir(attach);
        var str = [
    
            "MIME-Version: 1.0",
            "Content-Transfer-Encoding: 7bit",
            "to: " + receiverId,
            "subject: " + subject,
            "Content-Type: multipart/alternate; boundary=" + boundary + nl,
            "--" + boundary,
            "Content-Type: text/plain; charset=UTF-8",
            "Content-Transfer-Encoding: 7bit" + nl,
            message + nl,
            "--" + boundary,
            "--" + boundary,
            "Content-Type: Application/pdf; name=myPdf.pdf",
            'Content-Disposition: attachment; filename=myPdf.pdf',
            "Content-Transfer-Encoding: base64" + nl,
            attach,
            "--" + boundary + "--"
    
        ].join("\n");
    
        var encodedMail = new Buffer(str).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
        return encodedMail;
    }
    

    第 2 步:


    const auth = await authorize(accessToken);
    const gmail = google.gmail({
        version: 'v1',
        auth
    });
    var rawText = makeBody("This is subject", "This is message", "test@gmail.com");
    var res = await gmail.users.drafts.create({
        'userId': 'me',
        'resource': {
            'message': {
                'raw': rawText
            }
        }
    });
    

    也可以使用这个npm包mimemessage

    【讨论】:

      猜你喜欢
      • 2019-04-25
      • 2017-06-17
      • 2019-12-06
      • 2010-11-15
      • 1970-01-01
      • 2016-06-10
      • 2018-04-19
      • 2021-04-27
      • 2018-02-20
      相关资源
      最近更新 更多