npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@goede/nuxt-mail

v1.0.2

Published

Adds email sending capability to a Nuxt.js app. Adds a server route, an injected variable, and uses nodemailer to send emails.

Downloads

208

Readme

✉️ Nuxt Mail

This a full rewrite of nuxt-mail using typescript, @nuxt/module-builder and supports Nuxt >=3.0.0 only.

  • 📨 Send emails using useNuxtApp().$mail or the useMail() composable
  • ⚙️ Generated types based on configuration for type inference
  • 📫 Uses nodemailer from within a server route

[!WARNING] This module does not work for statically generated sites (SSG) as it relies on server routes to communicate with the SMTP server.

Install

$ npx nuxi module add @goede/nuxt-mail

Configuration

Add the module to the modules array in your nuxt.config.ts.

// nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    [
      "@goede/nuxt-mail",
      {
        message: {
          to: "[email protected]",
        },
        smtp: {
          host: "smtp.example.com",
          port: 587,
        },
      },
    ],
  ],
  // or use the top-level option:
  mail: {
    message: {
      to: "[email protected]",
    },
    smtp: {
      host: "smtp.example.com",
      port: 587,
    },
  },
});

[!WARNING] For security reasons a message configuration is required to set a to, cc or bcc property. This prevents sending out mails to arbitrary recipients from the client-side, only to those preconfigured.

The smtp options are required and directly passed to nodemailer. Refer to their documentation for available options.

Besides setting the recipient fields, you can preconfigure other message fields in the message config to send emails with the common values (such as subject, from, etc.).

Usage

Via composable

<script setup>
  const mail = useMail();

  mail.send({
    from: "John Doe",
    subject: "Incredible",
    text: "This is an incredible test message",
  });
</script>

Via injected variable

<script setup>
  const { $mail } = useNuxtApp();

  $mail.send({
    from: "John Doe",
    subject: "Incredible",
    text: "This is an incredible test message",
  });
</script>

Via Options API

<script lang="ts">
  export default defineNuxtcomponent({
    methods: {
      sendEmail() {
        this.$mail.send({
          from: "John Doe",
          subject: "Incredible",
          text: "This is an incredible test message",
        });
      },
    },
  });
</script>

Multiple message configs

Multiple message configurations can be set by changing the message config into an array.

// nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    [
      "@goede/nuxt-mail",
      {
        message: [
          { name: "contact", to: "[email protected]" },
          { name: "support", to: "[email protected]" },
        ],
        // ...
      },
    ],
  ],
});

These configurations can be used by passing config property that corresponds with the config name. These names will autocomplete using types generated on startup.

mail.send({
  config: "support",
  from: "John Doe",
  subject: "Incredible",
  text: "This is an incredible test message",
});

For legacy purposes we support passing the config index instead of the config name.

[!NOTE] This will be removed in v2 as the config name is now typesafe.

mail.send({
  config: 1, // Resolves to 'support'
  from: "John Doe",
  subject: "Incredible",
  text: "This is an incredible test message",
});

Setting up popular email services

Gmail

You have to setup an app-specific password to log into the SMTP server. Then, add the following config to your nuxt-mail config. Looks like there are multiple ways to configure Gmail, so it's best to try out the options:

// nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    [
      "@goede/nuxt-mail",
      {
        smtp: {
          service: "gmail",
          auth: {
            user: "[email protected]",
            pass: "<app-specific password>",
          },
        },
      },
    ],
  ],
});
// nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    [
      "nuxt-mail",
      {
        smtp: {
          host: "smtp.gmail.com",
          port: 587,
          auth: {
            user: "[email protected]",
            pass: "<app-specific password>",
          },
        },
      },
    ],
  ],
});

Missing something? Add your service here via a pull request.

Debugging mail errors

You can debug errors using the browser developer tools, if a 500 error is thrown (check out the console output), you can find the error message in the Network tab. For Chrome users, open the Network tab and look for the "send" request. Open it and select the "Response" tab, this should show the error message, in most cases the error is related to authentication with the SMTP server.

Open questions

"Self signed certificate in certificate chain" error

There is an open issue where the above error is thrown, if you know what could be causing this or have a solution for this please join the issue discussion and let us know!

Contribute

Are you missing something or want to contribute? Feel free to file an issue or a pull request! ⚙️

License

MIT License © Bobbie Goede & Sebastian Landwehr