@payloadcms/email-nodemailer 3.0.0-alpha.62
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.
- package/LICENSE.md +22 -0
- package/README.md +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +95 -0
- package/dist/index.js.map +1 -0
- package/package.json +49 -0
- package/src/index.ts +123 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018-2022 Payload CMS, LLC <info@payloadcms.com>
|
|
4
|
+
Portions Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Nodemailer Email Adapter
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Transporter } from 'nodemailer';
|
|
2
|
+
import type SMTPConnection from 'nodemailer/lib/smtp-connection';
|
|
3
|
+
import type { EmailAdapter } from 'payload/config';
|
|
4
|
+
export type NodemailerAdapterArgs = {
|
|
5
|
+
defaultFromAddress: string;
|
|
6
|
+
defaultFromName: string;
|
|
7
|
+
skipVerify?: boolean;
|
|
8
|
+
transport?: Transporter;
|
|
9
|
+
transportOptions?: SMTPConnection.Options;
|
|
10
|
+
};
|
|
11
|
+
export type NodemailerAdapter = EmailAdapter<unknown>;
|
|
12
|
+
/**
|
|
13
|
+
* Creates an email adapter using nodemailer
|
|
14
|
+
*
|
|
15
|
+
* If no email configuration is provided, an ethereal email test account is returned
|
|
16
|
+
*/
|
|
17
|
+
export declare const nodemailerAdapter: (args?: NodemailerAdapterArgs) => Promise<NodemailerAdapter>;
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAC7C,OAAO,KAAK,cAAc,MAAM,gCAAgC,CAAA;AAChE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAKlD,MAAM,MAAM,qBAAqB,GAAG;IAClC,kBAAkB,EAAE,MAAM,CAAA;IAC1B,eAAe,EAAE,MAAM,CAAA;IACvB,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,SAAS,CAAC,EAAE,WAAW,CAAA;IACvB,gBAAgB,CAAC,EAAE,cAAc,CAAC,OAAO,CAAA;CAC1C,CAAA;AAED,MAAM,MAAM,iBAAiB,GAAG,YAAY,CAAC,OAAO,CAAC,CAAA;AAErD;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,UACrB,qBAAqB,KAC3B,QAAQ,iBAAiB,CAc3B,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/* eslint-disable no-console */ import nodemailer from 'nodemailer';
|
|
2
|
+
import { InvalidConfiguration } from 'payload/errors';
|
|
3
|
+
/**
|
|
4
|
+
* Creates an email adapter using nodemailer
|
|
5
|
+
*
|
|
6
|
+
* If no email configuration is provided, an ethereal email test account is returned
|
|
7
|
+
*/ export const nodemailerAdapter = async (args)=>{
|
|
8
|
+
const { defaultFromAddress, defaultFromName, transport } = await buildEmail(args);
|
|
9
|
+
const adapter = ()=>({
|
|
10
|
+
defaultFromAddress,
|
|
11
|
+
defaultFromName,
|
|
12
|
+
sendEmail: async (message)=>{
|
|
13
|
+
return await transport.sendMail({
|
|
14
|
+
from: `${defaultFromName} <${defaultFromAddress}>`,
|
|
15
|
+
...message
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
return adapter;
|
|
20
|
+
};
|
|
21
|
+
async function buildEmail(emailConfig) {
|
|
22
|
+
if (!emailConfig) {
|
|
23
|
+
const transport = await createMockAccount(emailConfig);
|
|
24
|
+
if (!transport) throw new InvalidConfiguration('Unable to create Nodemailer test account.');
|
|
25
|
+
return {
|
|
26
|
+
defaultFromAddress: 'info@payloadcms.com',
|
|
27
|
+
defaultFromName: 'Payload',
|
|
28
|
+
transport
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
// Create or extract transport
|
|
32
|
+
let transport;
|
|
33
|
+
if ('transport' in emailConfig && emailConfig.transport) {
|
|
34
|
+
({ transport } = emailConfig);
|
|
35
|
+
} else if ('transportOptions' in emailConfig && emailConfig.transportOptions) {
|
|
36
|
+
transport = nodemailer.createTransport(emailConfig.transportOptions);
|
|
37
|
+
} else {
|
|
38
|
+
transport = await createMockAccount(emailConfig);
|
|
39
|
+
}
|
|
40
|
+
if (emailConfig.skipVerify !== false) {
|
|
41
|
+
await verifyTransport(transport);
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
defaultFromAddress: emailConfig.defaultFromAddress,
|
|
45
|
+
defaultFromName: emailConfig.defaultFromName,
|
|
46
|
+
transport
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
async function verifyTransport(transport) {
|
|
50
|
+
try {
|
|
51
|
+
await transport.verify();
|
|
52
|
+
} catch (err) {
|
|
53
|
+
console.error({
|
|
54
|
+
err,
|
|
55
|
+
msg: 'Error verifying Nodemailer transport.'
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Use ethereal.email to create a mock email account
|
|
61
|
+
*/ async function createMockAccount(emailConfig) {
|
|
62
|
+
try {
|
|
63
|
+
const etherealAccount = await nodemailer.createTestAccount();
|
|
64
|
+
const smtpOptions = {
|
|
65
|
+
...emailConfig || {},
|
|
66
|
+
auth: {
|
|
67
|
+
pass: etherealAccount.pass,
|
|
68
|
+
user: etherealAccount.user
|
|
69
|
+
},
|
|
70
|
+
fromAddress: emailConfig?.defaultFromAddress,
|
|
71
|
+
fromName: emailConfig?.defaultFromName,
|
|
72
|
+
host: 'smtp.ethereal.email',
|
|
73
|
+
port: 587,
|
|
74
|
+
secure: false
|
|
75
|
+
};
|
|
76
|
+
const transport = nodemailer.createTransport(smtpOptions);
|
|
77
|
+
const { pass, user, web } = etherealAccount;
|
|
78
|
+
console.info('E-mail configured with ethereal.email test account. ');
|
|
79
|
+
console.info(`Log into mock email provider at ${web}`);
|
|
80
|
+
console.info(`Mock email account username: ${user}`);
|
|
81
|
+
console.info(`Mock email account password: ${pass}`);
|
|
82
|
+
return transport;
|
|
83
|
+
} catch (err) {
|
|
84
|
+
if (err instanceof Error) {
|
|
85
|
+
console.error({
|
|
86
|
+
err,
|
|
87
|
+
msg: 'There was a problem setting up the mock email handler'
|
|
88
|
+
});
|
|
89
|
+
throw new InvalidConfiguration(`Unable to create Nodemailer test account. Error: ${err.message}`);
|
|
90
|
+
}
|
|
91
|
+
throw new InvalidConfiguration('Unable to create Nodemailer test account.');
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/* eslint-disable no-console */\nimport type { Transporter } from 'nodemailer'\nimport type SMTPConnection from 'nodemailer/lib/smtp-connection'\nimport type { EmailAdapter } from 'payload/config'\n\nimport nodemailer from 'nodemailer'\nimport { InvalidConfiguration } from 'payload/errors'\n\nexport type NodemailerAdapterArgs = {\n defaultFromAddress: string\n defaultFromName: string\n skipVerify?: boolean\n transport?: Transporter\n transportOptions?: SMTPConnection.Options\n}\n\nexport type NodemailerAdapter = EmailAdapter<unknown>\n\n/**\n * Creates an email adapter using nodemailer\n *\n * If no email configuration is provided, an ethereal email test account is returned\n */\nexport const nodemailerAdapter = async (\n args?: NodemailerAdapterArgs,\n): Promise<NodemailerAdapter> => {\n const { defaultFromAddress, defaultFromName, transport } = await buildEmail(args)\n\n const adapter: NodemailerAdapter = () => ({\n defaultFromAddress,\n defaultFromName,\n sendEmail: async (message) => {\n return await transport.sendMail({\n from: `${defaultFromName} <${defaultFromAddress}>`,\n ...message,\n })\n },\n })\n return adapter\n}\n\nasync function buildEmail(emailConfig?: NodemailerAdapterArgs): Promise<{\n defaultFromAddress: string\n defaultFromName: string\n transport: Transporter\n}> {\n if (!emailConfig) {\n const transport = await createMockAccount(emailConfig)\n if (!transport) throw new InvalidConfiguration('Unable to create Nodemailer test account.')\n\n return {\n defaultFromAddress: 'info@payloadcms.com',\n defaultFromName: 'Payload',\n transport,\n }\n }\n\n // Create or extract transport\n let transport: Transporter\n if ('transport' in emailConfig && emailConfig.transport) {\n ;({ transport } = emailConfig)\n } else if ('transportOptions' in emailConfig && emailConfig.transportOptions) {\n transport = nodemailer.createTransport(emailConfig.transportOptions)\n } else {\n transport = await createMockAccount(emailConfig)\n }\n\n if (emailConfig.skipVerify !== false) {\n await verifyTransport(transport)\n }\n\n return {\n defaultFromAddress: emailConfig.defaultFromAddress,\n defaultFromName: emailConfig.defaultFromName,\n transport,\n }\n}\n\nasync function verifyTransport(transport: Transporter) {\n try {\n await transport.verify()\n } catch (err: unknown) {\n console.error({ err, msg: 'Error verifying Nodemailer transport.' })\n }\n}\n\n/**\n * Use ethereal.email to create a mock email account\n */\nasync function createMockAccount(emailConfig?: NodemailerAdapterArgs) {\n try {\n const etherealAccount = await nodemailer.createTestAccount()\n\n const smtpOptions = {\n ...(emailConfig || {}),\n auth: {\n pass: etherealAccount.pass,\n user: etherealAccount.user,\n },\n fromAddress: emailConfig?.defaultFromAddress,\n fromName: emailConfig?.defaultFromName,\n host: 'smtp.ethereal.email',\n port: 587,\n secure: false,\n }\n const transport = nodemailer.createTransport(smtpOptions)\n const { pass, user, web } = etherealAccount\n\n console.info('E-mail configured with ethereal.email test account. ')\n console.info(`Log into mock email provider at ${web}`)\n console.info(`Mock email account username: ${user}`)\n console.info(`Mock email account password: ${pass}`)\n return transport\n } catch (err: unknown) {\n if (err instanceof Error) {\n console.error({ err, msg: 'There was a problem setting up the mock email handler' })\n throw new InvalidConfiguration(\n `Unable to create Nodemailer test account. Error: ${err.message}`,\n )\n }\n throw new InvalidConfiguration('Unable to create Nodemailer test account.')\n }\n}\n"],"names":["nodemailer","InvalidConfiguration","nodemailerAdapter","args","defaultFromAddress","defaultFromName","transport","buildEmail","adapter","sendEmail","message","sendMail","from","emailConfig","createMockAccount","transportOptions","createTransport","skipVerify","verifyTransport","verify","err","console","error","msg","etherealAccount","createTestAccount","smtpOptions","auth","pass","user","fromAddress","fromName","host","port","secure","web","info","Error"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,6BAA6B,GAK7B,OAAOA,gBAAgB,aAAY;AACnC,SAASC,oBAAoB,QAAQ,iBAAgB;AAYrD;;;;CAIC,GACD,OAAO,MAAMC,oBAAoB,OAC/BC;IAEA,MAAM,EAAEC,kBAAkB,EAAEC,eAAe,EAAEC,SAAS,EAAE,GAAG,MAAMC,WAAWJ;IAE5E,MAAMK,UAA6B,IAAO,CAAA;YACxCJ;YACAC;YACAI,WAAW,OAAOC;gBAChB,OAAO,MAAMJ,UAAUK,QAAQ,CAAC;oBAC9BC,MAAM,CAAC,EAAEP,gBAAgB,EAAE,EAAED,mBAAmB,CAAC,CAAC;oBAClD,GAAGM,OAAO;gBACZ;YACF;QACF,CAAA;IACA,OAAOF;AACT,EAAC;AAED,eAAeD,WAAWM,WAAmC;IAK3D,IAAI,CAACA,aAAa;QAChB,MAAMP,YAAY,MAAMQ,kBAAkBD;QAC1C,IAAI,CAACP,WAAW,MAAM,IAAIL,qBAAqB;QAE/C,OAAO;YACLG,oBAAoB;YACpBC,iBAAiB;YACjBC;QACF;IACF;IAEA,8BAA8B;IAC9B,IAAIA;IACJ,IAAI,eAAeO,eAAeA,YAAYP,SAAS,EAAE;QACrD,CAAA,EAAEA,SAAS,EAAE,GAAGO,WAAU;IAC9B,OAAO,IAAI,sBAAsBA,eAAeA,YAAYE,gBAAgB,EAAE;QAC5ET,YAAYN,WAAWgB,eAAe,CAACH,YAAYE,gBAAgB;IACrE,OAAO;QACLT,YAAY,MAAMQ,kBAAkBD;IACtC;IAEA,IAAIA,YAAYI,UAAU,KAAK,OAAO;QACpC,MAAMC,gBAAgBZ;IACxB;IAEA,OAAO;QACLF,oBAAoBS,YAAYT,kBAAkB;QAClDC,iBAAiBQ,YAAYR,eAAe;QAC5CC;IACF;AACF;AAEA,eAAeY,gBAAgBZ,SAAsB;IACnD,IAAI;QACF,MAAMA,UAAUa,MAAM;IACxB,EAAE,OAAOC,KAAc;QACrBC,QAAQC,KAAK,CAAC;YAAEF;YAAKG,KAAK;QAAwC;IACpE;AACF;AAEA;;CAEC,GACD,eAAeT,kBAAkBD,WAAmC;IAClE,IAAI;QACF,MAAMW,kBAAkB,MAAMxB,WAAWyB,iBAAiB;QAE1D,MAAMC,cAAc;YAClB,GAAIb,eAAe,CAAC,CAAC;YACrBc,MAAM;gBACJC,MAAMJ,gBAAgBI,IAAI;gBAC1BC,MAAML,gBAAgBK,IAAI;YAC5B;YACAC,aAAajB,aAAaT;YAC1B2B,UAAUlB,aAAaR;YACvB2B,MAAM;YACNC,MAAM;YACNC,QAAQ;QACV;QACA,MAAM5B,YAAYN,WAAWgB,eAAe,CAACU;QAC7C,MAAM,EAAEE,IAAI,EAAEC,IAAI,EAAEM,GAAG,EAAE,GAAGX;QAE5BH,QAAQe,IAAI,CAAC;QACbf,QAAQe,IAAI,CAAC,CAAC,gCAAgC,EAAED,IAAI,CAAC;QACrDd,QAAQe,IAAI,CAAC,CAAC,6BAA6B,EAAEP,KAAK,CAAC;QACnDR,QAAQe,IAAI,CAAC,CAAC,6BAA6B,EAAER,KAAK,CAAC;QACnD,OAAOtB;IACT,EAAE,OAAOc,KAAc;QACrB,IAAIA,eAAeiB,OAAO;YACxBhB,QAAQC,KAAK,CAAC;gBAAEF;gBAAKG,KAAK;YAAwD;YAClF,MAAM,IAAItB,qBACR,CAAC,iDAAiD,EAAEmB,IAAIV,OAAO,CAAC,CAAC;QAErE;QACA,MAAM,IAAIT,qBAAqB;IACjC;AACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@payloadcms/email-nodemailer",
|
|
3
|
+
"version": "3.0.0-alpha.62",
|
|
4
|
+
"description": "Payload Nodemailer Email Adapter",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/payloadcms/payload.git",
|
|
8
|
+
"directory": "packages/email-nodemailer"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"homepage": "https://payloadcms.com",
|
|
12
|
+
"author": "Payload CMS, Inc.",
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"nodemailer": "6.9.10"
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"payload": "3.0.0-alpha.62"
|
|
21
|
+
},
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"import": "./dist/index.js",
|
|
25
|
+
"require": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"registry": "https://registry.npmjs.org/"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=18.20.2"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist"
|
|
37
|
+
],
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/nodemailer": "6.4.14",
|
|
40
|
+
"payload": "3.0.0-alpha.62"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "pnpm build:swc && pnpm build:types",
|
|
44
|
+
"build:swc": "swc ./src -d ./dist --config-file .swcrc",
|
|
45
|
+
"build:types": "tsc --emitDeclarationOnly --outDir dist",
|
|
46
|
+
"build:clean": "find . \\( -type d \\( -name build -o -name dist -o -name .cache \\) -o -type f -name tsconfig.tsbuildinfo \\) -exec rm -rf {} + && pnpm build",
|
|
47
|
+
"clean": "rimraf {dist,*.tsbuildinfo}"
|
|
48
|
+
}
|
|
49
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
import type { Transporter } from 'nodemailer'
|
|
3
|
+
import type SMTPConnection from 'nodemailer/lib/smtp-connection'
|
|
4
|
+
import type { EmailAdapter } from 'payload/config'
|
|
5
|
+
|
|
6
|
+
import nodemailer from 'nodemailer'
|
|
7
|
+
import { InvalidConfiguration } from 'payload/errors'
|
|
8
|
+
|
|
9
|
+
export type NodemailerAdapterArgs = {
|
|
10
|
+
defaultFromAddress: string
|
|
11
|
+
defaultFromName: string
|
|
12
|
+
skipVerify?: boolean
|
|
13
|
+
transport?: Transporter
|
|
14
|
+
transportOptions?: SMTPConnection.Options
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type NodemailerAdapter = EmailAdapter<unknown>
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Creates an email adapter using nodemailer
|
|
21
|
+
*
|
|
22
|
+
* If no email configuration is provided, an ethereal email test account is returned
|
|
23
|
+
*/
|
|
24
|
+
export const nodemailerAdapter = async (
|
|
25
|
+
args?: NodemailerAdapterArgs,
|
|
26
|
+
): Promise<NodemailerAdapter> => {
|
|
27
|
+
const { defaultFromAddress, defaultFromName, transport } = await buildEmail(args)
|
|
28
|
+
|
|
29
|
+
const adapter: NodemailerAdapter = () => ({
|
|
30
|
+
defaultFromAddress,
|
|
31
|
+
defaultFromName,
|
|
32
|
+
sendEmail: async (message) => {
|
|
33
|
+
return await transport.sendMail({
|
|
34
|
+
from: `${defaultFromName} <${defaultFromAddress}>`,
|
|
35
|
+
...message,
|
|
36
|
+
})
|
|
37
|
+
},
|
|
38
|
+
})
|
|
39
|
+
return adapter
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function buildEmail(emailConfig?: NodemailerAdapterArgs): Promise<{
|
|
43
|
+
defaultFromAddress: string
|
|
44
|
+
defaultFromName: string
|
|
45
|
+
transport: Transporter
|
|
46
|
+
}> {
|
|
47
|
+
if (!emailConfig) {
|
|
48
|
+
const transport = await createMockAccount(emailConfig)
|
|
49
|
+
if (!transport) throw new InvalidConfiguration('Unable to create Nodemailer test account.')
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
defaultFromAddress: 'info@payloadcms.com',
|
|
53
|
+
defaultFromName: 'Payload',
|
|
54
|
+
transport,
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Create or extract transport
|
|
59
|
+
let transport: Transporter
|
|
60
|
+
if ('transport' in emailConfig && emailConfig.transport) {
|
|
61
|
+
;({ transport } = emailConfig)
|
|
62
|
+
} else if ('transportOptions' in emailConfig && emailConfig.transportOptions) {
|
|
63
|
+
transport = nodemailer.createTransport(emailConfig.transportOptions)
|
|
64
|
+
} else {
|
|
65
|
+
transport = await createMockAccount(emailConfig)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (emailConfig.skipVerify !== false) {
|
|
69
|
+
await verifyTransport(transport)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
defaultFromAddress: emailConfig.defaultFromAddress,
|
|
74
|
+
defaultFromName: emailConfig.defaultFromName,
|
|
75
|
+
transport,
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function verifyTransport(transport: Transporter) {
|
|
80
|
+
try {
|
|
81
|
+
await transport.verify()
|
|
82
|
+
} catch (err: unknown) {
|
|
83
|
+
console.error({ err, msg: 'Error verifying Nodemailer transport.' })
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Use ethereal.email to create a mock email account
|
|
89
|
+
*/
|
|
90
|
+
async function createMockAccount(emailConfig?: NodemailerAdapterArgs) {
|
|
91
|
+
try {
|
|
92
|
+
const etherealAccount = await nodemailer.createTestAccount()
|
|
93
|
+
|
|
94
|
+
const smtpOptions = {
|
|
95
|
+
...(emailConfig || {}),
|
|
96
|
+
auth: {
|
|
97
|
+
pass: etherealAccount.pass,
|
|
98
|
+
user: etherealAccount.user,
|
|
99
|
+
},
|
|
100
|
+
fromAddress: emailConfig?.defaultFromAddress,
|
|
101
|
+
fromName: emailConfig?.defaultFromName,
|
|
102
|
+
host: 'smtp.ethereal.email',
|
|
103
|
+
port: 587,
|
|
104
|
+
secure: false,
|
|
105
|
+
}
|
|
106
|
+
const transport = nodemailer.createTransport(smtpOptions)
|
|
107
|
+
const { pass, user, web } = etherealAccount
|
|
108
|
+
|
|
109
|
+
console.info('E-mail configured with ethereal.email test account. ')
|
|
110
|
+
console.info(`Log into mock email provider at ${web}`)
|
|
111
|
+
console.info(`Mock email account username: ${user}`)
|
|
112
|
+
console.info(`Mock email account password: ${pass}`)
|
|
113
|
+
return transport
|
|
114
|
+
} catch (err: unknown) {
|
|
115
|
+
if (err instanceof Error) {
|
|
116
|
+
console.error({ err, msg: 'There was a problem setting up the mock email handler' })
|
|
117
|
+
throw new InvalidConfiguration(
|
|
118
|
+
`Unable to create Nodemailer test account. Error: ${err.message}`,
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
throw new InvalidConfiguration('Unable to create Nodemailer test account.')
|
|
122
|
+
}
|
|
123
|
+
}
|