Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Send Email Node): Add content-id for email attachments #3632

Merged
merged 5 commits into from
May 19, 2023

Conversation

pemontto
Copy link
Contributor

@pemontto pemontto commented Jul 1, 2022

✨ Feature

This PR adds Content-IDs (CIDs) for email attachments based on their property name. Using CIDs is the most widely supported way to include inline images in HTML emails.

Using the workflow below you can send HTML emails with inline images from the attached files

View sample workflow
{
  "nodes": [
    {
      "parameters": {
        "functionCode": "items[0].json = [\n  {\n    name: '200'\n  },\n  {\n    name: '300'\n  },\n  {\n    name: '300'\n  },\n  {\n    name: '300'\n  },\n];\nreturn items;"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -220,
        -120
      ]
    },
    {
      "parameters": {
        "url": "=https://picsum.photos/{{$json[\"name\"]}}",
        "responseFormat": "file",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        180,
        -120
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.map(item => {\n  return {\n    json: item\n  }\n});\n"
      },
      "name": "Split into Items",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -20,
        -120
      ]
    },
    {
      "parameters": {
        "functionCode": "const binaryProperties = {};\nlet bodyHTML = '';\nfor (let i=0;i<items.length;i++) {\n  let item = items[i];\n  binaryProperties[`data${i}`] = item.binary.data;\n  bodyHTML += `\\n<h4>img cid:data${i}</h4><img src=\"cid:data${i}\">`\n}\n\n\n\nreturn [\n  {\n    json: {\n      binaryProperties: Object.keys(binaryProperties).join(','),\n      bodyHTML\n    },\n    binary: binaryProperties,\n  }\n]\n"
      },
      "name": "Merge into one item",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        380,
        -120
      ]
    },
    {
      "parameters": {
        "fromEmail": "me@example.com",
        "toEmail": "joe@example.com",
        "subject": "Test CID Embedded Images",
        "text": "Images",
        "html": "=<h1>Images</h1>\n{{ $json.bodyHTML }}",
        "attachments": "={{$json[\"binaryProperties\"]}}",
        "options": {
          "allowUnauthorizedCerts": true
        }
      },
      "name": "Send Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 1,
      "position": [
        580,
        -120
      ],
      "credentials": {
        "smtp": {
          "id": "47",
          "name": "SMTP account"
        }
      }
    }
  ],
  "connections": {
    "Mock Data": {
      "main": [
        [
          {
            "node": "Split into Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Merge into one item",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split into Items": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge into one item": {
      "main": [
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

🐛 Bug

This also fixes an issues where the error object has a circular reference with certificate errors which makes things very hard to debug, particularly in large workflows. Instead of seeing this:

The error now looks like this:

@n8n-assistant n8n-assistant bot added community Authored by a community member node/improvement New feature or request labels Jul 1, 2022
@Joffcom
Copy link
Member

Joffcom commented May 18, 2023

NODE-541

@Joffcom Joffcom added the in linear Issue or PR has been created in Linear for internal review label May 18, 2023
@codecov
Copy link

codecov bot commented May 19, 2023

Codecov Report

Patch coverage has no change and project coverage change: -0.01 ⚠️

Comparison is base (e88b9d8) 27.69% compared to head (39913e8) 27.69%.

❗ Current head 39913e8 differs from pull request most recent head 1b091d4. Consider uploading reports for the commit 1b091d4 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3632      +/-   ##
==========================================
- Coverage   27.69%   27.69%   -0.01%     
==========================================
  Files        2948     2948              
  Lines      180869   180871       +2     
  Branches    19635    19635              
==========================================
  Hits        50090    50090              
- Misses     130034   130036       +2     
  Partials      745      745              
Impacted Files Coverage Δ
...es/nodes-base/nodes/EmailSend/v2/send.operation.ts 0.00% <0.00%> (ø)

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@michael-radency michael-radency changed the title Add content-id for email attachments feat(Send Email Node): Add content-id for email attachments May 19, 2023
@michael-radency michael-radency merged commit 8fe8aad into n8n-io:master May 19, 2023
10 of 11 checks passed
@Joffcom
Copy link
Member

Joffcom commented May 19, 2023

Thanks for the PR @pemontto 🙌

MiloradFilipovic added a commit that referenced this pull request May 20, 2023
* master:
  fix(core): Prevent app crashes because of unhandled promises in poll and trigger nodes (#6278)
  fix(Postgres Node): Remove reusable connections (no-changelog) (#6259)
  ci(editor): Enfore stricter linting rules from `eslint-plugin-vue` (no-changelog) (#6242)
  fix(Strava Trigger Node): Fix issue with delete events failing to display data (#6277)
  feat(SSH Node): Credentials test (#6279)
  feat(Send Email Node): Add content-id for email attachments (#3632)
  feat(core): Include openssh in the base docker image (no-changelog) (#6275)

# Conflicts:
#	packages/nodes-base/nodes/Postgres/v2/actions/database/update.operation.ts
@janober
Copy link
Member

janober commented May 25, 2023

Got released with n8n@0.230.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Authored by a community member in linear Issue or PR has been created in Linear for internal review node/improvement New feature or request Released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants