发送有关 Google 站点上的新公告的电子邮件通知

Send an email notification for new announcement on a Google Site

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

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

这是我在 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());
}
} 

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

这是我在本站多次提问后得到的最终代码。感谢所有帮助我完善它的用户:Ritz, Michelle, and James Donnellan. These are the questions they answered to help me: , , and 。要完成此任务,您可以使用我从初稿中提取的这些代码:

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

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

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

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