我正在为我的 Google 网站编写电子邮件通知脚本,我已经对其进行了测试,它工作正常,代码如下:
var url_of_announcements_page = "https://sites.google.com/a/announcements";
var who_to_email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9afff7fbf3f6daf9f5f7eafbf4e3b4f9f5f7" rel="noreferrer noopener nofollow">[email protected]</a>";
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 联系人组,以便更快地进行维护。
我尝试用我在 Gmail 上创建的联系人群组替换电子邮件地址。我把它称为Test
,当我替换<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cda8a0aca4a18daea2a0bdaca3b4e3aea2a0" rel="noreferrer noopener nofollow">[email protected]</a>
时根据联系人组的名称,我遇到了以下错误:
我在网上查了一下,找不到对我的情况有具体帮助的东西。所以我有两个问题:
- 是否可以更改代码以使 Gmail 联系人组正常工作?
- 如果没有,除了 Gmail 联系人群组之外,我还可以使用其他工具吗?
最佳答案
您需要首先将电子邮件地址获取到数组中。使用联系人应用程序获取测试组,然后循环遍历所有联系人以获取他们的地址。
var emails = [];
var contacts = ContactsApp.getContactGroup('Test').getContacts();
for(var i in contacts){
emails.push(contacts[i].getPrimaryEmail());
}
然后按照 Cooper 的建议,在发送至或密件抄送参数中使用 emails
变量。
关于google-apps-script - 使用 Google App 脚本向群组而不是单个地址发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45538689/