我正在使用 Google 脚本发送一封电子邮件并查找对此的任何回复(应该只有一个回复,但这与这里并不真正相关)。理论上,我可以使用搜索、标签和ReplyTo:
选项中GmailApp.sendEmail
跟踪事情。然而,我遇到了一些重叠的问题/担忧,因为:
- 我每周都会发送相同的电子邮件,因此搜索很挑剔
- Scripts/Gmail 似乎更新得不够快,无法找到刚刚发送的电子邮件
我想使用 Gmail 为每封电子邮件提供的唯一 ID,但由于GmailApp.sendEmail
方法返回一个GmailApp
对象而不是GmailMessage
反对这似乎不可能。
那么,如何以编程方式跟踪我以编程方式发送的电子邮件?
下面是我正在使用的代码。愿意接受工作流程和方法的变化,但更愿意将其保留在 Google Apps 脚本中。
function trigger(minutes){
ScriptApp.newTrigger("checkForEmail")
.timeBased()
.after(100*60*minutes)
.create()
};
function sendEmail(){
//send the email
GmailApp.sendEmail("[email protected] /cdn-cgi/l/email-protection","Subject","Body",{replyTo: "[email protected] /cdn-cgi/l/email-protection"});
//get the Id of the email that was just sent
var emailId GmailApp.search("replyTo:[email protected] /cdn-cgi/l/email-protection",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("[email protected] /cdn-cgi/l/email-protection","Error",body);
ScriptApp.deleteTrigger(command);
};
};
尝试制作草稿,然后发送。
var message = GmailApp.createDraft("[email protected] /cdn-cgi/l/email-protection","Subject","Body",{replyTo: "[email protected] /cdn-cgi/l/email-protection"}).send();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)