📜 ⬆️ ⬇️

Notification of new emails in Gmail via SMS using Google Calendar + Google Apps Script

Alas, but this method will stop working from June 27, 2015, because Google disables the SMS alert function for the calendar.
support.google.com/calendar/answer/45351?hl=en

Important! SMS alerts will be disabled June 27
From June 27, 2015, you will no longer receive SMS alerts from Google Calendar. The SMS alert function was introduced before the widespread adoption of smartphones. However, modern mobile devices allow you to receive alerts in a more convenient format and even offline.
To view alerts, you can use the calendar application that was installed on your smartphone by default, or download Google Calendar for Android or iPhone. Learn more about alerts ...
Note. This change does not affect users of Google Apps for Work, Education and Government.




I want to share with Harbras-society, from my point of view, a useful live hacker: SMS notifications about a new mail in a Gmail box. It is implemented by a small script that lives and works in Google Drive (formerly Google Docs).

The principle of operation is as follows: the timer script checks the Inbox folder in the mailbox and if it detects new emails, it creates an event in the google calendar with the name as the email subject and includes an SMS notification for it. As a result, you receive an SMS message with the following content:
')
: @

Implementation details, as always, under the "cut".

So, what you need to get started: you must have a phone number tied to the Google Calendar, how to do it is written here - Mobile phone registration:
support.google.com/calendar/bin/answer.py?hl=en&hlrm=en&answer=45351

The next few steps need to be done in order for the script to correctly process the new mail.

Step 1 : Create a new shortcut in Gmail with a name, for example, SMSnotify (or whatever you like). How to do this is written here - Using shortcuts:
support.google.com/mail/bin/answer.py?hl=en&hlrm=en&answer=118708#0

Step 2 : Create a filter that will apply the SMSnotify shortcut to all incoming (To: <-> @ gmail.com ). How to do this is written here - Using filters:
support.google.com/mail/bin/answer.py?hl=en&hlrm=en&answer=6579#0
Thus, all new letters will be marked with this label.

Step 3 : Log in to Google Drive (or Docs, if you haven't already switched) and create a new spreadsheet (Spreadsheet). Open the created table and select Tools> Script Editor in the top menu.


Step 4 : Due to the fact that when installing notifications directly from the code, errors like this are often possible

Calendar: Mismatch: etags

(more info here: code.google.com/p/google-apps-script-issues/issues/detail?id=264 )

Need to create additional. calendar and set up SMS notifications: set as default for all new meetings like this:

Settings-> Calendars-> Notifications-> Default-> SMS-> %% min

In this example, the calendar name will be " email "

I was pleasantly surprised to learn that you can now create scripts as separate applications that are not built into the table from the Google Disk interface. So " Step 3 " can now be summarized as follows:


A new page will open with Google's “script-IDE”, and here the most interesting begins.

The full text of the code is under the spoiler.

Click this caption to show / hide the code.
 function my_notification() { var calendar = CalendarApp.getOwnedCalendarsByName('email')[0]; //    "email" var threads = GmailApp.getUserLabelByName('SMSnotify').getThreads(); //,        SMSnotify var now = new Date(); if(threads == 0) return; //  ,     for(i in threads) //   { calendar.createEvent(threads[i].getFirstMessageSubject(), new Date(now.getTime()+60000), new Date(now.getTime()+60000)); } GmailApp.getUserLabelByName('SMSnotify').removeFromThreads(threads); //  "SMSnotify"   } 


After you have entered the code, you need to create a trigger on which the script will run. To do this, click on the " Resources " menu, and in it on the " Triggers of the current script ."



We need a time-driven trigger with a repetition rate, for example, one hour.



In the process of changing the trigger trigger conditions or saving changes in the script itself, an authorization window will appear. Feel free to log in :)


After all the manipulations, as a test, we try to run the script for execution.



Sometimes you may receive messages about the failure of the script (even if the code is correct and everything worked before). These messages can be turned off (although I left): you need to click on “notifications” in the trigger editor window and this window will appear



Thank you for reading to the end, I hope I have not forgotten anything. I would be very happy if this is useful to someone.
Any comments, additions, criticism are welcome! I recently discovered this wonderful API and just started to get acquainted with it.

Related Links:
Standard Services Documentation (English only) - developers.google.com/apps-script/defaultservices
Various tutorials (English only) - developers.google.com/apps-script/articles

Acknowledgments: Thanks to Yan169 and OlegT users for their interest and advice!

Source: https://habr.com/ru/post/145447/


All Articles