Friday, 27 June 2014

Gmail Scheduled Send Email

Someone recently asked for the ability to schedule send an email. It was for a link to a class feedback form and ideally they'd like to be able to send it near the end of the class.

We looked at Boomerang, which had issues with authenticating with the York domain, and broke the CSS of Gmail slightly. We tried mx Hero, a Chrome app but after a tip off from a colleague and reading the reviews and discovering it at times didn't send the email, or used http rather than https etc we thought it might be better to look into making our own solution.

Apps Script to the rescue!

I wanted something that was really simple, and easily editable to do what you want.

I made an Apps Script to check your Gmail Drafts folder. If there's a message whose subject begins with a date, like this, it sends it ...

[27/06/2014] Hello, this is a message from the future!

How to install it...

1. Go to the Apps Script here and choose the menu, File > Make a Copy. You will want to change the admin_email to your email address (line 32) so that you get a BCC copy of any mail sent out. I have set the emails to come from noReply. You can also change that in line 82.

2. Now, to authorise the script to run, click the Run button and then click OK. This is Google making sure you want this code to be able to be run.

3. From the menu Resources > "Current project's triggers" and create a Trigger that will run the check_drafts() function every 5 minutes or so.  See below....

4. Now you are ready to go. All you now need to do is create draft emails that have title with a date EXACTLY in the format above... All you then have to do is close the email... and wait. It might not send at exactly the time you specify, depending on how often your Trigger is set to run, but it'll be close.

Every time the script runs, it looks for messages that have a date in square brackets. It makes a copy, BCC-ing in the email you specified ( this is important because otherwise it doesn't keep a copy in your Sent folder... you need a copy to make sure it has indeed been sent ). 

The Script creates a label called "Scheduled Gmail Sent" and moves sent drafts to the bin ( unfortunately, Apps Script won't let me send the mail so it appears in the Sent tray or delete it outright ). Maybe you can work out how this bit works.

I couldn't get inline images to work, but attachments work fine.

Remember your Google Gmail quota is 500 a day, but a message with 300 people BCC'd in counts only as one message.

Thursday, 5 June 2014

Email Me When Someone Edits a Google Document

Someone asked me if they could receive an email when a document has been edited.

function document_checker() {

  var doc = DriveApp.getFileById(doc_id)
  var last_update = doc.getLastUpdated()
  var scriptProperties = PropertiesService.getScriptProperties() 
  var last_checked = new Date( scriptProperties.getProperty('LAST_CHECKED') )
  Logger.log( "Last checked: " + last_checked + " " + typeof last_checked )
  Logger.log( last_update > last_checked )
  if (last_update > last_checked){
    Logger.log( "Sending mail" )
    MailApp.sendEmail("YOUR_EMAIL_GOES_HERE", "Document has been edited", doc.getUrl() )
    scriptProperties.setProperty('LAST_CHECKED', new Date() )

function setup(){
 var scriptProperties = PropertiesService.getScriptProperties();
 scriptProperties.setProperty('LAST_CHECKED', new Date() );

So... follow these steps.

a. Create an Apps Script in Google Drive ( you may need to "Connect more Apps" first and search for "Apps Script".

b. Paste in this script

c. Add your document ID. It's the long ugly bit in the URL of your document ( sometimes called a key ). Also change the YOUR_EMAIL_GOES HERE" bit.

d. Choose "setup" in the function menu and click "Run".

e. At this point you will need to authorize it.

f. Lastly, you create a Project Trigger to check ( once a day, once an hour, it's up to you ) and set it to run the "document_checker" function.

Crikey... it sounds difficult when you write it down. It's not that bad, trust me. I just thought this might be handy for someone. C'mon, it's easier than checking every hour :-)

p.s You might want to fancy up the email that gets sent. That's up to you, you design guru you.

Tuesday, 3 June 2014