Skip to content

Instantly share code, notes, and snippets.

@dfkoz
Last active June 30, 2023 12:49
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dfkoz/5860786 to your computer and use it in GitHub Desktop.
Save dfkoz/5860786 to your computer and use it in GitHub Desktop.
The Honeymailer sends a random photo from Google Drive at a preset interval.
// HOW TO USE THIS SCRIPT:
// 0. Upload your photos to Google Drive.
// 1. Log into Google Drive, and click CREATE -> Script -> Blank Project.
// 2. Delete all of the default code, and paste this code into your project.
// 3. Customize the email parameters below to your liking (note that the quotes around each line are very important.)
// 4. Test the script by selecting Run -> sendRandomPic. You should receive an email with a random picture.
// 5. Click Resources -> Current Project's Triggers, and set up the email to run as often as you would like.
// Name of the folder that contains your photos.
// Leave blank to search your top-level folder.
var folderName = "";
var extensions = ["JPG", "JPEG", "PNG", "GIF"];
// Email parameters
var to = "you@domain.com" // Comma-separated list of email addresses.
var subject = "Honeymailer Daily" // Subject line.
var body = "<p>Your daily picture:</p>" + // Body of the email. Important that the.
"<p><img src='cid:imageBlob' style='width: 200px;' /></p>" +
"<p>Like this one? See it <a href='{picture-link}'>here</a>.</p>";
function sendRandomPic() {
// Scan each file to see if it matches any of our filters.
// Iterating over all files appears to be faster than searching [?]
var folder = DocsList.getFolder(folderName);
var results = [];
var flag = true;
var startIndex = 0;
while (flag) {
var files = folder.getFiles(startIndex, 250);
for (f in files) {
var file = files[f];
var ext = file.getName().split('.').pop();
if (extensions.indexOf(ext) > -1) results.push(file);
}
files.length > 0 ? startIndex += files.length : flag = false;
}
// Finally, select a random file.
if (results.length == 0) {
file = null;
} else {
file = results[Math.floor(Math.random() * results.length + 1)];
}
if (file) {
// Send an email containing the picture.
MailApp.sendEmail({
to: to,
subject: subject,
htmlBody: body.replace("{picture-link}",file.getUrl()),
inlineImages: { imageBlob: file.getThumbnail() },
});
}
}
@Artapel
Copy link

Artapel commented Sep 21, 2017

Good day. Please may i ask for your assistance

I Would like to Have the script pick a random line from a txt or csv file and use it as the subject line. I would also like to Have the script pick a random line from a txt or csv file and use it in the the body of the E-mail.

Would you mind helping me with this?

Regards

@VarsharaniDesai
Copy link

Hey! Great work! I need your help with a error that I am receiving sometimes after execution of the script.

TypeError: Cannot read properties of undefined (reading 'getUrl')

Could you please help me with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment