Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/coverage
/dist
/node_modules
.idea
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,53 @@ export default {
message: {
to: '[email protected]',
},
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:
mail: {
message: {
to: '[email protected]',
},
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: {
mail: {
message: {
to: '[email protected]',
},
transport: 'smtp', // smtp | sendmail
smtp: {
host: "smtp.example.com",
port: 587,
},
sendmail: {
sendmail: true,
newline: 'unix',
path: '/usr/sbin/sendmail',
secure: true,
},
},
},
}
Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 59 additions & 0 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]' }, { to: '[email protected]' }],
transport: 'sendmail',
smtp: { port: 3001 },
sendmail: { sendmail: true, newline: 'unix', path: '/usr/sbin/sendmail', secure: true },
}],
],
}
`,
'pages/index.vue': endent`
<template>
<div />
</template>

<script setup>
const mail = useMail()

await mail.send({
from: '[email protected]',
subject: 'Incredible',
text: 'This is an incredible test message',
config: 1,
})
</script>
`,
});

const port = await getPort();
const nuxt = execaCommand('nuxt dev', { env: { PORT: port } });

try {
await nuxtDevReady(port);

const [capture] = await Promise.all([
this.mailServer.captureOne('[email protected]'),
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('[email protected]');
expect(capture.email.headers.to).toEqual('[email protected]');
} finally {
await kill(nuxt.pid);
}
},
async 'config by name'() {
await outputFiles({
'nuxt.config.js': endent`
Expand Down
3 changes: 2 additions & 1 deletion src/server-handler.post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down