google-apps-scriptgmailgoogle-sites

Send an email notification for new announcement on a Google Site


Is it possible to create an email notification for any new announcements made on a google site?

When I create a new announcement I wish to have it sent out to all the people that are signed up in a mailing list. If it is possible, what are the best options to use? Should I make it with a Google App Script or use another service?

Here is one I found online on Google App Script but it doesnt seem to work:

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

EDIT: I will check this question regularly to see the best answers and hopefully help anybody that needs an option such as this one in their site.


Solution

  • Here is the final code I obtained after multiple questions on this site. Here is the credit to all the users that have helped me perfect it: Ritz, Michelle, and James Donnellan. These are the questions they answered to help me: Post 1, Post 2, and Post 3. To accomplish this task you can use these codes that I have prefected from the first draft:

    This one requires you to write all the emails you wish to send the notification to in a line, seperated by a coma:

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

    This one requires you to set up a Contact Group in Gmail and add in all the emails you wish to notify:

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

    Hope this helps anybody wishing to do this!