使用 Google Apps 脚本和 Gmail API 将附件添加到 Gmail 草稿 [英] Adding attachment to Gmail draft with Google Apps Script and Gmail API

查看:16
本文介绍了使用 Google Apps 脚本和 Gmail API 将附件添加到 Gmail 草稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在使用 Google Apps Script 和 GMail API 创建的 Gmail 草稿中自动附加来自我的 Google Drive 的文件(最好使用文件 ID).我使用下面的语法.我可以轻松做到吗?顺便说一句,创建草稿效果很好.

I try to automatically attach a file from my Google Drive (so ideally with the file id) in my Gmail draft created with Google Apps Script and GMail API. I use the syntax below. Can I do that easily? Creating the draft works great by the way.

谢谢!克里斯

  function createDraft() {

  var forScope = GmailApp.getInboxUnreadCount(); // needed for auth scope

  var htmlBody = 'Howzit';

  var raw = 
      'Subject: Howzit
' + 
      'To: aa@bb.cc
' +
      'Content-Type: text/html; charset=UTF-8
' +
      '
' + htmlBody;

  var draftBody = Utilities.base64Encode(raw, Utilities.Charset.UTF_8).replace(///g,'_').replace(/+/g,'-');

  var params = {method:"post",
                contentType: "application/json",
                headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
                muteHttpExceptions:true,
                payload:JSON.stringify({
                  "message": {
                    "raw": draftBody
                  }
                })
               };

  var resp = UrlFetchApp.fetch("https://www.googleapis.com/gmail/v1/users/me/drafts", params);

}

推荐答案

下面的示例脚本怎么样?这是一个用于将文件附加到草稿的非常简单的脚本.所以请根据您的环境进行修改.

How about following sample script? This is a very simple script for attaching a file to a draft. So please modify this to your environment.

要使用此脚本,请在 API 控制台 启用 Gmail API.并且请在脚本中将文件ID导入到fileId中.

In order to use this script, please enable Gmail API at API console. And please import file ID to fileId in the script.

function createDraft() {
  var fileId = "### file id ###";
  var file = DriveApp.getFileById(fileId);
  var forScope = GmailApp.getInboxUnreadCount();
  var htmlBody = 'Howzit';
  var raw = 
      'Subject: Howzit
' + 
      'To: aa@bb.cc
' +
      'Content-Type: multipart/mixed; boundary=##########

' +
      '--##########
' +
      'Content-Type: text/html; charset=UTF-8

' + htmlBody + '
' +
      '--##########
' +
      'Content-Type: ' + file.getMimeType() + '; charset=UTF-8; name="' + file.getName() + '"
' +
      'Content-Disposition: attachment; filename="' + file.getName() + '"
' +
      'Content-Transfer-Encoding: base64

' + Utilities.base64Encode(file.getBlob().getBytes()) +
      '
--##########
';
  var draftBody = Utilities.base64EncodeWebSafe(raw, Utilities.Charset.UTF_8);
  var params = {
    method:"post",
    contentType: "application/json",
    headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
    muteHttpExceptions: true,
    payload: JSON.stringify({"message": {"raw": draftBody}})
  };
  var resp = UrlFetchApp.fetch("https://www.googleapis.com/gmail/v1/users/me/drafts", params);
  Logger.log(resp)
}

结果:

{
  "id": "#####",
  "message": {
    "id": "#####",
    "threadId": "#####",
    "labelIds": [
      "DRAFT"
    ]
  }
}

图片:

这篇关于使用 Google Apps 脚本和 Gmail API 将附件添加到 Gmail 草稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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