Problem forwarding attachements received by IMAP with Send Email

Hello,

I have problems forwarding attachments with the Send Email node. Here’s what I try to do:

  • I receive emails with IMAP Emails (works :heavy_check_mark:)
  • I extract, transform, enrich data from the email body via Function and HTTP Request nodes (works :heavy_check_mark:)
  • I create and send an email using the results of that transformation for email content (works :heavy_check_mark:, new mail with additional information arrives in my post box)

However, now I want to add attachments of the original email to the mail that I send. But I cannot figure out how and I cannot make sense of the examples I find in this forum.

I tried and add a reference in the attachments field of Send Email, like {{$node[“IMAP Email”].binary.attachment_0}} or {{$node[“IMAP Email”].binary}} but no attachment is sent. I think I understood that this has to do with the reference somehow pointing to the parameters only and not the data. But how to I access the data? Any hint is highly appreciated.

Thanks!

Hey @Steffen,

Welcome to the community :sparkling_heart:

In your workflow do you change the Binary Property? To help you better understand, in your IMAP node the value of the Binary Property is attachment_0. Throughout the workflow if you don’t change the value, you can add attachment_0 in the Send Email node.

If you can share the workflow, we can take a close look.
(Check out this to learn how you can share the workflow: How can I contribute? | Docs)

Thanks for your quick answer. Hm, that’s what I tried but I guess there’s something I was missing.

Here’s the code:

{
  "name": "Add Context Data",
  "nodes": [
    {
      "parameters": {
        "authentication": "basicAuth",
        "url": "my service url",
        "options": {},
        "headerParametersUi": {
          "parameter": []
        },
        "queryParametersUi": {
          "parameter": []
        }
      },
      "name": "Testing Tools",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        350,
        180
      ],
      "alwaysOutputData": false,
      "credentials": {
        "httpBasicAuth": "my credentials"
      }
    },
    {
      "parameters": {
        "downloadAttachments": true,
        "options": {}
      },
      "name": "IMAP Email",
      "type": "n8n-nodes-base.emailReadImap",
      "typeVersion": 1,
      "position": [
        -100,
        180
      ],
      "credentials": {
        "imap": "my IMAP credentials"
      }
    },
    {
      "parameters": {
        "fromEmail": "[email protected]",
        "toEmail": "[email protected]",
        "subject": "={{$node[\"IMAP Email\"].json[\"subject\"]}}",
        "text": "={{$node[\"IMAP Email\"].json[\"textPlain\"]}}",
        "html": "={{$node[\"Email Info Formater\"].json[\"html\"]}}",
        "attachments": "=attachment_0",
        "options": {
          "allowUnauthorizedCerts": true
        }
      },
      "name": "Send Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 1,
      "position": [
        780,
        180
      ],
      "credentials": {
        "smtp": "my smpt credentials"
      }
    },
    {
      "parameters": {
        "functionCode": "nvar regEx = /[0-9]{4}-[0-9]{3}-[0-9]{4}/;\nvar id = item.subject.match(regEx)[0];\n\nvar returnValue = {'id': id, 'html': item.textHtml}\nreturn returnValue;"
      },
      "name": "Extract Email Content",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        120,
        180
      ]
    },
    {
      "parameters": {
        "functionCode": "var ph = item;\nvar phInfo = '<div some html...>';\n\nvar regEx = /<tbody>/;\nvar phInfoHtml =  '<tbody><tr><td>' + phInfo + '</td></tr>';\nvar sourceHtml = $evaluateExpression('{{$node[\"Extract Email Content\"].json[\"html\"]}}');\n//var html = sourceHtml.search(regEx);\nvar html = sourceHtml.replace(regEx, phInfoHtml);\n\nreturnValue = {'phInfo': phInfo, 'html': html};\n\nreturn returnValue;"
      },
      "name": "Email Info Formater",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        580,
        180
      ]
    }
  ],
  "connections": {
    "Testing Tools": {
      "main": [
        [
          {
            "node": "Email Info Formater",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IMAP Email": {
      "main": [
        [
          {
            "node": "Extract Email Content",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Email Content": {
      "main": [
        [
          {
            "node": "Testing Tools",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Email Info Formater": {
      "main": [
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "saveExecutionProgress": "DEFAULT"
  },
  "id": 2
}

The problem is that the binary data gets “lost” in the second HTTP Request node (“Testing Tools”) as it replaces the data. So what you would have to do is to merge it back in again.

You can do that with a Merge node set to “Merge By Index” like this:
Screenshot from 2021-07-16 17-30-36

In your case you have to add the Merge Node before the “Send Email” node with the second input connected to the first node (“IMAP Email”).

Here the above example worfklow:

Click to expand! ```json { "nodes": [ { "parameters": { "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/Test-Logo.svg/783px-Test-Logo.svg.png", "responseFormat": "file", "options": {} }, "name": "HTTP Request", "type": "n8n-nodes-base.httpRequest", "typeVersion": 1, "position": [ 450, 700 ] }, { "parameters": { "url": "http://example.com", "responseFormat": "string", "options": {} }, "name": "HTTP Request1", "type": "n8n-nodes-base.httpRequest", "typeVersion": 1, "position": [ 700, 700 ] }, { "parameters": { "mode": "mergeByIndex" }, "name": "Merge", "type": "n8n-nodes-base.merge", "typeVersion": 1, "position": [ 900, 900 ] } ], "connections": { "HTTP Request": { "main": [ [ { "node": "HTTP Request1", "type": "main", "index": 0 }, { "node": "Merge", "type": "main", "index": 1 } ] ] }, "HTTP Request1": { "main": [ [ { "node": "Merge", "type": "main", "index": 0 } ] ] } } } ```

Nice! That solved my problem. I’m slowly getting the idea of how the data flows. Thanks a lot.

You are welcome. That is great to hear!

Have fun!