• Stars
    star
    129
  • Rank 278,447 (Top 6 %)
  • Language
    JavaScript
  • License
    Other
  • Created about 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

nuxt-mail

npm version Linux macOS Windows compatible Build status Coverage status Dependency status Renovate enabled
Open in Gitpod Buy Me a Coffee PayPal Patreon

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

Does not work for static sites (via nuxt generate) because the module creates a server route.

Install

# npm
$ npm install nuxt-mail

# Yarn
$ yarn add nuxt-mail

Configuration

Add the module to the modules array in your nuxt.config.js. Note to add it to modules instead of buildModules, otherwise the server route will not be generated.

// nuxt.config.js
export default {
  modules: [
    ['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,
    },
  },
}

The smtp options are required and directly passed to nodemailer. Refer to their documentation for available options. Also, you have to pass at least to, cc or bcc via the message config. This has security reasons, this way the client cannot send emails from your SMTP server to arbitrary recipients. You can actually preconfigure the message via the message config, so if you always want to send emails with the same subject or from address, you can configure them here.

The module injects the $mail variable, which we now use to send emails:

Nuxt 3

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>
export default {
  methods: {
    sendEmail() {
      this.$mail.send({
        from: 'John Doe',
        subject: 'Incredible',
        text: 'This is an incredible test message',
      })
    },
  },
}
</script>

Nuxt 2

For Nuxt 2, you need to install @nuxtjs/axios and add it to your module list before nuxt-mail:

// nuxt.config.js
export default {
  modules: [
    [
      '@nuxtjs/axios',
      ['nuxt-mail', { /* ... */ }],
    }],
  ],
}

Then you can use the injected variable like so:

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

Note about production use

When you use nuxt-mail in production and you configured a reverse proxy that hides your localhost behind a domain, you need to tell @nuxt/axios which base URL you are using. Otherwise nuxt-mail won't find the send route. Refer to @nuxt/axios options on how to do that. The easiest option is to set the API_URL environment variable, or set something else in your nuxt.config.js:

// nuxt.config.js
export default {
  axios: {
    baseURL: process.env.BASE_URL,
  },
}

Multiple message configs

It is also possible to provide multiple message configurations by changing the message config into an array.

// nuxt.config.js
export default {
  modules: [
    ['nuxt-mail', {
      message: [
        { name: 'contact', to: '[email protected]' },
        { name: 'support', to: '[email protected]' },
      ],
      ...
    }],
  ],
}

Then you can reference the config like this:

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

Or via index (in which case you do not need the name property):

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

Also, the module does not work for static sites (via nuxt generate) because the module creates a server route.

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:

// nuxt.config.js
export default {
  modules: [
    ['nuxt-mail', {
      smtp: {
        service: 'gmail',
        auth: {
          user: '[email protected]',
          pass: '<app-specific password>',
        },
      },
    }],
  ],
}

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

Debugging mail errors

If the mail doesn't get sent, you can debug the error 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, then find the "send" request. Open it and select the "Response" tab. There it should show the error message. In most cases, it is related to authentication with the SMTP server.

Open questions

"Self signed certificate in certificate chain" error

There is an issue where the above error is thrown. If someone knows a solution for this, it is warmly welcome 😍.

Contribute

Are you missing something or want to contribute? Feel free to file an issue or a pull request! βš™οΈ

Support

Hey, I am Sebastian Landwehr, a freelance web developer, and I love developing web apps and open source packages. If you want to support me so that I can keep packages up to date and build more helpful tools, you can donate here:

Buy Me a Coffee  If you want to send me a one time donation. The coffee is pretty good 😊.
PayPal Β Also for one time donations if you like PayPal.
Patreon Β Here you can support me regularly, which is great so I can steadily work on projects.

Thanks a lot for your support! ❀️

See also

  • nuxt-route-meta: Adds Nuxt page data to route meta at build time.
  • nuxt-modernizr: Adds a Modernizr build to your Nuxt.js app.
  • nuxt-mermaid-string: Embed a Mermaid diagram in a Nuxt.js app by providing its diagram string.
  • nuxt-content-git: Additional module for @nuxt/content that replaces or adds createdAt and updatedAt dates based on the git history.
  • nuxt-babel-runtime: Nuxt CLI that supports babel. Inspired by @nuxt/typescript-runtime.

License

MIT License Β© Sebastian Landwehr

More Repositories

1

nuxt-route-meta

Adds Nuxt page data to route meta at build time. Also supports TypeScript.
JavaScript
38
star
2

vue-mermaid-string

A Vue.js component that turns a Mermaid string into a diagram.
JavaScript
21
star
3

eslint-plugin-import-alias

An ESLint plugin that enforces the use of import aliases. Also supports autofixing.
JavaScript
14
star
4

nuxt-content-git

Additional module for @nuxt/content that replaces or adds createdAt and updatedAt dates based on the git history.
JavaScript
14
star
5

nuxt-modernizr

Adds a Modernizr build to your Nuxt.js app.
JavaScript
12
star
6

suppress-experimental-warnings

Filters experimental warnings from Node.js CLI output.
JavaScript
12
star
7

nuxt-content-body-html

Adds a property to each @nuxt/content document containing the raw HTML body, rendered from markdown.
JavaScript
8
star
8

mock-argv

Temporarily overrides the command line arguments. This is useful for testing.
JavaScript
6
star
9

expect-mocha-image-snapshot

A wrapper around jest-image-snapshot that makes it compatible to Mocha.
JavaScript
5
star
10

nuxt-mermaid-string

Embed a Mermaid diagram in a Nuxt.js app by providing its diagram string.
JavaScript
5
star
11

make-cli

Super easy declarative CLI framework with a single configuration object and a single function call.
JavaScript
5
star
12

nuxt-chargebee

Chargebee module for Nuxt.js that allows to checkout and open the self-service portal inside a Nuxt.js app.
JavaScript
5
star
13

output-files

Output a tree of files and directories by providing an object. Especially useful for testing with real files.
JavaScript
4
star
14

ts-ast-to-literal

Transform a TypeScript AST node into a real JS object.
JavaScript
4
star
15

with-local-tmp-dir

Creates a temporary folder inside cwd, cds inside the folder, runs a function, and removes the folder. Especially useful for testing.
JavaScript
4
star
16

base

Base package for projects.
JavaScript
3
star
17

nuxt-atomizer

Module that adds ACSS atomic CSS framework (aka Atomizer) support to a Nuxt.js app. Generates atomic classes from code.
JavaScript
3
star
18

github-repository-list-badges

Displays badges in the GitHub repository list of a user.
JavaScript
2
star
19

nuxt-content-hooks-git

nuxt-content hooks that replace or add createdAt and updatedAt dates based on the git history.
JavaScript
2
star
20

eslint-config

JavaScript
2
star
21

vue-icon

JavaScript
2
star
22

nuxt-dev-server

JavaScript
2
star
23

jest-image-matcher

A Jest matcher for image comparisons based on pixelmatch. Can also be used with Mocha. Useful for visual regression testing.
JavaScript
2
star
24

gh-repo-clone-all

Addition to GitHub CLI that clones all public and private repositories. It uses GitHub CLI and its authentication system and passes the filter options through to gh repo list.
JavaScript
2
star
25

pretest

JavaScript
1
star
26

tester-plugin-tmp-dir

JavaScript
1
star
27

mysqldump-search-replace-node

A node.js module that replaces a string in a MySQL dump and fixes serialized data.
JavaScript
1
star
28

package-name-regex

Provides a regex that can be used to test if a string is a valid NPM package name.
JavaScript
1
star
29

nuxt-firebase

JavaScript
1
star
30

github-latest-release

Shows the latest release version right next to the name on a GitHub repository page.
JavaScript
1
star
31

nuxt-express-server

JavaScript
1
star
32

accessibility-view

Accessibility View converts an arbitrary website into its accessibility relevant form.
JavaScript
1
star
33

depcheck-special-nuxt

Depcheck special that detects dependencies in nuxt modules.
JavaScript
1
star
34

firebase-functions-paddle

JavaScript
1
star