首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Google网站上发送新公告的电子邮件通知

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

Stack Overflow用户
提问于 2017-08-10 02:22:23
回答 1查看 1.8K关注 0票数 0

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

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

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

代码语言:javascript
复制
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());
}
} 

编辑:,我会定期检查这个问题,找出最好的答案,希望能帮助那些需要这个选项的人,比如在他们的网站上。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-10 02:22:23

这是我在这个网站上问了多个问题后得到的最终代码。以下是帮助我完善它的所有用户的功劳:RitzMichelleJames Donnellan。这些是他们回答的帮助我的问题:Post 1Post 2Post 3。要完成这项任务,您可以使用我从初稿中预先编写的以下代码:

这一条要求你把你想要发送通知的所有电子邮件写成一行,用昏迷隔开:

代码语言:javascript
复制
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中设立一个联络小组,并将您希望通知的所有电子邮件添加到:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45603434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档