【问题标题】:Send an email notification for new announcement on a Google Site发送电子邮件通知以了解 Google 网站上的新公告
【发布时间】:2017-08-10 02:22:23
【问题描述】:

是否可以为谷歌网站上发布的任何新公告创建电子邮件通知?

当我创建一个新公告时,我希望将它发送给所有在邮件列表中注册的人。如果可能的话,最好的选择是什么?我应该使用 Google App Script 还是使用其他服务?

这是我在 Google App Script 上在线找到的一个,但它似乎不起作用:

function myFunction() {

var url_of_announcements_page = "https://sites.google.com/announcements-page-link"; 
var who_to_email = "name@company.com" 

function emailAnnouncements(){
  var page = SitesApp.getPageByUrl(url_of_announcements_page); 
  if(page.getPageType() == SitesApp.PageType.ANNOUNCEMENTS_PAGE){ 

    var announcements = page.getAnnouncements({ start: 0,
                                               max: 10,
                                               includeDrafts: false,
                                               includeDeleted: false});
    announcements.reverse();                                    
    for(var i in announcements) { 
      var ann = announcements[i]; 
      var updated = ann.getLastUpdated().getTime(); 
      if (updated > ScriptProperties.getProperty('last-update')){ 
        var options = {}; 

        options.htmlBody = Utilities.formatString("<h1><a href='%s'>%s</a></h1>%s", ann.getUrl(), ann.getTitle(), ann.getHtmlContent());

        MailApp.sendEmail(who_to_email, "Announcement "+ann.getTitle(), ann.getTextContent()+"\n\n"+ann.getUrl(), options);

        ScriptProperties.setProperty('last-update',updated);
      }
    }
  }
}

function setup(){
  ScriptProperties.setProperty('last-update',new Date().getTime());
}
} 

编辑:我会定期检查这个问题以查看最佳答案,并希望能帮助任何需要在他们的网站上提供此类选项的人。

【问题讨论】:

    标签: google-apps-script gmail google-sites


    【解决方案1】:

    这是我在本网站上提出多个问题后获得的最终代码。这是所有帮助我完善它的用户的功劳:RitzMichelleJames Donnellan。这些是他们为帮助我而回答的问题:Post 1Post 2Post 3。要完成这项任务,您可以使用我在初稿中完善的这些代码:

    这要求您将所有希望发送通知的电子邮件写成一行,用逗号分隔:

    var url_of_announcements_page = "https://sites.google.com/a/announcements"; 
    var who_to_email = ("email@gmail.com");
    
    function emailAnnouncements(){
      var page = SitesApp.getPageByUrl(url_of_announcements_page); 
      if(page.getPageType() == SitesApp.PageType.ANNOUNCEMENTS_PAGE){ 
    
        var announcements = page.getAnnouncements({ start: 0,
                                                   max: 10,
                                                   includeDrafts: false,
                                                   includeDeleted: false});
        announcements.reverse();                                    
        for(var i in announcements) { 
          var ann = announcements[i]; 
          var updated = ann.getLastUpdated().getTime(); 
          if (updated > PropertiesService.getScriptProperties().getProperty("last-update")){ 
            var options = {}; 
    
            options.htmlBody = Utilities.formatString("<h1><a href='%s'>%s</a></h1>%s", ann.getUrl(), ann.getTitle(), ann.getHtmlContent());
    
            MailApp.sendEmail(who_to_email, "Notification - '"+ann.getTitle()+"'", ann.getTextContent()+"\n\n"+ann.getUrl(), options);
    
            PropertiesService.getScriptProperties().setProperty('last-update',updated);
          }
        }
      }
    }
    
    function setup(){
     PropertiesService.getScriptProperties().setProperty('last-update',new Date().getTime());
    }  
    

    这需要您在 Gmail 中设置一个联系人组并添加您希望通知的所有电子邮件:

    var url_of_announcements_page = "https://sites.google.com/a/announcements"; 
    var who_to_email = [];
    var contacts = ContactsApp.getContactGroup('Contact Group').getContacts();
     for(var i in contacts){
        who_to_email.push(contacts[i].getPrimaryEmail());
       }
    
    function emailAnnouncements(){
      var page = SitesApp.getPageByUrl(url_of_announcements_page); 
      if(page.getPageType() == SitesApp.PageType.ANNOUNCEMENTS_PAGE){ 
    
        var announcements = page.getAnnouncements({ start: 0,
                                                   max: 10,
                                                   includeDrafts: false,
                                                   includeDeleted: false});
        announcements.reverse();                                    
        for(var i in announcements) { 
          var ann = announcements[i]; 
          var updated = ann.getLastUpdated().getTime(); 
          if (updated > PropertiesService.getScriptProperties().getProperty("last-update")){ 
            var options = {}; 
    
            options.htmlBody = Utilities.formatString("<h1><a href='%s'>%s</a></h1>%s", ann.getUrl(), ann.getTitle(), ann.getHtmlContent());
    
            MailApp.sendEmail(who_to_email, "Notification - '"+ann.getTitle()+"'", ann.getTextContent()+"\n\n"+ann.getUrl(), options);
    
            PropertiesService.getScriptProperties().setProperty('last-update',updated);
          }
        }
      }
    }
    
    function setup(){
     PropertiesService.getScriptProperties().setProperty('last-update',new Date().getTime());
    }  
    

    希望这对希望这样做的人有所帮助!

    【讨论】:

      【解决方案2】:

      Romain Vialard 有一个包含相关代码的电子表格,我用它来允许个人Subscribe to Changes for Site Viewers 发送通知。如果需要,该代码可供您查看和编辑。它有几个发送内容的选项。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-09
        • 2013-04-13
        • 2011-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多