如何使用Google Apps脚本代码创建草稿电子邮件(从985)? [英] How to use the Google Apps Script code for creating a Draft email (from 985)?

查看:273
本文介绍了如何使用Google Apps脚本代码创建草稿电子邮件(从985)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Issue 985 ,一个名为yurec )在#29提供了一些代码,以在Google Apps脚本中创建一个草稿(文字)电子邮件。有些人感谢他的好工作,并说它的测试和工作。



我有点困惑,如何使用它,哪些部分的代码需要改变(如id和threadId?)。



无论如何,最好我猜这个用法就像:



MailApp.createDraft(emailTo,subject,body ,{'name':'Bob from Example Ltd'});



但是无论什么作品都与我很好。感谢您的帮助。



工作代码 - 您只需根据所选答案设置Gmail API



  function createDraft(){

var forScope = GmailApp.getInboxUnreadCount(); // auth scope需要

var raw =
'主题:testing Draft \\\
'+
'To:my.test.account@gmail.com\\\
'+
'Content-Type:multipart / alternative; boundary = 1234567890123456789012345678\\\
'+
'testing Draft msg\\\
'+
'--1234567890123456789012345678 - \\\
';

var draftBody = Utilities.base64Encode(raw);

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


解决方案

您只需要在控制台中设置Gmail API



脚本编辑器中




  • 资源> 高级脚本服务 (截图1)

  • 点击 Google Developers Console 链接在窗口底部
    (截图2)



在开发者控制台中:




  • 向下滚动并找到 Gmail API ,然后将其打开。 (截图3)



关闭开发人员控制台

点击高级Google服务模式



点击确定应用程式,您都已设定好。






以下是使用


测试的代码

  function createDraft(){
try {
var forScope = GmailApp.getInboxUnreadCount(); // auth scope需要

var raw =
'主题:从应用程序脚本测试草案\''$
//'To:cyrus@mydomain.net\ n'+
'Content-Type:multipart / alternative; boundary = 1234567890123456789012345678\\\
'+
'testing Draft msg\\\
'+
'--1234567890123456789012345678 - \\\
';

var draftBody = Utilities.base64Encode(raw);

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.getContentText());
} catch(err){
Logger.log(err.lineNumber +' - '+ err);
}
}

截图1



截图2



截图3


In Issue 985, a developer named "yurec" (perhaps) in comment #29 provides some code to create a draft (text) email in Google Apps Script. Some others thank him for the good work and say that it's tested and working.

I'm a bit stumped as to how to use it and which parts of the code need changing (like "id" and "threadId"?).

Anyway, ideally I guess the usage would be like:

MailApp.createDraft(emailTo, subject, body, {'name':'Bob from Example Ltd'});

But whatever works is good with me. Thanks for any help here. Getting this to work will be fantastico.

Working Code - You just need to set up the Gmail API as per the selected answer

function createDraft() {

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

  var raw = 
      'Subject: testing Draft\n' + 
      'To: my.test.account@gmail.com\n' +
      'Content-Type: multipart/alternative; boundary=1234567890123456789012345678\n' +
      'testing Draft msg\n' + 
      '--1234567890123456789012345678--\n';

  var draftBody = Utilities.base64Encode(raw);

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

解决方案

You only need to setup the Gmail API in the console

In Script Editor:

  • Resources > Advanced Script Services (screenshot 1)
  • click on Google Developers Console link at the bottom of the window (screenshot 2)

In Developers Console:

  • Scroll down and find Gmail API and turn it on. (screenshot 3)

Close the Developers Console tab

Click Ok in the Advanced Google Services modal

Authorize the app, and you're all set.


Here is the code I tested with

function createDraft() {
  try{
  var forScope = GmailApp.getInboxUnreadCount(); // needed for auth scope

  var raw = 
      'Subject: testing Draft from Apps Script\n' + 
      //'To: cyrus@mydomain.net\n' +
      'Content-Type: multipart/alternative; boundary=1234567890123456789012345678\n' +
      'testing Draft msg\n' + 
      '--1234567890123456789012345678--\n';

  var draftBody = Utilities.base64Encode(raw);

  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.getContentText());
  }catch(err){
    Logger.log(err.lineNumber + ' - ' + err);
  }
}

Screenshot 1

Screenshot 2

Screenshot 3

这篇关于如何使用Google Apps脚本代码创建草稿电子邮件(从985)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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