在Google Apps脚本中访问发送的电子邮件 [英] Accessing sent email in Google Apps Script

查看:161
本文介绍了在Google Apps脚本中访问发送的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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




  • 我每个星期发送相同的电子邮件,所以搜索是精彩的

  • 脚本/ Gmail似乎没有快速更新,无法找到刚刚发送的电子邮件。



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



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



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

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

函数sendEmail(){
//发送电子邮件
GmailApp.sendEmail(name@gmail.com,主题,正文,{replyTo: myname+modifier@gmail.com});
//获取刚刚发送的电子邮件的ID
var emailId GmailApp.search(replyTo:name+modifier@gmail.com,0,1)[0] .getMessages()[ 0];
ScriptProperties.setProperty(emailId,emailId);
//设置触发器以检查
触发器(45)
};

函数checkForEmail(){
var emailId = ScriptProperties.getProperty(emailId);
var email = GmailApp.getMessageById(emailId);
var count = email.getThread()。getMessageCount();
var command =checkForEmail
if(count == 1){
//设置触发器再次检查
ScriptApp.deleteTrigger(command)
trigger(5 )
}
if(count == 2){
//使用新电子邮件:提醒我,下载附件等
var attachments = email.getThread ).getAttachments()
ScriptApp.deleteTrigger(command);
}
else {
//有些奇怪,让我知道
var body =检查电子邮件时出现错误(+ emailId +)。
GmailApp.sendEmail(myname@gmail.com,错误,正文);
ScriptApp.deleteTrigger(command);
};
};


解决方案

对于Gmail的搜索问题, Gmail搜索运营商,运营商之后: 之前:可以帮助你。



要获取发送的电子邮件的ID,我不知道如何很容易得到它一个想到的概念证明,你可以调整和测试,就像:

  ... 
GmailApp.sendEmail(name@gmail.com,主题,正文,{replyTo:myname+modifier@gmail.com});
do {
/ *搜索应该与您一样准确* /
threads = GmailApp.search('replyTo:name+modifier@gmail.com是:发送之前:2013 / 08/27 after:2013/08/27 subject:Subject',0,1);
} while(!threads.length);
...

除了进行所有必要的验证(例如设置超时以避免无限循环),将不得不检查这不会给出问题,例如 c code code code code code code code code code code code code code code code $找到发送的邮件的ID。这些只是一些想法。


I am using a Google Script to send an email and look for any responses to it (there should only be one response, but that is not really relevant here). In theory, I can use search, label, and the ReplyTo: option in GmailApp.sendEmail to keep track of things. However, I am running into a couple of overlapping issues/concerns because:

  • I am sending the same email every week, so search is finicky
  • Scripts/Gmail doesn't seem to update quickly enough to find the email it just sent

I want to use the unique Id that Gmail gives every email, but since the GmailApp.sendEmail method returns a GmailApp object rather than a GmailMessage object this doesn't seem possible.

So, how do I programmatically track an email that I have programattically sent?

Below is the code I am using. Open to changes in workflow and methodology, but would prefer to keep this in Google Apps Script.

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);
};
};

解决方案

For the issue of search Gmail, have available the following Gmail search operators, operators after: before: can help you.

To get the ID of the email sent, I do not know how to get it easily. A proof of concept that comes to mind and that you can adapt and test, is something like:

   ...
   GmailApp.sendEmail("name@gmail.com","Subject","Body",{replyTo: "myname+modifier@gmail.com"});
   do {
     /* The search should be as accurate as you can */
     threads = GmailApp.search('replyTo:name+modifier@gmail.com is:sent before:2013/08/27 after:2013/08/27 subject:"Subject"', 0, 1);
   } while(!threads.length);
   ...

Besides making all necessary validations (e.g. set a timeout to avoid an infinite loop), will have to check that this does not give problems, e.g. Script invoked too many times for this user per second or the like.

Another option may be to set another trigger to find the ID of the mail sent. These are just some ideas.

这篇关于在Google Apps脚本中访问发送的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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