Embed random image from a folder into email body to help with free labour

Describe the issue/error/question

My mother has kindly offered to help me with my business affairs and is filing my mountains of paperwork for me as I scan it. I’ve successfully created a workflow that uploads new scans to Nextcloud and then sends an email to her with some details about the file and a link to the folder in which it’s saved.

This is random both in concept and practice:
I want to embed a random image of her grandchildren taken from a local folder (or Nextcloud, maybe as a step 2), at the bottom of each email to make it a more pleasant experience for her. The best thing I can come up with at the moment is using a static URL to embed the image into the email and creating either a separate workflow or maybe even use a bash script or something to change a list of files from a directory to said filename/URL once per day, however, the trigger for randomisation is not time-based so this wouldn’t really work properly unless it was set to rename ever 1 minute or something like that. That seems too resource intensive and in my opinion it should be possible to use logic to randomly select a file from a list of files rather than manipulate the files to become consistent with the output requirement.

It’s partly a thought experiment for me after having caught the n8n bug in a big way over the last few weeks, and I’m curious to know how others might approach it?

1 Like

I thought I’d start by creating an array of filenames and randomising which one is printed to the console based on code snippets I’ve seen on stackoverflow. It’s very simple at the moment and works when testing on JSFiddle code playground and other places:

The error message is:
ERROR: Cannot read properties of undefined (reading 'every') [Line 752]

(Line 752??)

If I can get it working with an static array of values, then I figure I’d use the read binary files node to read from a directory and then write the values into said array. I’ve no idea how to do that yet and this isn’t even working, but baby step ‘n’ all that :slight_smile:

Hey @rapidscampi,

Try this for your function.

const pics = ["pic1.jpg", "pic2.jpg", "pic3.jpg", "pic4.jpg", "pic5.jpg", "pic6.jpg", "pic7.jpg"];
return {'image': pics[Math.floor(Math.random() * pics.length)]};
1 Like