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

时间:2017-08-10 02:22:23

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

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

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

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

编辑:我会定期查看此问题以查看最佳答案,并希望能够帮助任何需要此类网站选项的人。

2 个答案:

答案 0 :(得分: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());
}  

希望这有助于任何希望这样做的人!

答案 1 :(得分:1)

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