使用 Google App Script 向群组而非个人地址发送电子邮件 [英] Email a group and not individual addresses with Google App Script

查看:47
本文介绍了使用 Google App Script 向群组而非个人地址发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的 Google 站点编写电子邮件通知脚本,我已经对其进行了测试,它运行良好,代码如下:

I am working on an email notification script for my Google Site, I have tested it and it works fine, here is the code:

var url_of_announcements_page = "https://sites.google.com/a/announcements"; 
var who_to_email = "email@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 > 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()+"

"+ann.getUrl(), options);

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

function setup(){
 PropertiesService.getScriptProperties().setProperty('last-update',new Date().getTime());
}  

唯一的问题是我希望有多个人收到这封电子邮件.我知道可以在每个电子邮件地址后使用逗号逐个添加它们.但是,这需要大量维护,我想将其切换到 Gmail 联系人组以加快维护速度.

The only issue is that I wish to have multiple people receive this email. I know it is possible to add them one by one using a coma after each email address. However, this is a lot of maintenance and I wanted to switch it to a Gmail Contact Group to make maintenance quicker.

我尝试用我在 Gmail 上创建的联系人组替换电子邮件地址.我将其称为 Test,当我将 email@company.com 替换为联系人组的名称时,我出现了以下错误:

I tried replacing the email address by a Contact Group that I created on Gmail. I had called it Test, and when I replaced email@company.com by the name of the contact group I came out with this error:

我在网上查看过,但找不到对我的具体情况有帮助的东西.所以我有两个问题:

I have looked online and I can't find something that helps me specifically with my situation. So I have two questions:

  1. 有没有办法更改代码以使 Gmail 联系人组正常工作?
  2. 如果没有,除了 Gmail 联系人组之外,我还有什么可以用于此目的的吗?

推荐答案

您需要先将电子邮件地址获取到一个数组中.使用联系人应用获取测试组,然后遍历所有联系人以获取他们的地址.

You need to get the email address to an array first. Use the contacts app to get the Test group and then loop through all the contacts to get their addresses.

var emails = []; 
var contacts = ContactsApp.getContactGroup('Test').getContacts();
 for(var i in contacts){
    emails.push(contacts[i].getPrimaryEmail());
   }

然后按照 Cooper 的建议在 send to 或 bcc 参数中使用 emails 变量.

Then use the emails variable in the send to or bcc parameter as Cooper suggested.

这篇关于使用 Google App Script 向群组而非个人地址发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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