Create Email signatures through Typeform

Hey all! I just joined the n8n community and I’m loving the tool so far!

I’m currently working on the automation of creating and emailing email signatures but can’t seem to find a way to save a .html file and load that into an email. The purpose is fairly straightforward;

Step 1:
Get typeform data: name, email, phonenumber and title.

Step 2:
Add there variables into the existing html template

Step 3:
Save the html file to signature.html. This file can be overwritten every time as it’s only used once while sending.

Step 4:
Email the person with the file attached.

My question is specifically on how to save the file with the new variables in it. Step 1 and step 4 aren’t a problem.

Any able to help this rookie out? Thanks!

Welcome to the community @tino

Add there variables into the existing html template

You can replace the variables in the HTML using string.replace() in a function node.

Save the HTML file to signature.html. This file can be overwritten every time as it’s only used once while sending.

With a function node, you can also “create” the attachment. In other words, turn the HTML string to n8n binary data. Check n8n data structure..

The workflow should look something to the image below. You would have to replace the set node for the Typeform trigger and add a node to send emails depending on the email provider you are using.

The two important nodes here are: Replace Variables and Create Binary Data.

Function node Replace Variables:

const data = $node["Set"].json
let html = $node["Function"].json.html


for (const key of Object.keys(data)) {
  const regularExpression = new RegExp(`{${key}}`, 'g');
  html = html.replace(regularExpression, data[key])
}

return [
  {
    json:{
      html,
    }
  }
]

Function node Create Binary Data:

const html = items[0].json.html;

return [
  {
    json: {},
    binary: {
      data: {
        data: Buffer.from(html).toString('base64'),
        mimeType: 'text/html; charset=UTF-8',
        fileName: 'signature.html',
      }
    }
  }
]

Example workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        200,
        390
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      html: `<h1>My name is: {name}, and I'm {age} years old</h1>`\n    }\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        680,
        300
      ],
      "notesInFlow": true,
      "notes": "Get HTML template"
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "name",
              "value": "ricardo"
            },
            {
              "name": "age",
              "value": "30"
            }
          ],
          "number": []
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        380
      ],
      "notesInFlow": true,
      "notes": "Mockup Typeform"
    },
    {
      "parameters": {
        "functionCode": "const data = $node[\"Set\"].json\nlet html = $node[\"Function\"].json.html\n\n\nfor (const key of Object.keys(data)) {\n  const regularExpression = new RegExp(`{${key}}`, 'g');\n  html = html.replace(regularExpression, data[key])\n}\n\nreturn [\n  {\n    json:{\n      html,\n    }\n  }\n]\n\n"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1030,
        290
      ],
      "notesInFlow": true,
      "notes": "Replace Variables"
    },
    {
      "parameters": {
        "functionCode": "const html = items[0].json.html;\n\nreturn [\n  {\n    json: {},\n    binary: {\n      data: {\n        data: Buffer.from(html).toString('base64'),\n        mimeType: 'text/html; charset=UTF-8',\n        fileName: 'signature.html',\n      }\n    }\n  }\n]\n\n\n\n"
      },
      "name": "Function2",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1270,
        290
      ],
      "notesInFlow": true,
      "notes": "Create Binary Data"
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function1": {
      "main": [
        [
          {
            "node": "Function2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Works like a charm! Thank you @RicardoE105!

Glad that it worked. Have fun.

1 Like