nuxt-mail

repository·master·Indexed 43 days ago

https://github.com/dword-design/nuxt-mail

A Nuxt.js module that adds email sending capabilities using nodemailer. It provides a server route, an injected variable, and supports both Composition API via the useMail() composable and the Options API via this.$mail. Version 8.0.5.

Tokens
1.9K
Snippets
9
Records
12
Agent score
56%

What's inside nuxt-mail

  1. Use multiple message configurations

    master

    You can define multiple presets by providing an array to the message configuration. Each object in the array can have a name property. You can then trigger a specific configuration using the config key in the .send() method, either by its name or its index in the array.

    // nuxt.config.ts
    export default defineNuxtConfig({
      modules: [
        ['nuxt-mail', {
          message: [
            { name: 'contact', to: 'contact@foo.de' },
            { name: 'support', to: 'support@foo.de' },
          ],
          // ... smtp config
        }],
      ],
    });
    
    // Usage in component
    mail.send({
      config: 'support', // or config: 1
      from: 'John Doe',
      subject: 'Incredible',
      text: 'This is an incredible test message',
    });
  2. Configure Gmail SMTP

    master

    To use Gmail, you must set up an app-specific password. You can configure it using the service: 'gmail' option or by manually specifying the host and port.

    // Option 1: Using service name
    export default defineNuxtConfig({
      modules: [
        ['nuxt-mail', {
          smtp: {
            service: 'gmail',
            auth: {
              user: 'foo@gmail.com',
              pass: '<app-specific password>',
            },
          },
        }],
      ],
    });
    
    // Option 2: Manual host/port
    export default defineNuxtConfig({
      modules: [
        ['nuxt-mail', {
          smtp: {
            host: "smtp.gmail.com",
            port: 587,
            auth: {
              user: 'foo@gmail.com',
              pass: '<app-specific password>',
            },
          },
        }],
      ],
    });
  3. Configure nuxt-mail

    master

    Configure the module in your nuxt.config.ts by adding it to the modules array or using the top-level mail option.

    Important Requirements:

    • The smtp options are required and passed directly to nodemailer.
    • You must provide at least to, cc, or bcc via the message config to prevent clients from sending emails to arbitrary recipients.

    You can also use environment variables like NUXT_MAIL, NUXT_MAIL_SMTP, or NUXT_MAIL_MESSAGE as JSON strings; they will be deserialized into the runtime config automatically.

    // nuxt.config.ts
    export default defineNuxtConfig({
      modules: [
        ['nuxt-mail', {
          message: {
            to: 'foo@bar.de',
          },
          smtp: {
            host: "smtp.example.com",
            port: 587,
          },
        }],
      ],
      // or use the top-level option:
      mail: {
        message: {
          to: 'foo@bar.de',
        },
        smtp: {
          host: "smtp.example.com",
          port: 587,
        },
      },
    });
  4. Debug mail errors

    master

    If an email fails to send, check the browser developer tools. If a 500 error occurs, inspect the Network tab:

    1. Find the send request.
    2. Open the request and select the Response tab.
    3. The error message (often related to SMTP authentication) will be displayed there.
  5. Send emails via Options API

    master

    In the Nuxt Options API, the mail instance is available via this.$mail.

    <script>
    export default {
      methods: {
        sendEmail() {
          this.$mail.send({
            from: 'John Doe',
            subject: 'Incredible',
            text: 'This is an incredible test message',
          });
        },
      },
    };
    </script>
  6. Send emails via useMail composable

    master

    In the Nuxt 3 Composition API (<script setup>), use the useMail() composable to access the mail instance and call .send().

    <script setup>
    const mail = useMail();
    
    mail.send({
      from: 'John Doe',
      subject: 'Incredible',
      text: 'This is an incredible test message',
    });
    </script>
  7. Use MailOptionsInput for flexible configuration

    master

    The MailOptionsInput interface allows for more flexible input when configuring mail, specifically allowing a single Message instead of an array of messages.

    • message: Accepts either a single Message or an array of Message[].
    • smtp: An SMTPTransport.Options object.
  8. Access the mail instance with useMail()

    master

    In a Nuxt application, you can access the configured mail instance using the useMail() composable. This provides access to the $mail property injected into the Nuxt app instance by the nuxt-mail module.

    import { useMail } from '#imports';
    
    const mail = useMail();
    // Use the mail instance to send emails
  9. Configure messages with specific priority/config

    master

    The MessageWithConfig interface extends the standard SendMailOptions (excluding recipient fields) by adding a config property, which is a number.

    When providing input via MessageWithConfigInput, the config property can be either a number or a string.

  10. Configure MailOptions for the Nuxt module

    master

    The MailOptions interface defines the structure for sending mail via the module. It consists of an array of Message objects and an smtp configuration object.

    • message: An array of Message objects (Message[]).
    • smtp: An SMTPTransport.Options object or null.
    const options: MailOptions = {
      message: [
        { to: 'recipient@example.com', subject: 'Subject', text: 'Body' }
      ],
      smtp: {
        host: 'smtp.example.com',
        port: 587,
        secure: false,
        auth: { user: 'user', pass: 'pass' }
      }
    };
  11. Define mail messages and recipients

    master

    A Message represents an individual email to be sent. It is based on SendMailOptions from nodemailer and requires at least one recipient field: to, cc, or bcc. You can also optionally provide a name property.

    Required fields (at least one):

    • to
    • cc
    • bcc
    // Example of a Message object
    const message: Message = {
      to: 'user@example.com',
      subject: 'Hello',
      text: 'This is a test message'
    };