Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Node.JS中使用gmail API创建带有附件的草稿邮件?_Javascript_Node.js_Google Api_Gmail Api - Fatal编程技术网

Javascript 如何在Node.JS中使用gmail API创建带有附件的草稿邮件?

Javascript 如何在Node.JS中使用gmail API创建带有附件的草稿邮件?,javascript,node.js,google-api,gmail-api,Javascript,Node.js,Google Api,Gmail Api,我正在使用Gmail API,需要创建带有附件的新草稿,我遵循官方文档: 此代码段在Gmail中创建了草稿邮件,但无法从本地计算机附加该文件 在这里,我可以找到partId和Parts数组,并在此处输入代码,附件来自本地 有效载荷参考: //-post请求的有效负载--// // ------------------ // { 正文:{ 附件:, 数据:, 尺寸:0 }, 文件名:, 标题:[ { 姓名:, 价值: } ], mimeType:, 党:, 部分:[ { 正文:{}, 文件名:,

我正在使用Gmail API,需要创建带有附件的新草稿,我遵循官方文档:

此代码段在Gmail中创建了草稿邮件,但无法从本地计算机附加该文件

在这里,我可以找到partId和Parts数组,并在此处输入代码,附件来自本地

有效载荷参考:

//-post请求的有效负载--// // ------------------ // { 正文:{ 附件:, 数据:, 尺寸:0 }, 文件名:, 标题:[ { 姓名:, 价值: } ], mimeType:, 党:, 部分:[ { 正文:{}, 文件名:, 标题:[ 无效的 ], mimeType:, 党:, 部分:[ 无效的 ] } ]
} 经过一些研究,我找到了一个解决方案,在使用Gmail API创建草稿时附加一个图像,我从这个来源得到了提示

这是完整的工作示例:

步骤1:

步骤2:


也可以使用这个npm包

经过一些研究,我找到了一个解决方案,在使用Gmail API创建草稿时附加一个图像,我从这个来源得到了提示

这是完整的工作示例:

步骤1:

步骤2:

也可以使用此npm包

    let response2 = await gmail.users.drafts.create({
        'userId': 'me',
        'resource': {
            'message': {
                'raw': payload
            }
        }
    });
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;
}
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
        }
    }
});