Forwarder of email

Hello, I’m trying to build an email forwarder for our PEC address that don’t support this feature on new pec arrival.
So I started with IMAP Node and SMTP but I cannot send attachments, so I try to insert Function to extract Binary but how can i attach them to smtp?
this is my file

{
  "name": "IMPAS PEC",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "mailbox": "=INBOX",
        "downloadAttachments": true,
        "options": {
          "customEmailConfig": "[\"UNSEEN\"]"
        }
      },
      "name": "IMAP Email",
      "type": "n8n-nodes-base.emailReadImap",
      "typeVersion": 1,
      "position": [
        450,
        300
      ],
      "credentials": {
        "imap": "PEC FORWARD"
      }
    },
    {
      "parameters": {
        "fromEmail": "[email protected]",
        "toEmail": "[email protected];",
        "subject": "={{$node[\"IMAP Email\"].json[\"subject\"]}}",
        "text": "={{$node[\"IMAP Email\"].json[\"textPlain\"]}}",
        "html": "={{$node[\"IMAP Email\"].json[\"textHtml\"]}}",
        "attachments": "={{$binary.data}}",
        "options": {
          "allowUnauthorizedCerts": "={{false}}"
        }
      },
      "name": "Send Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 1,
      "position": [
        900,
        300
      ],
      "credentials": {
        "smtp": "SMTP account"
      }
    },
    {
      "parameters": {
        "functionCode": "return Object.keys(items[0].binary).map(key => {\n  return {\n    json: {},\n    binary: {\n      data: items[0].binary[key],\n    }\n  }\n});\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        670,
        150
      ]
    }
  ],
  "connections": {
    "IMAP Email": {
      "main": [
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          },
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Email": {
      "main": [
        []
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": 1
}

Regards
Alex

Hey @smilzao,

Welcome to the community :sparkling_heart:

Thank you for sharing the workflow code. I took a quick glance, and I have made some changes to it. You can use the below workflow

{
  "nodes": [
    {
      "parameters": {
        "downloadAttachments": true,
        "options": {
          "customEmailConfig": "[\"UNSEEN\"]"
        }
      },
      "name": "IMAP Email",
      "type": "n8n-nodes-base.emailReadImap",
      "typeVersion": 1,
      "position": [
        450,
        300
      ],
      "credentials": {
        "imap": "PEC FORWARD"
      }
    },
    {
      "parameters": {
        "fromEmail": "[email protected]",
        "toEmail": "[email protected]",
        "subject": "={{$node[\"IMAP Email\"].json[\"subject\"]}}",
        "text": "={{$node[\"IMAP Email\"].json[\"textPlain\"]}}",
        "html": "={{$node[\"IMAP Email\"].json[\"textHtml\"]}}",
        "attachments": "data",
        "options": {}
      },
      "name": "Send Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 1,
      "position": [
        850,
        300
      ],
      "credentials": {
        "smtp": "SMTP account"
      }
    },
    {
      "parameters": {
        "functionCode": "return Object.keys(items[0].binary).map(key => {\n  return {\n    json: {},\n    binary: {\n      data: items[0].binary[key],\n    }\n  }\n});\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "IMAP Email": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Please note that I’ve not tested it, so it may not cover all the edge cases, however, tweaking it a bit would solve it!

Thank you, now it sends mail with attachments even I receive one mail for any attach.
No I try to study to send only one mail with all attachs

Thank you again
Alex

Ah yes. As mentioned earlier, I didn’t test it so it might have missed some edge case. Let me try it out :slight_smile:

Hey @smilzao,

Ran a quick test and found a solution. You can remove the Function node between the Email Read IMAP node and the Send Email node. In the Attachments parameter of the Send Email node you should use this expression: {{Object.keys($binary).join(',')}}. This expression will reference the name the binary data, and create a comma separated list. You will then receive a single email with multiple attachments. This will also work if an email has a single attachment.

Your workflow should like this: