从Google协作平台发送电子邮件通知 [英] Sending an Email Notification from Google Sites

查看:94
本文介绍了从Google协作平台发送电子邮件通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Google网站上创建新公告时,我一直在尝试设置电子邮件通知。我使用了我在网上找到的基本代码,但它不适合我。这里是:

  function myFunction(){

var url_of_announcements_page =https:// sites .google.com /公告页面链接;
VAR who_to_email = name@company.com

功能emailAnnouncements(){
变种页= 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(更新> 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()+\\\
\\\
+ ann.getUrl(),options);

ScriptProperties.setProperty('last-update',updated);




$ b函数setup(){
ScriptProperties.setProperty('last-update',new Date( ).getTime());
}
}

代码似乎在没有出现任何错误消息的情况下运行。但是,我没有收到我在代码中编写的帐户上的电子邮件。我已给予完全许可,以便脚本可以从我的帐户发送电子邮件。它似乎并没有完成所需的任务。

我用来编写公告的google站点仍然是私人的,只有我能看到它,这是否在这个代码中的作用不起作用?



如果你看到任何错误或者有什么想法是什么问题,我会很高兴知道。 b

解决方案

您已经在 myFunction 下编写了这两个函数。你需要单独编写它。另外 ScriptProperties API已弃用,请使用 PropertiesService 。请参阅下面的代码。希望这有助于您!

  var url_of_announcements_page =https://sites.google.com/announcements-page-link; 
var who_to_email = Session.getActiveUser()。getEmail();

函数emailAnnouncements(){
var page = SitesApp.getPageByUrl(url_of_announcements_page);
如果(page.getPageType()== SitesApp.PageType.ANNOUNCEMENTS_PAGE){

变种公告= page.getAnnouncements({开始:0,
最大: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,Announcement+ ann.getTitle(),ann.getTextContent()+\\\
\\\
+ ann.getUrl(),options);

PropertiesService.getScriptProperties()。setProperty('last-update',updated);




$ b函数设置(){
PropertiesService.getScriptProperties()。setProperty('last-update' ,new Date()。getTime());
}


I've been trying to set up an email notification for when I create a new announcement in my google site. I used a base code I found online but it isn't working for me. Here it is:

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

The code seems to run with out any error message appearing. However, I do not receive the emails on the account I write in the code. I have given full permission so that the script can send the email from my account. It just doesn't seem to fulfil the task needed.

The google site I am using to write the announcements is still private and only I can see it, does that play a role in this code not working?

If you see any mistakes or have any idea what the issue is I would be happy to know.

解决方案

You have written both the functions under myFunction. You need to write it separately. Also ScriptProperties API is deprecated, use PropertiesService. Refer the below code. Hope this helps!

var url_of_announcements_page = "https://sites.google.com/announcements-page-link"; 
var who_to_email = Session.getActiveUser().getEmail();

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, "Announcement "+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());
}

这篇关于从Google协作平台发送电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆