2017-08-10 82 views
0

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

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

这是一个我在网上发现了谷歌应用程序脚本,但它似乎没有工作:

function myFunction() { 

var url_of_announcements_page = "https://sites.google.com/announcements-page-link"; 
var who_to_email = "[email protected]" 

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

编辑:我会定期检查这个问题,看到的最好的答案,希望能帮助别人这需要在他们的网站上有这样一个选项。

回答

1

这是我在本网站的多个问题后获得的最终代码。以下是所有帮助我完善它的用户的功劳:RitzMichelleJames Donnellan。这些是他们回答帮助我的问题:Post 1Post 2Post 3。为了完成这个任务,你可以用,我已经从最初的草稿prefected这些代码:

这一个要求你写你希望将通知发送到在一条线上的电子邮件,由一个昏迷分隔:

var url_of_announcements_page = "https://sites.google.com/a/announcements"; 
var who_to_email = ("[email protected]"); 

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

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

相关问题