Emailing, reading a template once, emailing to many with a personalized content

Hi Community

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 };

image

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

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Just checking this content from David Roberts as it is very similar to what I would like to do !

@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, \{\{ \}\}
  • The modifier /gm is used (global and multi-line)
2 Likes

This works perfectly !

In my n8n version, I don’t have the “MULTIPLEX” option. I have used “All possible combination” and it seems OK.

Thanks, I better understand the concept/mindset behind n8n !

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.