Send JSON result as email body

Hi there,

I would like to send the result of my last node as a JSON array as the actual email body content.

Is there any way that would allow me to do this? It seems I can only select the actual variables but not the full json result.

Any help or ideas are appreciated.

Welcome to the community @paul123

Not sure if I fully understand.

Can you please share the structure of the data of your ‘last node’ and how it should be send in the body?

So this is an example how it looks, and that’s exact the content I would like to have as the body of the email send.

{
    "order_id": "JSON-123",
    "currency_code": "GBP",
    "payment_method": "paypal",
    "order_date": "2017-09-10T15:12:40",
    "ust_id": "DE-985632",
    "email": "[email protected]",
    "telephone": "025552",
    "invoice": {
        "salutation": 1,
        "firstname": "Rainer",
        "lastname": "Rechnung",
        "housenumber": "42",
        "address_1": "Rechnungsstra\u00dfe 12",
        "address_2": "Rechnungszusatz 1",
        "zip": "12345",
        "state": "Bundesland/Bundesstaat",
        "city": "Rechnungshausen",
        "country": "DE",
        "company": "Rechnungsschreiber"
    },
    "shipping": {
        "salutation": 0,
        "firstname": "Lars",
        "lastname": "Liefer",
        "housenumber": "1337",
        "address_1": "Lieferstra\u00dfe 3",
        "address_2": "Lieferzusatz 2",
        "zip": "67890",
        "state": "Bundesland/Bundesstaat",
        "city": "Lieferhausen",
        "country": "FR",
        "company": "Liefermeister"
    },
    "order_status_id": 3,
    "internal_note": "Eine JSON Email Bestellung",
    "comment": "Bitte schnell bearbeiten",
    "shipcost": 4.99,
    "shipping_date": "mm/dd/yyyy",
    "order_items": [
        {
            "quantity": 2.33,
            "totalprice": 4.66,
            "product_id": null,
            "name": "Mein neuer Artikel",
            "sku": "1234-ddd",
            "imageurl": "https:\/\/some.url.tld\/image.jpeg",
            "taxrate": 19
        },
        {
            "quantity": 2.33,
            "totalprice": 4.66,
            "product_id": null,
            "name": "Mein neuer Artikel 2",
            "sku": "5789-ddd",
            "imageurl": "https:\/\/some.url.tld\/image.jpeg",
            "taxrate": 7
        }
    ],
    "tags": [
        "Stammkunde",
        "PRIO"
    ]
}

what do you mean exactly by the body of the email sent? are you using a node to send an email and you want this to be the body of the email?

Yes, exactly.

Can you tell me which node are you using to send the email, so that I can prepare an example with that node?

I’m using the ‘Send Email’ node.

Check the example below. Everything you want to reference data from a node you need to use expressions. Info about expressions can be found here https://docs.n8n.io/nodes/expressions.html#expressions.

If that does not help simply get back to me.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n    \"order_id\": \"JSON-123\",\n    \"currency_code\": \"GBP\",\n    \"payment_method\": \"paypal\",\n    \"order_date\": \"2017-09-10T15:12:40\",\n    \"ust_id\": \"DE-985632\",\n    \"email\": \"[email protected]\",\n    \"telephone\": \"025552\",\n    \"invoice\": {\n        \"salutation\": 1,\n        \"firstname\": \"Rainer\",\n        \"lastname\": \"Rechnung\",\n        \"housenumber\": \"42\",\n        \"address_1\": \"Rechnungsstra\\u00dfe 12\",\n        \"address_2\": \"Rechnungszusatz 1\",\n        \"zip\": \"12345\",\n        \"state\": \"Bundesland/Bundesstaat\",\n        \"city\": \"Rechnungshausen\",\n        \"country\": \"DE\",\n        \"company\": \"Rechnungsschreiber\"\n    },\n    \"shipping\": {\n        \"salutation\": 0,\n        \"firstname\": \"Lars\",\n        \"lastname\": \"Liefer\",\n        \"housenumber\": \"1337\",\n        \"address_1\": \"Lieferstra\\u00dfe 3\",\n        \"address_2\": \"Lieferzusatz 2\",\n        \"zip\": \"67890\",\n        \"state\": \"Bundesland/Bundesstaat\",\n        \"city\": \"Lieferhausen\",\n        \"country\": \"FR\",\n        \"company\": \"Liefermeister\"\n    },\n    \"order_status_id\": 3,\n    \"internal_note\": \"Eine JSON Email Bestellung\",\n    \"comment\": \"Bitte schnell bearbeiten\",\n    \"shipcost\": 4.99,\n    \"shipping_date\": \"mm/dd/yyyy\",\n    \"order_items\": [\n        {\n            \"quantity\": 2.33,\n            \"totalprice\": 4.66,\n            \"product_id\": null,\n            \"name\": \"Mein neuer Artikel\",\n            \"sku\": \"1234-ddd\",\n            \"imageurl\": \"https:\\/\\/some.url.tld\\/image.jpeg\",\n            \"taxrate\": 19\n        },\n        {\n            \"quantity\": 2.33,\n            \"totalprice\": 4.66,\n            \"product_id\": null,\n            \"name\": \"Mein neuer Artikel 2\",\n            \"sku\": \"5789-ddd\",\n            \"imageurl\": \"https:\\/\\/some.url.tld\\/image.jpeg\",\n            \"taxrate\": 7\n        }\n    ],\n    \"tags\": [\n        \"Stammkunde\",\n        \"PRIO\"\n    ]\n}\n  \n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        500,
        300
      ],
      "notesInFlow": true,
      "notes": "Mockup data"
    },
    {
      "parameters": {
        "html": "={{$node[\"Function\"].json}}",
        "options": {}
      },
      "name": "Send Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 1,
      "position": [
        700,
        300
      ],
      "credentials": {
        "smtp": "asasasas"
      }
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Many thanks! This would definitely help, didn’t know about the expressions.

I tried to run the solution you provided, but do get an error:

    ERROR: The "chunk" argument must be of type string or an instance of Buffer. Received an instance of Object
TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer. Received an instance of Object
    at validChunk (_stream_writable.js:281:10)
    at PassThrough.Writable.write (_stream_writable.js:316:21)
    at PassThrough.Writable.end (_stream_writable.js:585:10)
    at Immediate._onImmediate (/usr/local/lib/node_modules/n8n/node_modules/nodemailer/lib/mime-node/index.js:961:46)
    at processImmediate (internal/timers.js:456:21)

can you share the workflow with me? So that I can have a proper look. Select all the nodes, copy and paste

I copied the workflow you provided me with to an otherwise blank project. Only changed the SMTP and mailadress details. Does it not give problems if you try to run it?

ahh yeah sorry thought it was a different workflow. Either way found the issue. Check the example below.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n    \"order_id\": \"JSON-123\",\n    \"currency_code\": \"GBP\",\n    \"payment_method\": \"paypal\",\n    \"order_date\": \"2017-09-10T15:12:40\",\n    \"ust_id\": \"DE-985632\",\n    \"email\": \"[email protected]\",\n    \"telephone\": \"025552\",\n    \"invoice\": {\n        \"salutation\": 1,\n        \"firstname\": \"Rainer\",\n        \"lastname\": \"Rechnung\",\n        \"housenumber\": \"42\",\n        \"address_1\": \"Rechnungsstra\\u00dfe 12\",\n        \"address_2\": \"Rechnungszusatz 1\",\n        \"zip\": \"12345\",\n        \"state\": \"Bundesland/Bundesstaat\",\n        \"city\": \"Rechnungshausen\",\n        \"country\": \"DE\",\n        \"company\": \"Rechnungsschreiber\"\n    },\n    \"shipping\": {\n        \"salutation\": 0,\n        \"firstname\": \"Lars\",\n        \"lastname\": \"Liefer\",\n        \"housenumber\": \"1337\",\n        \"address_1\": \"Lieferstra\\u00dfe 3\",\n        \"address_2\": \"Lieferzusatz 2\",\n        \"zip\": \"67890\",\n        \"state\": \"Bundesland/Bundesstaat\",\n        \"city\": \"Lieferhausen\",\n        \"country\": \"FR\",\n        \"company\": \"Liefermeister\"\n    },\n    \"order_status_id\": 3,\n    \"internal_note\": \"Eine JSON Email Bestellung\",\n    \"comment\": \"Bitte schnell bearbeiten\",\n    \"shipcost\": 4.99,\n    \"shipping_date\": \"mm/dd/yyyy\",\n    \"order_items\": [\n        {\n            \"quantity\": 2.33,\n            \"totalprice\": 4.66,\n            \"product_id\": null,\n            \"name\": \"Mein neuer Artikel\",\n            \"sku\": \"1234-ddd\",\n            \"imageurl\": \"https:\\/\\/some.url.tld\\/image.jpeg\",\n            \"taxrate\": 19\n        },\n        {\n            \"quantity\": 2.33,\n            \"totalprice\": 4.66,\n            \"product_id\": null,\n            \"name\": \"Mein neuer Artikel 2\",\n            \"sku\": \"5789-ddd\",\n            \"imageurl\": \"https:\\/\\/some.url.tld\\/image.jpeg\",\n            \"taxrate\": 7\n        }\n    ],\n    \"tags\": [\n        \"Stammkunde\",\n        \"PRIO\"\n    ]\n}\n  \n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        490,
        300
      ],
      "notesInFlow": true,
      "notes": "Mockup data"
    },
    {
      "parameters": {
        "text": "={{JSON.stringify($node[\"Function\"].json)}}",
        "options": {}
      },
      "name": "Send Email1",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 1,
      "position": [
        750,
        300
      ],
      "credentials": {
        "smtp": "asasasas"
      }
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Send Email1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like