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.
... is very misleading. The only way it'll send email is if you set sendUpdates
to all
for the request.
First, you can verify that sendUpdates
is the key piece to sending email for new events via the advanced API:
- Go to this link.
Hit the Try it now button up top.
This should show you a form with the following info populated:
- 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"
}
]
}
Change the your.email+1@example.com
to your email address.
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.
Hit the Execute button.
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 you want to definitively prove that
sendUpdates
is important, set it to blank instead of all
. Send the invite. Notice that it didn't arrive in your inbox.
If it's not working, make sure that you follow these caveats:
- 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.
- Ensure that
sendUpdates
is set to all
.
- 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.
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.
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().
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
).
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:
First, enable Google Calendar API in your Cloud Platform project:
- In your script, navigate to Resources > Cloud Platform project...
Do one of the following:
Next, enable Google Calendar API for the script:
- Back in your script, Resources > Advanced Google services
Hit "On" next to Google Calendar 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);
}
Some caveats:
- When you pass an eventId, you will need to remove the
@google.com
at the end of the eventId.
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.
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!
- All the previous caveats in the list above apply.
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.
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 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.