I’m learning n8n, … trying to implement a very simple emailing solution, reading contacts from a Google Sheet (email, firstname, lastname) merging each item with a template containing some tags like {{firstname}}, … {{lastname}}… to be replaced by real content, and then send each personalized content via SMTP. Something that should be very easy, …
Reading contacts, and sending emails woks fine.
I have an issue with the merge. Not sure how to do that? With a code node ? I have tried the following code :
code node
var result = $node[“template”].json[“template”];
let firstname = $item(“0”).$node[“contacts”].json[“firstname”];
result = result.replace(“{{firstname}}”, firstname);
return { result };
This does not produce the expected result as this code returns an empty string when I run the global process but produces the correct content when tested during development.
Also, I’m not sure how the global workflow should look like. I’m trying to use the code node as a lookup (enrichment) but it does not work this way I think, …
Happy to get your recommandations,… I didn’t find similar or existing templates to learn from. You maybe have something similar to share ?
Thanks for your help,
Information on my n8n setup
I’m using the latest n8n version, self hosted on a Win 11 machine, standard npm installation
@hsteph , following your initial question and the attempt to build the workflow, I can suggest the following:
Explanation:
Merge node in Multiplex mode combines each contact with a template
Set node called output substitutes your template expressions with the actual values from contacts node producing the email content accompanied with the recipient email address
The template substitution uses RegEx to accommodate variations of the templated expression (just in case). In particular, /\{\{ ?firstname ?\}\}/gm. This patterns allow for an optional space after and before the opening and closing curly brackets respectively.
You can extend the expression with the other replacement as in {{ $json.template.replaceAll(/\{\{ ?firstname ?\}\}/gm, $json["firstname"]).replaceAll(/\{\{ ?lastname ?\}\}/gm, $json["lastname"]) }}
More about RegEx:
The curly braces are the reserved characters. Hence I had to escape them, \{\{ \}\}