Skip to content

Instantly share code, notes, and snippets.

@harryfk
Last active January 29, 2017 22:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harryfk/4b31f27e7578efe7d7186f0df2fd3afb to your computer and use it in GitHub Desktop.
Save harryfk/4b31f27e7578efe7d7186f0df2fd3afb to your computer and use it in GitHub Desktop.
// this is a Google Apps script which fetches a random gif from Giphy and then sends it via email to people
function sendMail(){
var url = "http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=thanks";
var response = UrlFetchApp.fetch(url, {}).getContentText();
response = JSON.parse(response);
var subject = 'Your friendly time tracking reminder.';
var body = 'Please, would you track your time today? It only takes five minutes.\n\n ';
var sender = 'Annoying Harry';
var attachmentId = fetchImageToDrive(response.data.image_original_url)
var file = DriveApp.getFileById(attachmentId);
GmailApp.sendEmail('harry@diesdas.digital', subject, body, { name: sender, attachments: [file.getAs(MimeType.GIF)] });
GmailApp.sendEmail('lorenz@diesdas.digital', subject, body, { name: sender, attachments: [file.getAs(MimeType.GIF)] });
// …
function fetchImageToDrive(imageUrl){
var imageBlob = UrlFetchApp.fetch(imageUrl).getBlob();
var image = DriveApp.createFile(imageBlob);
image.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);
return image.getId();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment