SMS notification for Gmail using Google Apps Scrip

2019-02-13 14:34发布

问题:

I am working on SMS notification each time I receive an email that meets certain criteria; I decided to use Google App Scripts for this.

I have been inspired in particular by the following article https://developers.google.com/apps-script/articles/gmail_filter_sms. I also checked the related question in StackOverflow SMS Alerts for Important Mails in Gmail.

I improved the original script from developers.google.com by cleaning up the events the next time the script is run (I was receiving the SMS alerts each time the script is run). The script is currently working by using the label 'SendText' and creating events in calendar 'AlertSMS'.

However the SMS I receive only contain the subject and author of the email: I need to display the content of the email (or at least a part of it). I tried with no luck to add it to the description of the event. Anybody got an idea on how to do it?

Hereunder, the code of my script:

function sendText() {
  var now = new Date().getTime();

  // Delete old events
  var events = CalendarApp.openByName('AlertSMS').getEvents(new Date('January 1, 2010 EST'), new Date(now-30000));
  for (i in events) {
    events[i].deleteEvent();
  }  

  // Get list of emails to set alert for
  var label = GmailApp.getUserLabelByName('SendText');
  var threads = label.getThreads();

  // Create new events for emails alert
  for(i in threads){
    var message=threads[i].getMessages()[0];
    CalendarApp.openByName('AlertSMS').createEvent('[SMS] '+threads[i].getFirstMessageSubject()+' -from- '+message.getFrom(),
      new Date(now+60000), new Date(now+60000), { description:message.getBody() }).addSmsReminder(0);
  }
  label.removeFromThreads(threads);
}

回答1:

You can use services such as (the completely free) IFTTT or (the somewhat free) Zapier to trigger an SMS action upon receiving emails matching a criteria.

Here are some IFTTT "recipes" which connect GMail to SMS.



回答2:

You don't really need the Calendar hack to send arbitrary text messages to yourself - every mobile provider has an email to text gateway. For verizon, for example, its 5551234567@vtext.com (substitute your phone number) and there exists similar for other carriers. Just use GmailApp to email those numbers and you can send whatever you want.