Skip to content

Instantly share code, notes, and snippets.

@kshwetabh
Created February 11, 2013 02:09
Show Gist options
  • Save kshwetabh/4751989 to your computer and use it in GitHub Desktop.
Save kshwetabh/4751989 to your computer and use it in GitHub Desktop.
Reminder script for GMail: A simple Google Apps Script to create a Google Calendar event (reminder) for any mail (with a specific label). You can then setup ("timed") triggers in Apps Script... (public version of the GMailReminder gist)
/**
* Reminder script for GMail: A simple Google Apps Script to create a Google Calendar event (reminder) for any mail (with a
* specific label). You can then setup ("timed") triggers in Apps Script (hourly, etc) to monitor your Inbox.
* How to Use:
* 1. Log into Google Drive account and create a Google Script.
* 2. Copy and paste the below snippet into the gs file.
* 3. Make sure to update the 'reminderLabel', 'calendarName' and 'reminderDuration' as per your preference.
* 4. Test the script to make sure it is working properly.
* 5. Setup a time-driven Project Trigger (hourly, etc) to automatically run this script and create calendar events.
*
* Inspired by Gmail Snooze script by Corey Goldfeder
* http://gmailblog.blogspot.com/2011/07/gmail-snooze-with-apps-script.html
*
**/
function gMailReminder() {
var reminderLabel = "GReminder", //Substitute your label here
calendarName = "Mobile Calendar", ////Substitute your Calendar name here
reminderDuration = 2, //duration in hours
label = GmailApp.getUserLabelByName(reminderLabel),
threads = label.getThreads();
if (threads.length > 0) {
//get calendar by name
var cals = CalendarApp.getCalendarsByName(calendarName);
var now = new Date().getTime();
for (i in threads) {
cals[0].createEvent(reminderLabel + '- '+threads[0].getFirstMessageSubject(),
new Date(now+(60000*60*reminderDuration)),
new Date(now+(60000*60*reminderDuration)), {description: threads[i].getPermalink()});
}
//Remove the label from the mails to avoid duplicate event creation on next run
label.removeFromThreads(threads);
}
}
@chaoscreater
Copy link

chaoscreater commented Jul 30, 2021

Looks like this doesn't work, I'm getting the following error at line 6:

TypeError: Cannot read property 'getThreads' of null

threads = label.getThreads();

Looks like if you use labels with non-english characters, then the above won't work. See updated solution here:
https://stackoverflow.com/questions/68584353/create-calendar-event-from-gmail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment