@lowdefy/connection-smtp 6.0.0 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,6 +16,10 @@
16
16
  import schema from './schema.js';
17
17
  export default {
18
18
  schema,
19
+ // An outside service, not app data: a journey on a data set keeps its real target.
20
+ meta: {
21
+ dataSet: 'external'
22
+ },
19
23
  requests: {
20
24
  SMTPMailSend
21
25
  }
@@ -13,17 +13,37 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { type } from '@lowdefy/helpers';
16
+ import resolveMailFilter from '../resolveMailFilter.js';
16
17
  import send from '../send.js';
17
18
  import schema from './schema.js';
18
- async function SMTPMailSend({ request, connection }) {
19
+ async function SMTPMailSend({ request, connection, environment }) {
19
20
  const messages = type.isArray(request) ? request : [
20
21
  request
21
22
  ];
23
+ // The current environment can switch email off (config.environments.<env>.email.enabled: false):
24
+ // nothing is sent, and each message reports it was not.
25
+ if (environment?.email?.enabled === false) {
26
+ return {
27
+ response: 'Mail is disabled in this environment.',
28
+ results: messages.map(()=>({
29
+ messageId: null,
30
+ to: null,
31
+ disabled: true
32
+ }))
33
+ };
34
+ }
35
+ const filter = resolveMailFilter({
36
+ connection,
37
+ environment
38
+ });
22
39
  const results = [];
23
40
  // Send sequentially so a single SMTP connection or pool is not overwhelmed.
24
41
  for (const mail of messages){
25
42
  results.push(await send({
26
- connection,
43
+ connection: {
44
+ ...connection,
45
+ filter
46
+ },
27
47
  mail
28
48
  }));
29
49
  }
@@ -12,27 +12,90 @@
12
12
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
- */ import { type } from '@lowdefy/helpers';
15
+ */ // eslint-plugin-import's node resolver does not read package exports maps.
16
+ // eslint-disable-next-line import/no-unresolved
17
+ import addressparser from 'nodemailer/lib/addressparser';
18
+ import { type } from '@lowdefy/helpers';
19
+ // nodemailer reads a string recipient as a comma-separated list, so a string holding several
20
+ // addresses is split into one entry per address, with nodemailer's own parser. A string holding
21
+ // one address is kept as written.
22
+ function splitEntries(entries) {
23
+ if (type.isNone(entries)) {
24
+ return [];
25
+ }
26
+ return (type.isArray(entries) ? entries : [
27
+ entries
28
+ ]).flatMap((entry)=>{
29
+ if (!type.isString(entry)) {
30
+ return [
31
+ entry
32
+ ];
33
+ }
34
+ const parsed = addressparser(entry, {
35
+ flatten: true
36
+ });
37
+ return parsed.length > 1 ? parsed : [
38
+ entry
39
+ ];
40
+ });
41
+ }
42
+ // An entry from splitEntries: a string with one address, a request object ({ name, email }) or
43
+ // a parsed nodemailer object ({ name, address }).
16
44
  function getBareEmail(entry) {
17
45
  if (type.isObject(entry)) {
18
- return String(entry.email ?? '').toLowerCase();
46
+ return String(entry.address ?? entry.email ?? '').trim().toLowerCase();
47
+ }
48
+ if (!type.isString(entry)) {
49
+ return '';
50
+ }
51
+ const [parsed] = addressparser(entry, {
52
+ flatten: true
53
+ });
54
+ return (parsed?.address ?? '').trim().toLowerCase();
55
+ }
56
+ function getDomain(email) {
57
+ return email.slice(email.lastIndexOf('@') + 1);
58
+ }
59
+ // .invalid is reserved and never resolves (RFC 6761). A trailing dot is the fully qualified
60
+ // form of the same domain.
61
+ function isInvalid(entry) {
62
+ const domain = getDomain(getBareEmail(entry)).replace(/\.+$/, '');
63
+ return domain === 'invalid' || domain.endsWith('.invalid');
64
+ }
65
+ // Returns the field as written when nothing is dropped, so mail without .invalid recipients
66
+ // reaches nodemailer unchanged.
67
+ function dropInvalid(entries) {
68
+ const split = splitEntries(entries);
69
+ const kept = split.filter((entry)=>!isInvalid(entry));
70
+ if (kept.length === split.length) {
71
+ return entries;
19
72
  }
20
- const angleMatch = /<([^<>]+)>/.exec(String(entry));
21
- if (angleMatch) {
22
- return angleMatch[1].toLowerCase();
73
+ return kept.length === 0 ? undefined : kept;
74
+ }
75
+ // Mail to .invalid is dropped on every send, filter or not, and only those recipients are
76
+ // dropped: a real cc or bcc recipient still gets the mail when every to recipient is .invalid.
77
+ // Returns null when no recipient is left.
78
+ function dropInvalidRecipients(mail) {
79
+ const to = dropInvalid(mail.to);
80
+ const cc = dropInvalid(mail.cc);
81
+ const bcc = dropInvalid(mail.bcc);
82
+ if (type.isNone(to) && type.isNone(cc) && type.isNone(bcc)) {
83
+ return null;
23
84
  }
24
- return String(entry).toLowerCase();
85
+ return {
86
+ ...mail,
87
+ to,
88
+ cc,
89
+ bcc
90
+ };
25
91
  }
26
92
  function createMatcher({ allowlist, regex }) {
27
93
  const allowedDomains = type.isNone(allowlist) ? null : allowlist.map((domain)=>domain.toLowerCase());
28
94
  const pattern = type.isNone(regex) ? null : new RegExp(regex);
29
95
  return function matches(entry) {
30
96
  const email = getBareEmail(entry);
31
- if (allowedDomains !== null) {
32
- const domain = email.slice(email.lastIndexOf('@') + 1);
33
- if (!allowedDomains.includes(domain)) {
34
- return false;
35
- }
97
+ if (allowedDomains !== null && !allowedDomains.includes(getDomain(email))) {
98
+ return false;
36
99
  }
37
100
  if (pattern !== null && !pattern.test(email)) {
38
101
  return false;
@@ -41,14 +104,13 @@ function createMatcher({ allowlist, regex }) {
41
104
  };
42
105
  }
43
106
  function filterEntries({ entries, matches }) {
44
- if (type.isNone(entries)) {
45
- return [];
46
- }
47
- return (type.isArray(entries) ? entries : [
48
- entries
49
- ]).filter(matches);
107
+ return splitEntries(entries).filter(matches);
50
108
  }
51
- function applyMailFilter({ filter, mail }) {
109
+ function applyMailFilter({ filter, mail: received }) {
110
+ const mail = dropInvalidRecipients(received);
111
+ if (mail === null) {
112
+ return null;
113
+ }
52
114
  if (type.isNone(filter)) {
53
115
  return mail;
54
116
  }
@@ -0,0 +1,35 @@
1
+ /*
2
+ Copyright 2020-2026 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { type } from '@lowdefy/helpers';
16
+ // The connection's own filter wins over the current environment's delivery filter
17
+ // (config.environments.<env>.email.filter) when it sets at least one field. `false` turns
18
+ // filtering off, the environment's too — for mail that must reach the real recipient in every
19
+ // environment. An unset connection filter, or one whose fields all resolve to null (a
20
+ // `_secret` that is not set on this deployment, say), falls back to the environment's, so a
21
+ // missing value can never switch the safety net off.
22
+ function setsAField(filter) {
23
+ if (!type.isObject(filter)) return false;
24
+ return [
25
+ 'replaceAddress',
26
+ 'allowlist',
27
+ 'regex'
28
+ ].some((field)=>!type.isNone(filter[field]));
29
+ }
30
+ function resolveMailFilter({ connection, environment }) {
31
+ if (connection.filter === false) return null;
32
+ if (setsAField(connection.filter)) return connection.filter;
33
+ return environment?.email?.filter ?? null;
34
+ }
35
+ export default resolveMailFilter;
@@ -101,12 +101,19 @@ export default {
101
101
  description: 'Default email address replies should be sent to.'
102
102
  },
103
103
  // null allowed on all filter values because they come from _secret;
104
- // unset secrets resolve null - an unset filter field is disabled.
104
+ // unset secrets resolve null - an unset filter field is disabled. An unset
105
+ // (null) filter falls back to the current environment's email filter;
106
+ // false turns filtering off, the environment's too. Recipients on the reserved
107
+ // .invalid domain are always dropped (applyMailFilter.js).
105
108
  filter: {
106
109
  type: [
107
110
  'object',
108
- 'null'
111
+ 'null',
112
+ 'boolean'
109
113
  ],
114
+ not: {
115
+ const: true
116
+ },
110
117
  additionalProperties: false,
111
118
  properties: {
112
119
  replaceAddress: {
package/dist/types.js CHANGED
@@ -16,6 +16,13 @@
16
16
  connections: [
17
17
  'SMTP'
18
18
  ],
19
+ // Non-scopable: the tenant wall does not cover this type, so its
20
+ // connections are never scoped and accept no tenant: declaration.
21
+ connectionMetas: {
22
+ SMTP: {
23
+ tenant: false
24
+ }
25
+ },
19
26
  requests: [
20
27
  'SMTPMailSend'
21
28
  ]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowdefy/connection-smtp",
3
- "version": "6.0.0",
3
+ "version": "7.0.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
@@ -27,8 +27,12 @@
27
27
  "url": "https://github.com/lowdefy/lowdefy.git"
28
28
  },
29
29
  "type": "module",
30
+ "sideEffects": [
31
+ "**/*.css"
32
+ ],
30
33
  "exports": {
31
34
  "./connections": "./dist/connections.js",
35
+ "./send": "./dist/connections/SMTP/send.js",
32
36
  "./types": "./dist/types.js",
33
37
  "./transportSchema": "./dist/connections/SMTP/transportSchema.js"
34
38
  },
@@ -36,12 +40,12 @@
36
40
  "dist/*"
37
41
  ],
38
42
  "dependencies": {
39
- "@lowdefy/helpers": "6.0.0",
40
- "nodemailer": "9.0.3"
43
+ "@lowdefy/helpers": "7.0.0",
44
+ "nodemailer": "10.0.13"
41
45
  },
42
46
  "devDependencies": {
43
47
  "@jest/globals": "28.1.3",
44
- "@lowdefy/ajv": "6.0.0",
48
+ "@lowdefy/ajv": "7.0.0",
45
49
  "@swc/cli": "0.8.1",
46
50
  "@swc/core": "1.15.32",
47
51
  "@swc/jest": "0.2.39",