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

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

问题描述

每当我通过Google日历用户界面添加访客时,都会弹出以下对话框:

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日历用户界面时,此功能可完美运行.问题是我正在尝试使用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.

我正在通过使用

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

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

我能找到的最接近的是,当您

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).

更新:

  • 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. 转到
  1. Go to this link.
  2. Hit the Try it now button up top.

这应该向您显示一个表格,其中填充了以下信息:

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

  • calendarId:primary
  • 发送更新: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日历的用户界面发送日历事件一样.

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日历的界面也是如此.它会询问您是否要将更新发送给用户,您选择是",但是由于该事件是过去的,因此它实际上不会发送电子邮件.
  1. You must not send an email to the same logged in Google account. E.g. either use a plus address, or send it to a different account.
  2. Ensure that sendUpdates is set to all.
  3. Ensure that the event is some time in the future. Google will never send email for invites sent to events in the past. This is true even if you manually use Google Calendar's interface. It'll ask you if you want to send updates to the user(s), you choose "yes", but because the event is in the past, it won't actually send email.


由于最初的问题已经说明了如何使用标准的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 日历API一样)或sendUpdates(就像我们使用 advanced一样) 日历API)使用CalendarApp时.我们唯一的方法是 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).不必与原始JSON和Advanced Calendar API交互,我们可以使用Google的高级API(又称为Calendar)周围的包装器.

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. 在您的脚本中,导航到资源> Cloud Platform项目...
  2. 执行下列操作之一:

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

  • 如果您看到This script has an Apps Script-managed Cloud Platform project.恭喜,您已完成此步骤
  • 如果看到This script is currently associated with project:,请执行以下操作:

  • 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. 单击This script is currently associated with project:

这将带您进入Google Cloud Platform上的当前项目.

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

  • 如果以上两项均失败,则可以尝试核路线并以任何方式重新创建电子表格/doc/脚本.
  • 接下来,为脚本启用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一起使用,因为如果不支持,则可能意味着需要手动发送电子邮件,并调查

    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天全站免登陆