在 CalendarEvent 上调用 addGuest 时如何发送标准邀请电子邮件? [英] How do I send the standard invitation email when calling addGuest on a CalendarEvent?

查看:33
本文介绍了在 CalendarEvent 上调用 addGuest 时如何发送标准邀请电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我通过 Google 日历 UI 添加来宾时,都会弹出以下对话框:

Whenever I add a guest via the Google Calendar UI, the following dialog pops up:

如果我选择发送",它会向用户发送一封格式良好的电子邮件,并提供回复日历事件的选项:

If I choose "Send" it sends a nicely formatted email to the user with an option to respond to the calendar event too:

当您手动使用 Google 日历 UI 时,这可以完美运行.问题是我正在尝试使用 Google Apps 脚本自动将人员添加到活动中.

This works flawlessly when you're manually using the Google Calendar UI. Problem is I'm trying to use Google Apps Scripts to automate adding people to events.

我正在使用 addGuest():

event.addGuest("user@example.com");

但是,似乎没有发送电子邮件的选项.

However, there doesn't seem to be an option to send an email.

我能找到的最接近的是当你 以编程方式创建事件,可以设置sendInvites:

The closest I could find is that when you programmatically create an event, you can set sendInvites:

  var event = CalendarApp.getDefaultCalendar().createEvent(
    'Test',
    new Date('April 5, 2029 20:00:00 UTC'),
    new Date('April 5, 2029 21:00:00 UTC'),
    {sendInvites: true, guests:"your.email+1@example.com"});
  Logger.log(event.getId());

这会正确发送一封格式良好的电子邮件.

This sends a nicely formatted email correctly.

但是,sendInvites 选项仅在新创建事件时有效.如果以后我打电话:

However, the sendInvites option only works when the event is newly created. If at a later time I call:

  var event = CalendarApp.getEventById("insert_id_here");
  event.addGuest("your.email+2@example.com");

...它不会发送电子邮件更新(即使该事件是使用 sendInvites=true 创建的).这意味着 sendInvites 是暂时的,不会在事件中持续存在.

... it does not send an email update (even though the event was created with sendInvites=true). This means sendInvites is transient and doesn't persist on the event.

在事件上调用 addGuest() 时,有什么方法可以 sendInvites 吗?例如.event.addGuest("user@example.com", {sendInvite: true})?

Is there any way to sendInvites when calling addGuest() on an event? E.g. event.addGuest("user@example.com", {sendInvite: true})?

是否有其他解决方法可以生成与点击上方 UI 中的发送"按钮时发送的电子邮件相同的电子邮件?

Are there any other workarounds that will produce the same email that is sent when hitting that "Send" button in the UI above?

注意:我不想想使用 MailApp 发送电子邮件,因为它不会像 Google 自动发送的那样格式很好(例如带有嵌入式日历邀请、与会者、描述、回复链接,等等,等等.

Note: I do not want to use MailApp to send an email because it won't be nicely formatted like the one Google automatically sends out (e.g. with embedded calendar invite, attendees, description, response links, etc, etc, etc).

更新:

  • 我刚刚看到 getAllTagKeysAPI 并希望我可以使用它来获取 sendInvites 键,但事实并非如此.即使对于 sendInvites 设置为 true 的事件,它也是空的.
  • I just saw getAllTagKeys in the API and was hoping I could use that to get the sendInvites key, but that's not the case. It is empty even for events that have sendInvites set to true.

推荐答案

正如 tehhowch 提到的,关键是您必须为此使用高级 Google API.但是,此页面上的文字说:

As tehhowch mentioned, the key is you must use the Advanced Google API for this. However, the text on this page that says:

这会向与会者发送一封邀请电子邮件,并将活动放在他们的日历上.

This sends an invitation email to the attendees and places the event on their calendar.

... 非常具有误导性.它发送电子邮件的唯一方法是将请求的 sendUpdates 设置为 all.

... is very misleading. The only way it'll send email is if you set sendUpdates to all for the request.

首先,您可以验证 sendUpdates 是通过高级 API 为 new 事件发送电子邮件的关键部分:

First, you can verify that sendUpdates is the key piece to sending email for new events via the advanced API:

  1. 转到 此链接.
  2. 点击顶部的立即试用按钮.

这应该会显示一个包含以下信息的表单:

This should show you a form with the following info populated:

  • calendarId: primary
  • sendUpdates: all
  • 请求正文:

  • calendarId: primary
  • sendUpdates: all
  • Request body:

{
  "start": {
    "date": "2029-04-04"
  },
  "end": {
    "date": "2029-04-04"
  },
  "summary": "Test",
  "attendees": [
    {
      "email": "your.email+1@example.com"
    }
  ]
}

your.email+1@example.com 更改为您的电子邮件地址.

Change the your.email+1@example.com to your email address.

请注意,这不能是您登录时使用的确切电子邮件地址.否则脚本会注意到您正在尝试向自己发送电子邮件而不是发送.相反,如果您有 Google 电子邮件帐户,则应使用 加上地址.

Note that this cannot be your exact email address that you are logged in with. Otherwise the script will notice that you're trying to send an email to yourself and not send it. Instead, if you have a Google email account, you should use a plus address instead.

点击执行按钮.

授予它权限后,您将在收件箱中收到一封电子邮件,其中日历事件已完全填充,就像使用 Google 日历的 UI 时发送的一样.

After giving it permissions, you will receive an email to your inbox with the calendar event fully populated just as it is sent when using Google Calendar's UI.

如果它不起作用,请确保您遵循这些警告:

If it's not working, make sure that you follow these caveats:

  1. 您不得向同一登录的 Google 帐户发送电子邮件.例如.要么使用加号地址,要么将其发送到其他帐户.
  2. 确保将 sendUpdates 设置为 all.
  3. 确保事件发生在未来的某个时间.Google 绝不会为过去发送的活动邀请发送电子邮件.即使您手动使用 Google 日历的界面也是如此.它会询问您是否要向用户发送更新,您选择是",但由于该事件已发生,因此它实际上不会发送电子邮件.

<小时>

由于最初的问题已经展示了如何使用标准的 CalendarApp 来完成我们上面所做的事情,所以我们还没有完成多少工作.既然我们知道 sendUpdates 很重要,那么是时候考虑添加访客了.


Since the original question already shows how to use the standard CalendarApp to do what we did above, we didn't accomplish much, yet. Now that we know that sendUpdates is important, though, it's time to look at adding a guest.

正如最初的问题所指出的,无法指定 sendInvite(就像我们使用 standard Calendar API 那样)或 sendUpdates使用 CalendarApp 时(就像我们使用 advanced Calendar API 一样).我们唯一的方法是 addGuest().

As the original question points out, there is no way to specify sendInvite (as we would with the standard Calendar API) or sendUpdates (as we would with the advanced Calendar API) when using CalendarApp. The only method we have is addGuest().

幸运的是,我们可以使用与上面类似的高级日历 API,并向事件发送更新(又名 PATCH).我们可以使用 Google 对高级 API(又名 Calendar)的漂亮包装器,而不是与原始 JSON 和高级日历 API 交互.

Luckily, we can use the Advanced Calendar API similar to how we did above and send an update (aka PATCH) to the event. Rather than interfacing with raw JSON and the Advanced Calendar API, we can use Google's nice wrapper around the advanced API (aka Calendar).

但是,由于我们使用的是高级日历 API,因此我们必须遵守此注意事项:

However, since we're using the advanced Calendar API, we must abide by this note:

注意:这是一项高级服务,使用前必须启用.

Note: This is an advanced service that must be enabled before use.

这意味着在跳入代码之前有一些先决条件:

This means there are some prerequisites before jumping into code:

  1. 首先,在您的 Cloud Platform 项目中启用 Google Calendar API:

  1. First, enable Google Calendar API in your Cloud Platform project:

  1. 在您的脚本中,导航到资源 > 云平台项目...
  2. 执行以下操作之一:

  1. In your script, navigate to Resources > Cloud Platform project...
  2. Do one of the following:

  • 如果您看到 此脚本有一个由 Apps 脚本管理的云平台项目.恭喜,您重新完成这一步
  • 如果您看到 此脚本当前与项目关联:,请执行以下操作:

  • If you see This script has an Apps Script-managed Cloud Platform project., congratulations, you're done with this step!
  • If you see This script is currently associated with project:, do the following:

  1. 点击此脚本当前与项目关联的链接:

这会将您带到 Google Cloud Platform 上的当前项目.

This will take you to the current project on Google Cloud Platform.

  • 如果以上两种方法都失败,您可以尝试核路线并重新创建电子表格/文档/脚本.
  • 接下来,为脚本启用 Google Calendar API:

  • Next, enable Google Calendar API for the script:

    1. 回到您的脚本中,资源 > 高级 Google 服务
    2. 点击 Google Calendar API 旁边的开".

    1. Back in your script, Resources > Advanced Google services
    2. Hit "On" next to Google Calendar API.

    您现在可以在脚本中使用高级 API.

    You're now ready to use the advanced API in a script.

  • 有了这些先决条件,您现在可以创建一个函数:

    With those prerequisites out of the way, you can now create a function:

    // Note: requires advanced API
    function addGuestAndSendEmail(calendarId, eventId, newGuest) {
      var event = Calendar.Events.get(calendarId, eventId);
      var attendees = event.attendees;
      attendees.push({email: newGuest});
    
      var resource = { attendees: attendees };
      var args = { sendUpdates: "all" };
    
      Calendar.Events.patch(resource, calendarId, eventId, args);
    }
    

    一些注意事项:

    1. 传递 eventId 时,您需要删除 eventId 末尾的 @google.com.
    2. 您必须先调用 get 以获取当前的受邀者列表.如果不这样做,您将不小心删除原来参加活动的所有人员.有关详细信息,请参阅此答案.

    1. When you pass an eventId, you will need to remove the @google.com at the end of the eventId.
    2. You must call get first to get the current list of invitees. If you don't, you'll accidentally remove all the people that were on the event originally. See this answer for more info.

    注意:patch 很聪明,只会向实际添加的人发送电子邮件更新.例如.如果当前参加活动的人是 [alice, bob],并且您发送带有 [alice, bob, charlie] 的补丁,则只有 charlie 将收到一封电子邮件.这正是您想要的!

    Note: patch is smart and will only send email updates to the people that were actually added. E.g. if the current people on an event are [alice, bob], and you send a patch with [alice, bob, charlie], only charlie will receive an email. This is exactly what you want!

    同样,关键是知道如何将 sendUpdates 传入 insert() 方法.我找不到有关补丁方法1 的任何文档,但是自动完成显示可以添加一个optionalArgs 参数.我随机尝试了你在上面看到的格式,它奏效了!

    Again, the key is knowing how to pass in sendUpdates to the insert() method. I couldn't find any documentation about the patch method1, but the auto-complete revealed that there is an optionalArgs parameter that can be added. I randomly tried the format you see above, and it worked!

    如果它不起作用,请务必阅读以上所有注意事项.

    If it's not working, make sure you read all caveats above.

    注意:我很高兴这适用于 Google 的 API,因为如果没有,则可能意味着需要手动发送电子邮件,并查看 如何将日历元数据添加到邮件中,以便 Gmail 正确解析它.

    Note: I'm glad this worked with Google's API, because if it didn't, it likely meant needing to manually send the email(s), and look into how to add calendar metadata to a message so that Gmail parses it properly.

    1我们有通用补丁API,但这仅在发送原始 JSON 时有用.我们正在使用 Google 的 Calendar 包装器,它不会不完全相同.

    1 We have the general patch API, but this is only useful when sending the raw JSON. We're using Google's Calendar wrapper, which doesn't map exactly the same.

    这篇关于在 CalendarEvent 上调用 addGuest 时如何发送标准邀请电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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