### 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: '' } ``` -------------------------------- ### Configure Module in nuxt.config.ts Source: https://context7.com/kleinpetr/nuxt-nodemailer/llms.txt Register the module and provide SMTP options. The `from` field is automatically applied to all `sendMail()` calls. ```typescript // nuxt.config.ts export default defineNuxtConfig({ modules: ['nuxt-nodemailer'], nodemailer: { from: '"My App" ', host: 'smtp.mailtrap.io', port: 465, secure: true, auth: { user: 'smtp_user', pass: 'smtp_password', }, }, }) ``` -------------------------------- ### Send HTML Email with Attachment Source: https://context7.com/kleinpetr/nuxt-nodemailer/llms.txt The `sendMail` helper accepts standard Nodemailer mail options, including attachments. The `from` address is automatically included. ```typescript // server/api/send-welcome.ts export default defineEventHandler(async (event) => { const { sendMail } = useNodeMailer() // Send HTML email with attachment — `from` is injected automatically const result = await sendMail({ to: 'newuser@example.com', cc: 'manager@myapp.com', subject: 'Welcome to MyApp!', html: '

Welcome!

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" ', to: 'customer@example.com', subject: 'Invoice #1042', text: 'Please find your invoice attached.', }) return { messageId: result.messageId } }) ``` -------------------------------- ### useNodeMailer() Composable Source: https://context7.com/kleinpetr/nuxt-nodemailer/llms.txt The `useNodeMailer()` composable is auto-imported into Nuxt server event handlers. It provides access to `sendMail()`, the raw `transport` object, and the `nodemailer` configuration. ```APIDOC ## useNodeMailer() ### Description Provides access to email sending functionalities within Nuxt server event handlers. ### Usage ```javascript // 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' }) } }) ``` ### 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" ', host: 'smtp.mailtrap.io', port: 465, secure: true, auth: { user: 'john@doe.com', pass: '', }, }, } ``` -------------------------------- ### Overwrite Nodemailer Configuration with Environment Variables Source: https://github.com/kleinpetr/nuxt-nodemailer/blob/main/README.md Environment variables can be used to override specific Nodemailer configuration options. Prefix the variable name with NUXT_NODEMAILER_ and use uppercase. ```bash NUXT_NODEMAILER_AUTH_PASS=yourpassword NUXT_NODEMAILER_FROM="..." ``` -------------------------------- ### Create New Transport or Use Existing with Nodemailer Source: https://github.com/kleinpetr/nuxt-nodemailer/blob/main/README.md Access the nodemailer instance and transport object via useNodeMailer. This allows for creating new transports or using the pre-configured existing transport to send emails. ```typescript export default defineEventHandler(() => { const { transport, nodemailer } = useNodeMailer() // you can create a new transport return nodemailer.createTransport(...) // or use the existing one return transport.sendMail(...) }) ``` -------------------------------- ### sendMail(options) Function Source: https://context7.com/kleinpetr/nuxt-nodemailer/llms.txt A convenience function that wraps `transport.sendMail()`. It automatically includes the `from` address specified in the module configuration and accepts standard Nodemailer mail options. ```APIDOC ## sendMail(options) ### Description Sends an email using the configured Nodemailer transport. The `from` address is automatically prepended. ### Parameters - **options** (object) - Required - Any [Nodemailer mail options](https://nodemailer.com/message/) including `to`, `subject`, `text`, `html`, `attachments`, `cc`, `bcc`, etc. ### Returns - `Promise`: A Promise that resolves to Nodemailer's `SentMessageInfo` object, which includes the `messageId`. ### Usage ```javascript // server/api/send-welcome.ts export default defineEventHandler(async (event) => { const { sendMail } = useNodeMailer() // Send HTML email with attachment — `from` is injected automatically const result = await sendMail({ to: 'newuser@example.com', cc: 'manager@myapp.com', subject: 'Welcome to MyApp!', html: '

Welcome!

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": "" } ``` ``` -------------------------------- ### Override Configuration with Environment Variables Source: https://context7.com/kleinpetr/nuxt-nodemailer/llms.txt Environment variables prefixed with `NUXT_NODEMAILER_` can override configuration keys defined in `nuxt.config.ts`. Nested keys use underscores. ```bash # .env NUXT_NODEMAILER_FROM="My App " NUXT_NODEMAILER_AUTH_PASS=supersecretpassword NUXT_NODEMAILER_HOST=smtp.sendgrid.net NUXT_NODEMAILER_PORT=587 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.