【问题标题】:Gmail auto reply script - how to set conditional time and stop multiple replies?Gmail 自动回复脚本 - 如何设置条件时间并停止多个回复?
【发布时间】:2020-01-12 05:10:24
【问题描述】:

我已经调整了 Gmail 自动回复脚本,以便在我的非工作日向向我发送消息的人发送电子邮件。我希望脚本在星期五的 16:30 开始发送回复,但我不确定我的代码是否正确。

在上周五测试时,我的脚本似乎没有发送任何回复,但在周六、周日和周一运行良好。

function autoReply() {
var interval = 5;
  var date = new Date();
  var day = date.getDay();
  var hour = date.getHours();
  var minute = date.getMinutes();
  if ([1,6,0].indexOf(day) > -1 || (day == 1 && hour < 8) || (day == 5 && hour == 16 && minute >= 30)) {
    var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
    var threads = GmailApp.search('is:inbox after:' + timeFrom);
    for (var i = 0; i < threads.length; i++) {
      if (threads[i].isUnread()){
        threads[i].reply("Hi," + "\n\n Thanks for your email. My working days are Tuesday to Friday, so I won't pick up your message until Tuesday morning.");
      threads[i].markRead();
      threads[i].star();
      }
    }
  }
}

此外,有什么方法可以调整我的脚本,使其不会将自动回复邮件发送给已经收到它的发件人(与 Gmail 的标准自动回复功能一样)。我已经使用诸如“条件回复”之类的术语在 Stack Overflow 上搜索了此解决方案,但无济于事。 (感谢我可能完全使用了错误的术语!)

任何帮助将不胜感激!

【问题讨论】:

    标签: google-apps-script gmail


    【解决方案1】:

    您需要通过条件 (day == 5 &amp;&amp; hour &gt;= 17) 来补充您的 if 语句

    要验证电子邮件是否已发送给某些发件人,您可以使用PropertiesService。这允许您将发件人保存在缓存中,并比较每封新电子邮件的发件人是否已包含在属性中:

    function autoReply() {
    var interval = 5;
      var date = new Date();
      var day = date.getDay();
      var hour = date.getHours();
      var minute = date.getMinutes();
      if ([1,6,0].indexOf(day) > -1 || (day == 1 && hour < 8) || (day == 5 && hour == 16 && minute >= 30)|| (day == 5 && hour >= 17)){
        var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
        var threads = GmailApp.search('is:inbox after:' + timeFrom);
        for (var i = 0; i < threads.length; i++) {
          if (threads[i].isUnread()){
             var sender=threads[i].getMessages()[0].getFrom();
            if(PropertiesService.getScriptProperties().getKeys().length==0){
               PropertiesService.getScriptProperties().setProperty('from', '');
             }
            var scriptProperties = PropertiesService.getScriptProperties().getProperty('fromArray');
            if(scriptProperties.indexOf(sender)==-1){
              threads[i].reply("Hi," + "\n\n Thanks for your email. My working days are Tuesday to Friday, so I won't pick up your message until Tuesday morning.")
              threads[i].markRead();
              threads[i].star();
              scriptProperties=scriptProperties+sender; 
              PropertiesService.getScriptProperties().setProperty('from',  scriptProperties);
              }
          }
        }
      }
    }
    

    【讨论】:

    • 非常感谢您的回答——这真的很有帮助!虽然我不得不承认我对 PropertiesService 有点困惑。我必须在脚本中指定发件人吗?
    • 您可以将脚本属性想象为脚本完成后继续存在的变量,因此可以在下一次脚本运行期间检索其内容。该脚本为它正在循环的每个未读线程检索var sender=threads[i].getMessages()[0].getFrom();,并使用 indexOf() 检查此发送者是否已包含在脚本属性中。如果是 - 什么都不会发生,如果不是 - 一封电子邮件被发送给这个发件人,他的电子邮件被添加到脚本属性中。
    猜你喜欢
    • 2021-09-28
    • 2016-12-21
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多