gpt4 book ai didi

email - 在 Google Apps 脚本中访问已发送的电子邮件

转载 作者:行者123 更新时间:2023-12-02 00:05:48 26 4
gpt4 key购买 nike

我正在使用 Google Script 发送电子邮件并查找对它的任何回复(应该只有一个回复,但这在这里并不真正相关)。理论上,我可以使用 GmailApp.sendEmail 中的搜索、标签和 ReplyTo: 选项来跟踪事情。但是,我遇到了一些重叠的问题/疑虑,因为:

  • 我每周都发送相同的电子邮件,所以搜索很挑剔
  • Scripts/Gmail 的更新速度似乎不够快,无法找到它刚刚发送的电子邮件

我想使用 Gmail 为每封电子邮件提供的唯一 ID,但由于 GmailApp.sendEmail 方法返回的是 GmailApp 对象而不是 GmailMessage object 这似乎不可能。

那么,如何以编程方式跟踪我以编程方式发送的电子邮件?

下面是我使用的代码。对工作流程和方法的变化持开放态度,但更愿意将其保留在 Google Apps 脚本中。

function trigger(minutes){
ScriptApp.newTrigger("checkForEmail")
.timeBased()
.after(100*60*minutes)
.create()
};

function sendEmail(){
//send the email
GmailApp.sendEmail("name@gmail.com","Subject","Body",{replyTo: "myname+modifier@gmail.com"});
//get the Id of the email that was just sent
var emailId GmailApp.search("replyTo:name+modifier@gmail.com",0,1)[0].getMessages()[0];
ScriptProperties.setProperty("emailId", emailId);
//set a trigger to check later
trigger(45)
};

function checkForEmail(){
var emailId = ScriptProperties.getProperty("emailId");
var email = GmailApp.getMessageById(emailId);
var count = email.getThread().getMessageCount();
var command = "checkForEmail"
if (count == 1){
//set trigger to check again
ScriptApp.deleteTrigger(command)
trigger(5)
}
if (count == 2){
//do stuff with the new email: alert me, download attachments, etc.
var attachments = email.getThread().getAttachments()
ScriptApp.deleteTrigger(command);
}
else {
//something is weird, let me know
var body = "there was an error with checking an email ("+emailId+")."
GmailApp.sendEmail("myname@gmail.com","Error",body);
ScriptApp.deleteTrigger(command);
};
};

最佳答案

尝试制作草稿,然后发送。

var message = GmailApp.createDraft("name@gmail.com","Subject","Body",{replyTo: "myname+modifier@gmail.com"}).send();

关于email - 在 Google Apps 脚本中访问已发送的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18470622/

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