gpt4 book ai didi

google-apps-script - 如何使用谷歌应用程序脚本发送电子邮件草稿

转载 作者:行者123 更新时间:2023-12-03 21:35:12 30 4
gpt4 key购买 nike

我正在使用 Google 应用程序脚本,并希望创建一个脚本,该脚本从草稿中提取邮件并在它们具有“明天发送”标签时发送它们。
查找带有特定标签的草稿非常简单:

 var threads = GmailApp.search('in:draft label:send-tomorrow');

但是我没有看到发送消息的 API!
我看到的唯一选择是:
- 打开消息
- 提取正文/附件/标题/从/到/抄送/密件抄送
- 使用上述参数发送一条新消息
- 销毁之前的草稿

这看起来很烦人,我不确定嵌入图像、多个附件等是否能很好地工作......

任何提示?

最佳答案

The only option I see is to: - open the message - extract body/attachments/title/from/to/cc/bcc - send a new message with the above params - destroy the previous draft



这是 this blog的确切主题作者:阿米特·阿加拉瓦尔他的脚本只是按照您的描述执行,但不处理内嵌图像。对于那些,您可以修改来自 this article 的代码。 .

但你是对的 - 如果你不能只发送愚蠢的东西,那么即使有一个草稿消息又有什么意义呢?!

我们可以使用 GMail API Users.drafts: send从 Google Apps 脚本发送草稿。以下独立脚本执行此操作,并处理必要的授权。

脚本

完整脚本可在 this gist 中获得.

/*
* Send all drafts labeled "send-tomorrow".
*/
function sendDayOldDrafts() {
var threads = GmailApp.search('in:draft label:send-tomorrow');

for (var i=0; i<threads.length; i++) {
var msgId = threads[0].getMessages()[0].getId();
sendDraftMsg( msgId );
}
}


/**
* Sends a draft message that matches the given message ID.
* Throws if unsuccessful.
* See https://developers.google.com/gmail/api/v1/reference/users/drafts/send.
*
* @param {String} messageId Immutable Gmail Message ID to send
*
* @returns {Object} Response object if successful, see
* https://developers.google.com/gmail/api/v1/reference/users/drafts/send#response
*/
function sendDraftMsg( msgId ) {
// Get draft message.
var draftMsg = getDraftMsg(msgId,"json");
if (!getDraftMsg(msgId)) throw new Error( "Unable to get draft with msgId '"+msgId+"'" );

// see https://developers.google.com/gmail/api/v1/reference/users/drafts/send
var url = 'https://www.googleapis.com/gmail/v1/users/me/drafts/send'
var headers = {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
};
var params = {
method: "post",
contentType: "application/json",
headers: headers,
muteHttpExceptions: true,
payload: JSON.stringify(draftMsg)
};
var check = UrlFetchApp.getRequest(url, params)
var response = UrlFetchApp.fetch(url, params);

var result = response.getResponseCode();
if (result == '200') { // OK
return JSON.parse(response.getContentText());
}
else {
// This is only needed when muteHttpExceptions == true
var err = JSON.parse(response.getContentText());
throw new Error( 'Error (' + result + ") " + err.error.message );
}
}


/**
* Gets the current user's draft messages.
* Throws if unsuccessful.
* See https://developers.google.com/gmail/api/v1/reference/users/drafts/list.
*
* @returns {Object[]} If successful, returns an array of
* Users.drafts resources.
*/
function getDrafts() {
var url = 'https://www.googleapis.com/gmail/v1/users/me/drafts';
var headers = {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
};
var params = {
headers: headers,
muteHttpExceptions: true
};
var check = UrlFetchApp.getRequest(url, params)
var response = UrlFetchApp.fetch(url, params);

var result = response.getResponseCode();
if (result == '200') { // OK
return JSON.parse(response.getContentText()).drafts;
}
else {
// This is only needed when muteHttpExceptions == true
var error = JSON.parse(response.getContentText());
throw new Error( 'Error (' + result + ") " + error.message );
}
}

/**
* Gets the draft message ID that corresponds to a given Gmail Message ID.
*
* @param {String} messageId Immutable Gmail Message ID to search for
*
* @returns {String} Immutable Gmail Draft ID, or null if not found
*/
function getDraftId( messageId ) {
if (messageId) {
var drafts = getDrafts();

for (var i=0; i<drafts.length; i++) {
if (drafts[i].message.id === messageId) {
return drafts[i].id;
}
}
}

// Didn't find the requested message
return null;
}


/**
* Gets the draft message content that corresponds to a given Gmail Message ID.
* Throws if unsuccessful.
* See https://developers.google.com/gmail/api/v1/reference/users/drafts/get.
*
* @param {String} messageId Immutable Gmail Message ID to search for
* @param {String} optFormat Optional format; "object" (default) or "json"
*
* @returns {Object or String} If successful, returns a Users.drafts resource.
*/
function getDraftMsg( messageId, optFormat ) {
var draftId = getDraftId( messageId );

var url = 'https://www.googleapis.com/gmail/v1/users/me/drafts'+"/"+draftId;
var headers = {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
};
var params = {
headers: headers,
muteHttpExceptions: true
};
var check = UrlFetchApp.getRequest(url, params)
var response = UrlFetchApp.fetch(url, params);

var result = response.getResponseCode();
if (result == '200') { // OK
if (optFormat && optFormat == "JSON") {
return response.getContentText();
}
else {
return JSON.parse(response.getContentText());
}
}
else {
// This is only needed when muteHttpExceptions == true
var error = JSON.parse(response.getContentText());
throw new Error( 'Error (' + result + ") " + error.message );
}
}

授权

要使用 Google 的 API,我们需要为当前用户提供一个 OAuth2 token ——就像我们对高级服务所做的一样。这是使用 ScriptApp.getOAuthToken() 完成的.

将代码复制到您自己的脚本后,打开资源 -> 高级 Google 服务,打开 Google Developers Console 的链接,并为您的项目启用 Gmail API。

只要脚本包含至少一个需要用户权限的 GMailApp 方法,就会为 OAuthToken 正确设置认证范围。在这个例子中,这是由 GmailApp.search() 处理的。在 sendDayOldDrafts() ;但是为了保险起见,您可以使用 API 直接在函数中包含一个不可访问的函数调用。

关于google-apps-script - 如何使用谷歌应用程序脚本发送电子邮件草稿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27206595/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com