### Install nuxt-nodemailer and nodemailer Source: https://context7.com/kleinpetr/nuxt-nodemailer/llms.txt Install the module and its peer dependency using your preferred package manager. ```bash # pnpm pnpm add -D nuxt-nodemailer nodemailer # npm npm install --save-dev nuxt-nodemailer nodemailer # yarn yarn add --dev nuxt-nodemailer nodemailer ``` -------------------------------- ### Install nuxt-nodemailer with Package Managers Source: https://github.com/kleinpetr/nuxt-nodemailer/blob/main/README.md Install the nuxt-nodemailer module and nodemailer dependency using your preferred package manager. ```bash # Using ni ni -D nuxt-nodemailer nodemailer ``` ```bash # Using pnpm pnpm add -D nuxt-nodemailer nodemailer ``` ```bash # Using yarn yarn add --dev nuxt-nodemailer nodemailer ``` ```bash # Using npm npm install --save-dev nuxt-nodemailer nodemailer ``` -------------------------------- ### Send Email Using useNodeMailer() Composable Source: https://context7.com/kleinpetr/nuxt-nodemailer/llms.txt The `useNodeMailer()` composable is auto-imported in server event handlers. Use the `sendMail` helper to send emails with configured `from` address. ```typescript // server/api/contact.post.ts export default defineEventHandler(async (event) => { const body = await readBody(event) const { sendMail } = useNodeMailer() try { const result = await sendMail({ to: 'admin@myapp.com', subject: `Contact form: ${body.subject}`, text: body.message, html: `
${body.message}
`, }) return { success: true, messageId: result.messageId } } catch (error) { throw createError({ statusCode: 500, message: 'Failed to send email' }) } }) // Expected response: { success: true, messageId: 'Your account is ready.
', attachments: [ { filename: 'welcome.pdf', path: '/app/assets/welcome.pdf', }, ], }) // result.messageId is the SMTP message ID assigned by the mail server console.log('Sent:', result.messageId) return { messageId: result.messageId } }) ``` -------------------------------- ### Send Email Using Raw Transporter Instance Source: https://context7.com/kleinpetr/nuxt-nodemailer/llms.txt Use the raw Nodemailer `Transporter` instance for full control over mail options, including custom `from` addresses per message or advanced Nodemailer features like DKIM signing. ```typescript // server/api/notify.ts export default defineEventHandler(async () => { const { transport } = useNodeMailer() // Verify SMTP connection is alive await transport.verify() // Use transport directly with an explicit `from` override const result = await transport.sendMail({ from: '"Billing Team"${body.message}
`, }) return { success: true, messageId: result.messageId } } catch (error) { throw createError({ statusCode: 500, message: 'Failed to send email' }) } }) ``` ### Returns - `sendMail` (function): A function to send emails, automatically inheriting the configured `from` address. - `transport` (object): The raw Nodemailer `Transporter` instance for advanced use cases. - `nodemailer` (object): The Nodemailer configuration object. ``` -------------------------------- ### Define Placeholder Keys for Environment Variable Overrides Source: https://context7.com/kleinpetr/nuxt-nodemailer/llms.txt Ensure placeholder keys exist in `nuxt.config.ts` for environment variables to override them effectively. ```typescript // nuxt.config.ts — define placeholder keys so env vars can override them export default defineNuxtConfig({ modules: ['nuxt-nodemailer'], nodemailer: { from: '', // overridden by NUXT_NODEMAILER_FROM host: '', // overridden by NUXT_NODEMAILER_HOST port: 587, // overridden by NUXT_NODEMAILER_PORT auth: { user: '', // overridden by NUXT_NODEMAILER_AUTH_USER pass: '', // overridden by NUXT_NODEMAILER_AUTH_PASS }, }, }) ``` -------------------------------- ### Create Custom Transport with Nodemailer Library Source: https://context7.com/kleinpetr/nuxt-nodemailer/llms.txt Access the raw SMTP config object from Nuxt runtime config to build a new Nodemailer transport with custom settings, such as pooling and max connections, using the `nodemailer` npm package directly. ```typescript // server/api/custom-transport.ts import nodemailerLib from 'nodemailer' export default defineEventHandler(async () => { const { nodemailer: config } = useNodeMailer() // Build a new transport using the base config plus overrides const customTransport = nodemailerLib.createTransport({ ...config, pool: true, // use pooled SMTP connections maxConnections: 5, }) const result = await customTransport.sendMail({ from: config.from, to: 'bulk@example.com', subject: 'Bulk message', text: 'Sent via pooled transport', }) return { messageId: result.messageId } }) ``` -------------------------------- ### Send Email Using useNodeMailer Composable Source: https://github.com/kleinpetr/nuxt-nodemailer/blob/main/README.md Use the useNodeMailer composable within a server event handler to send emails. The sendMail function automatically inherits the 'from' argument from the module's configuration. ```typescript export default defineEventHandler(() => { const { sendMail } = useNodeMailer() return sendMail({ subject: 'Nuxt + nodemailer', text: 'Hello from nuxt-nodemailer!', to: 'john@doe.com' }) }) ``` -------------------------------- ### Configure Nuxt Nodemailer Module Source: https://github.com/kleinpetr/nuxt-nodemailer/blob/main/README.md Configure the nuxt-nodemailer module in your nuxt.config.js. The configuration options mirror Nodemailer's SMTP options. Sensitive information should be managed via environment variables. ```typescript export default { modules: [ 'nuxt-nodemailer' ], nodemailer: { from: '"John Doe"Your account is ready.
', attachments: [ { filename: 'welcome.pdf', path: '/app/assets/welcome.pdf', }, ], }) // result.messageId is the SMTP message ID assigned by the mail server console.log('Sent:', result.messageId) return { messageId: result.messageId } }) ``` ### Response Example ```json { "messageId": "