diff --git a/.gitignore b/.gitignore index 03d0bf1..23e6f73 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /coverage /dist /node_modules +.idea diff --git a/README.md b/README.md index 54aeee1..cc9df47 100644 --- a/README.md +++ b/README.md @@ -79,10 +79,17 @@ export default { message: { to: 'foo@bar.de', }, + transport: 'smtp', // smtp | sendmail smtp: { host: "smtp.example.com", port: 587, }, + sendmail: { + sendmail: true, + newline: 'unix', + path: '/usr/sbin/sendmail', + secure: true, + }, }], ], // or use the top-level option: @@ -90,10 +97,17 @@ export default { message: { to: 'foo@bar.de', }, + transport: 'smtp', // smtp | sendmail smtp: { host: "smtp.example.com", port: 587, }, + sendmail: { + sendmail: true, + newline: 'unix', + path: '/usr/sbin/sendmail', + secure: true, + }, }, // or use runtimeConfig runtimeConfig: { @@ -101,10 +115,17 @@ export default { message: { to: 'foo@bar.de', }, + transport: 'smtp', // smtp | sendmail smtp: { host: "smtp.example.com", port: 587, }, + sendmail: { + sendmail: true, + newline: 'unix', + path: '/usr/sbin/sendmail', + secure: true, + }, }, }, } diff --git a/src/index.js b/src/index.js index 56f98f8..8a9d53d 100644 --- a/src/index.js +++ b/src/index.js @@ -34,15 +34,20 @@ export default function (moduleOptions, nuxt) { nuxt.options[isNuxt3 ? 'runtimeConfig' : 'privateRuntimeConfig']; const options = { + transport: 'smtp', ...runtimeConfig.mail, ...nuxt.options.mail, ...moduleOptions, }; - if (!options.smtp) { + if (options.transport === 'smtp' && !options.smtp) { throw new Error('SMTP config is missing.'); } + if (options.transport === 'sendmail' && !options.sendmail) { + throw new Error('Sendmail config is missing.'); + } + if ( (Array.isArray(options.message) && options.message.length === 0) || !options.message diff --git a/src/index.spec.js b/src/index.spec.js index 39c6618..6fb5c26 100644 --- a/src/index.spec.js +++ b/src/index.spec.js @@ -282,6 +282,65 @@ export default { await kill(nuxt.pid); } }, + async 'config by index sendmail'() { + /* + * To pass this test you need to set up the Postfix locally and configure the relay host to the test mailServer + * in the /etc/postfix/main.cf + * ... + * relayhost = [127.0.0.1]:3001 + * ... + * + */ + await outputFiles({ + 'nuxt.config.js': endent` + export default { + modules: [ + ['../src/index.js', { + message: [{ to: 'foo@bar.com' }, { to: 'johndoe@gmail.com' }], + transport: 'sendmail', + smtp: { port: 3001 }, + sendmail: { sendmail: true, newline: 'unix', path: '/usr/sbin/sendmail', secure: true }, + }], + ], + } + `, + 'pages/index.vue': endent` + + + + `, + }); + + const port = await getPort(); + const nuxt = execaCommand('nuxt dev', { env: { PORT: port } }); + + try { + await nuxtDevReady(port); + + const [capture] = await Promise.all([ + this.mailServer.captureOne('johndoe@gmail.com'), + this.page.goto(`http://localhost:${port}`), + ]); + + expect(capture.email.body).toEqual('This is an incredible test message'); + expect(capture.email.headers.subject).toEqual('Incredible'); + expect(capture.email.headers.from).toEqual('a@b.de'); + expect(capture.email.headers.to).toEqual('johndoe@gmail.com'); + } finally { + await kill(nuxt.pid); + } + }, async 'config by name'() { await outputFiles({ 'nuxt.config.js': endent` diff --git a/src/server-handler.post.js b/src/server-handler.post.js index 60051cc..f204a3b 100644 --- a/src/server-handler.post.js +++ b/src/server-handler.post.js @@ -4,7 +4,8 @@ import nodemailer from 'nodemailer'; import options from '#mail/options.mjs'; import send from '#mail/send.mjs'; -const transport = nodemailer.createTransport(options.smtp); +const transportType = options.transport || 'smtp'; +const transport = nodemailer.createTransport(options[transportType]); export default defineEventHandler(async event => { try {