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

**URL:** <https://community.n8n.io/t/emailing-reading-a-template-once-emailing-to-many-with-a-personalized-content/50921>\
**Category:** Questions\
**Created:** [July 29, 2024, 10:32pm UTC](https://community.n8n.io/t/emailing-reading-a-template-once-emailing-to-many-with-a-personalized-content/50921 "2024-07-29T22:32:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hsteph](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/hsteph/32/42450_2.png) [@hsteph](https://community.n8n.io/u/hsteph)\
**Post date:** [July 29, 2024, 10:32pm UTC](https://community.n8n.io/t/emailing-reading-a-template-once-emailing-to-many-with-a-personalized-content/50921/1 "2024-07-29T22:32:19Z")

</div>

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, …

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/5/8/58fbbf8db0c7df32ae58cf805b57f8546a69359b.png)

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](https://europe1.discourse-cdn.com/n8n/original/3X/e/0/e04539b0f0c6da36b1545a1f462b00baaa2267b8.png)

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

---

<div class="post-metadata">

**Author:** ![n8n](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/n8n/32/196425_2.png) [@n8n](https://community.n8n.io/u/n8n)\
**Post date:** [July 29, 2024, 10:32pm UTC](https://community.n8n.io/t/emailing-reading-a-template-once-emailing-to-many-with-a-personalized-content/50921/2 "2024-07-29T22:32:20Z")

</div>

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:**

---

<div class="post-metadata">

**Author:** ![hsteph](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/hsteph/32/42450_2.png) [@hsteph](https://community.n8n.io/u/hsteph)\
**Post date:** [July 30, 2024, 12:04am UTC](https://community.n8n.io/t/emailing-reading-a-template-once-emailing-to-many-with-a-personalized-content/50921/3 "2024-07-30T00:04:01Z")

</div>

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

> **[Gmail campaign sender: Bulk-send emails and follow up automatically if no...](https://n8n.io/workflows/2137-gmail-campaign-sender-bulk-send-emails-and-follow-up-automatically-if-no-reply/)**
>
> This workflow allows you to send multi-step email campaigns using n8n, Gmail and Google Sheets. You define a sequence of emails, and a list of email...

---

<div class="post-metadata">

**Author:** ![ihortom](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/ihortom/32/11473_2.png) [@ihortom](https://community.n8n.io/u/ihortom)\
**Post date:** [July 30, 2024, 12:27am UTC](https://community.n8n.io/t/emailing-reading-a-template-once-emailing-to-many-with-a-personalized-content/50921/4 "2024-07-30T00:27:07Z")

</div>

@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](https://www.w3schools.com/jsref/jsref_obj_regexp.asp) 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)

---

<div class="post-metadata">

**Author:** ![hsteph](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/hsteph/32/42450_2.png) [@hsteph](https://community.n8n.io/u/hsteph)\
**Post date:** [July 30, 2024, 1:07am UTC](https://community.n8n.io/t/emailing-reading-a-template-once-emailing-to-many-with-a-personalized-content/50921/5 "2024-07-30T01:07:14Z")

</div>

> [@ihortom](#):
>
> **Merge** node in Multiplex mode

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 !

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/system/32/195824_2.png) [@system](https://community.n8n.io/u/system)\
**Post date:** [August 13, 2024, 2:37pm UTC](https://community.n8n.io/t/emailing-reading-a-template-once-emailing-to-many-with-a-personalized-content/50921/6 "2024-08-13T14:37:53Z")

</div>

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