Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. /**
  2. * HOW TO USE THIS SCRIPT:
  3. * 0. Upload your images to Google Drive.
  4. * 1. Log into Google Drive, and click CREATE -> Script -> Blank Project.
  5. * 2. Delete all of the default code, and paste this code into your project.
  6. * 3. Customize the email parameters below to your liking (note that the quotes
  7. * around each line are very important.)
  8. * 4. Test the script by selecting Run -> sendRandomImage. You should receive an
  9. * email with a random image.
  10. * 5. Click Resources -> Current Project's Triggers, and set up the email to run
  11. * as often as you would like.
  12. */
  13.  
  14. // Name of the folder that contains your photos
  15. // (leave blank to search your top-level folder)
  16. var folderName = "";
  17.  
  18. // Email parameters
  19. var to = "you@domain.com" // comma-separated list of email addresses
  20. var subject = "Honeymailer Daily" // subject line
  21. var body = "<p>Your random image:</p>" + // body of the email
  22. "<p><img src='cid:imageBlob' style='max-width: 100%;' /></p>" +
  23. "<p>Like this one? See it <a href='{picture-link}'>here</a>.</p>";
  24.  
  25. // Valid filetypes to search for
  26. var fileTypes = [MimeType.JPEG, MimeType.GIF, MimeType.PNG];
  27.  
  28. /**
  29. * Emails a random image in a Google Drive folder to an email address.
  30. */
  31. function sendRandomImage() {
  32. var folders = DriveApp.getFoldersByName(folderName);
  33. if (!folders.hasNext()) return;
  34. var folder = folders.next();
  35. var results = [];
  36. var flag = true;
  37. var startIndex = 0;
  38.  
  39. // Add all files with the correct filetypes to the results
  40. fileTypes.forEach(function(type) {
  41. var files = folder.getFilesByType(type);
  42. while (files.hasNext()) {
  43. results.push(files.next());
  44. }
  45. });
  46.  
  47. if (results.length == 0) return; // no results
  48.  
  49. // Select a random image
  50. var file = results[Math.floor(Math.random() * results.length + 1)];
  51.  
  52. // Send an email containing the image
  53. MailApp.sendEmail({
  54. to: to,
  55. subject: subject,
  56. htmlBody: body.replace("{picture-link}", file.getUrl()),
  57. inlineImages: {
  58. imageBlob: file.getBlob()
  59. }
  60. });
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement