nuxt-mail 3.0.23 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -67,13 +67,14 @@ $ yarn add nuxt-mail
67
67
  ```
68
68
  <!-- /INSTALL -->
69
69
 
70
- ## Usage
70
+ ## Configuration
71
+
72
+ 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.
71
73
 
72
- 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. We also have to install the [@nuxtjs/axios](https://www.npmjs.com/package/@nuxtjs/axios) module because it is used internally to call the server route:
73
74
  ```js
75
+ // nuxt.config.js
74
76
  export default {
75
77
  modules: [
76
- '@nuxtjs/axios',
77
78
  ['nuxt-mail', {
78
79
  message: {
79
80
  to: 'foo@bar.de',
@@ -101,36 +102,109 @@ The `smtp` options are required and directly passed to [nodemailer](https://node
101
102
 
102
103
  The module injects the `$mail` variable, which we now use to send emails:
103
104
 
104
- ```js
105
- // Inside a component
106
- this.$mail.send({
105
+ ## Nuxt 3
106
+
107
+ ### Via composable
108
+
109
+ ```html
110
+ <script setup>
111
+ const mail = useMail()
112
+
113
+ mail.send({
107
114
  from: 'John Doe',
108
115
  subject: 'Incredible',
109
116
  text: 'This is an incredible test message',
110
117
  })
118
+ </script>
111
119
  ```
112
120
 
113
- You can also directly call the generated `/mail/send` post route:
121
+ ### Via injected variable
114
122
 
115
- ```js
116
- // Inside a component
117
- this.$axios.$post('/mail/send', {
123
+ ```html
124
+ <script setup>
125
+ const { $mail } = useNuxtApp()
126
+
127
+ $mail.send({
118
128
  from: 'John Doe',
119
129
  subject: 'Incredible',
120
130
  text: 'This is an incredible test message',
121
131
  })
132
+ </script>
133
+ ```
134
+
135
+ ### Via Options API
136
+
137
+ ```html
138
+ <script>
139
+ export default {
140
+ methods: {
141
+ sendEmail() {
142
+ this.$mail.send({
143
+ from: 'John Doe',
144
+ subject: 'Incredible',
145
+ text: 'This is an incredible test message',
146
+ })
147
+ },
148
+ },
149
+ }
150
+ </script>
122
151
  ```
123
152
 
124
- Note that the data are passed to [nodemailer](https://nodemailer.com/message/). Refer to the documentation for available config options.
153
+ ## Nuxt 2
154
+
155
+ For Nuxt 2, you need to install [@nuxtjs/axios](https://www.npmjs.com/package/@nuxtjs/axios) and add it to your module list before `nuxt-mail`:
156
+
157
+ ```js
158
+ // nuxt.config.js
159
+ export default {
160
+ modules: [
161
+ [
162
+ '@nuxtjs/axios',
163
+ ['nuxt-mail', { /* ... */ }],
164
+ }],
165
+ ],
166
+ }
167
+ ```
168
+
169
+ Then you can use the injected variable like so:
170
+
171
+ ```html
172
+ <script>
173
+ export default {
174
+ methods: {
175
+ sendEmail() {
176
+ this.$mail.send({
177
+ from: 'John Doe',
178
+ subject: 'Incredible',
179
+ text: 'This is an incredible test message',
180
+ })
181
+ },
182
+ },
183
+ }
184
+ </script>
185
+ ```
186
+
187
+ ### Note about production use
188
+
189
+ 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](https://axios.nuxtjs.org/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`:
190
+
191
+ ```js
192
+ // nuxt.config.js
193
+ export default {
194
+ axios: {
195
+ baseURL: process.env.BASE_URL,
196
+ },
197
+ }
198
+ ```
125
199
 
126
200
  ## Multiple message configs
127
201
 
128
202
  It is also possible to provide multiple message configurations by changing the `message` config into an array.
129
203
 
130
204
  ```js
205
+ // nuxt.config.js
131
206
  export default {
132
207
  modules: [
133
- '@nuxtjs/axios',
134
208
  ['nuxt-mail', {
135
209
  message: [
136
210
  { name: 'contact', to: 'contact@foo.de' },
@@ -145,7 +219,7 @@ export default {
145
219
  Then you can reference the config like this:
146
220
 
147
221
  ```js
148
- this.$axios.$post('/mail/send', {
222
+ mail.send({
149
223
  config: 'support',
150
224
  from: 'John Doe',
151
225
  subject: 'Incredible',
@@ -156,7 +230,7 @@ this.$axios.$post('/mail/send', {
156
230
  Or via index (in which case you do not need the `name` property):
157
231
 
158
232
  ```js
159
- this.$axios.$post('/mail/send', {
233
+ mail.send({
160
234
  config: 1, // Resolves to 'support'
161
235
  from: 'John Doe',
162
236
  subject: 'Incredible',
@@ -164,20 +238,6 @@ this.$axios.$post('/mail/send', {
164
238
  })
165
239
  ```
166
240
 
167
- ## Note about production use
168
-
169
- 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](https://axios.nuxtjs.org/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`:
170
-
171
- ```js
172
- // nuxt.config.js
173
-
174
- export default {
175
- axios: {
176
- baseURL: process.env.BASE_URL,
177
- },
178
- }
179
- ```
180
-
181
241
  Also, the module does not work for static sites (via `nuxt generate`) because the module creates a server route.
182
242
 
183
243
  ## Setting up popular email services
@@ -187,11 +247,10 @@ Also, the module does not work for static sites (via `nuxt generate`) because th
187
247
  You have to setup an [app-specific password](https://myaccount.google.com/apppasswords) to log into the SMTP server. Then, add the following config to your `nuxt-mail` config:
188
248
 
189
249
  ```js
250
+ // nuxt.config.js
190
251
  export default {
191
252
  modules: [
192
- '@nuxtjs/axios',
193
253
  ['nuxt-mail', {
194
- // ...
195
254
  smtp: {
196
255
  service: 'gmail',
197
256
  auth: {
@@ -208,7 +267,7 @@ Missing something? Add your service here via a [pull request](https://github.com
208
267
 
209
268
  ## Debugging mail errors
210
269
 
211
- If the mail doesn't get sent, you can debug the error using the browser developer tools. If a `400` 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.
270
+ 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.
212
271
 
213
272
  ## Open questions
214
273
 
@@ -0,0 +1,2 @@
1
+ import { useNuxtApp } from '#app';
2
+ export const useMail = () => useNuxtApp().$mail;
package/dist/index.js CHANGED
@@ -1,89 +1,75 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = _default;
7
-
8
- var _findIndex = _interopRequireDefault(require("@dword-design/functions/dist/find-index"));
9
-
10
- var _omit = _interopRequireDefault(require("@dword-design/functions/dist/omit"));
11
-
12
- var _some = _interopRequireDefault(require("@dword-design/functions/dist/some"));
13
-
14
- var _express = _interopRequireDefault(require("express"));
15
-
16
- var _nodemailer = _interopRequireDefault(require("nodemailer"));
17
-
18
- var _nuxtPushPlugins = _interopRequireDefault(require("nuxt-push-plugins"));
19
-
20
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
-
22
- function _default(moduleOptions) {
23
- var _options$message;
24
-
25
- const options = { ...this.options.mail,
1
+ import some from "@dword-design/functions/dist/some.js";
2
+ import { addImports, addServerHandler, addTemplate, createResolver, isNuxt3 as isNuxt3Try } from '@nuxt/kit';
3
+ import express from 'express';
4
+ import fs from 'fs-extra';
5
+ import nodemailer from 'nodemailer';
6
+ import nuxtPushPlugins from 'nuxt-push-plugins';
7
+ import parsePackagejsonName from 'parse-packagejson-name';
8
+ import P from 'path';
9
+ import send from "./send.js";
10
+ const resolver = createResolver(import.meta.url);
11
+ const packageConfig = fs.readJsonSync(resolver.resolve('../package.json'));
12
+ const moduleName = parsePackagejsonName(packageConfig.name).fullName;
13
+ export default function (moduleOptions, nuxt) {
14
+ nuxt = nuxt || this;
15
+ const options = {
16
+ ...nuxt.options.mail,
26
17
  ...moduleOptions
27
18
  };
28
-
29
19
  if (!options.smtp) {
30
20
  throw new Error('SMTP config is missing.');
31
21
  }
32
-
33
22
  if (Array.isArray(options.message) && options.message.length === 0 || !options.message) {
34
23
  throw new Error('You have to provide at least one config.');
35
24
  }
36
-
37
25
  if (!Array.isArray(options.message)) {
38
26
  options.message = [options.message];
39
27
  }
40
-
41
- if (_options$message = options.message, (0, _some.default)(c => !c.to && !c.cc && !c.bcc)(_options$message)) {
28
+ if (some(c => !c.to && !c.cc && !c.bcc)(options.message)) {
42
29
  throw new Error('You have to provide to/cc/bcc in all configs.');
43
30
  }
44
-
45
- const app = (0, _express.default)();
46
-
47
- const transport = _nodemailer.default.createTransport(options.smtp);
48
-
49
- app.use(_express.default.json());
50
- app.post('/send', async (req, res) => {
51
- req.body = {
52
- config: 0,
53
- ...req.body
54
- };
55
-
56
- try {
57
- var _req$body, _options$message$req$;
58
-
59
- if (typeof req.body.config === 'string') {
60
- var _options$message2;
61
-
62
- const configIndex = (_options$message2 = options.message, (0, _findIndex.default)(_ => _.name === req.body.config)(_options$message2));
63
-
64
- if (configIndex === -1) {
65
- throw new Error(`Message config with name '${req.body.config}' not found.`);
66
- }
67
-
68
- req.body.config = configIndex;
69
- } else if (!options.message[req.body.config]) {
70
- throw new Error(`Message config not found at index ${req.body.config}.`);
31
+ let isNuxt3 = true;
32
+ try {
33
+ isNuxt3 = isNuxt3Try();
34
+ } catch {
35
+ isNuxt3 = false;
36
+ }
37
+ if (isNuxt3) {
38
+ addTemplate({
39
+ filename: P.join(moduleName, 'options.js'),
40
+ getContents: () => `export default ${JSON.stringify(options, undefined, 2)}`,
41
+ write: true
42
+ });
43
+ addTemplate({
44
+ filename: P.join(moduleName, 'send.js'),
45
+ getContents: () => fs.readFile(resolver.resolve('./send.js'), 'utf8'),
46
+ write: true
47
+ });
48
+ nuxt.options.alias['#mail'] = P.resolve(nuxt.options.buildDir, moduleName);
49
+ addServerHandler({
50
+ handler: resolver.resolve('./server-handler.post.js'),
51
+ route: '/mail/send'
52
+ });
53
+ addImports([{
54
+ from: resolver.resolve('./composable.js'),
55
+ name: 'useMail'
56
+ }]);
57
+ } else {
58
+ const app = express();
59
+ const transport = nodemailer.createTransport(options.smtp);
60
+ app.use(express.json());
61
+ app.post('/send', async (req, res) => {
62
+ try {
63
+ await send(req.body, options, transport);
64
+ } catch (error) {
65
+ return res.status(500).send(error.message);
71
66
  }
72
-
73
- await transport.sendMail({ ...(_req$body = req.body, (0, _omit.default)(['config', 'to', 'cc', 'bcc'])(_req$body)),
74
- ...(_options$message$req$ = options.message[req.body.config], (0, _omit.default)(['name'])(_options$message$req$))
75
- });
76
- } catch (error) {
77
- return res.status(400).send(error.message);
78
- }
79
-
80
- return res.sendStatus(200);
81
- });
82
- this.addServerMiddleware({
83
- handler: app,
84
- path: '/mail'
85
- });
86
- (0, _nuxtPushPlugins.default)(this, require.resolve("./plugin"));
87
- }
88
-
89
- module.exports = exports.default;
67
+ return res.sendStatus(200);
68
+ });
69
+ nuxt.addServerMiddleware({
70
+ handler: app,
71
+ path: '/mail'
72
+ });
73
+ }
74
+ nuxtPushPlugins(nuxt, resolver.resolve(`./plugin-nuxt${isNuxt3 ? 3 : 2}.js`));
75
+ }
@@ -0,0 +1,9 @@
1
+ export default ((context, inject) => inject('mail', {
2
+ send: async config => {
3
+ try {
4
+ await context.app.$axios.$post('/mail/send', config);
5
+ } catch (error) {
6
+ throw new Error(error.response.data);
7
+ }
8
+ }
9
+ }));
@@ -0,0 +1,13 @@
1
+ import { useFetch } from '#app';
2
+ export default ((context, inject) => inject('mail', {
3
+ send: async config => {
4
+ try {
5
+ await useFetch('/mail/send', {
6
+ body: config,
7
+ method: 'POST'
8
+ });
9
+ } catch (error) {
10
+ throw new Error(error.response.data);
11
+ }
12
+ }
13
+ }));
package/dist/send.js ADDED
@@ -0,0 +1,21 @@
1
+ import findIndex from "@dword-design/functions/dist/find-index.js";
2
+ import omit from "@dword-design/functions/dist/omit.js";
3
+ export default (async (body, options, transport) => {
4
+ body = {
5
+ config: 0,
6
+ ...body
7
+ };
8
+ if (typeof body.config === 'string') {
9
+ const configIndex = findIndex(_ => _.name === body.config)(options.message);
10
+ if (configIndex === -1) {
11
+ throw new Error(`Message config with name '${body.config}' not found.`);
12
+ }
13
+ body.config = configIndex;
14
+ } else if (!options.message[body.config]) {
15
+ throw new Error(`Message config not found at index ${body.config}.`);
16
+ }
17
+ await transport.sendMail({
18
+ ...omit(['config', 'to', 'cc', 'bcc'])(body),
19
+ ...omit(['name'])(options.message[body.config])
20
+ });
21
+ });
@@ -0,0 +1,16 @@
1
+ import { createError, defineEventHandler, readBody } from 'h3';
2
+ import nodemailer from 'nodemailer';
3
+ import options from '#mail/options.js';
4
+ import send from '#mail/send.js';
5
+ const transport = nodemailer.createTransport(options.smtp);
6
+ export default defineEventHandler(async event => {
7
+ try {
8
+ await send(await readBody(event), options, transport);
9
+ } catch (error) {
10
+ throw createError({
11
+ statusCode: 500,
12
+ statusMessage: error.message
13
+ });
14
+ }
15
+ return '';
16
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-mail",
3
- "version": "3.0.23",
3
+ "version": "3.1.1",
4
4
  "description": "Adds email sending capability to a Nuxt.js app. Adds a server route, an injected variable, and uses nodemailer to send emails.",
5
5
  "keywords": [
6
6
  "email",
@@ -23,6 +23,8 @@
23
23
  "funding": "https://github.com/sponsors/dword-design",
24
24
  "license": "MIT",
25
25
  "author": "Sebastian Landwehr <info@sebastianlandwehr.com>",
26
+ "type": "module",
27
+ "exports": "./dist/index.js",
26
28
  "main": "dist/index.js",
27
29
  "files": [
28
30
  "dist"
@@ -38,27 +40,27 @@
38
40
  },
39
41
  "dependencies": {
40
42
  "@dword-design/functions": "^4.0.0",
43
+ "@nuxt/kit": "^3.0.0",
41
44
  "express": "^4.17.1",
45
+ "fs-extra": "^11.1.0",
46
+ "h3": "^1.0.2",
42
47
  "nodemailer": "^6.4.11",
43
- "nuxt-push-plugins": "^2.0.0"
48
+ "nuxt-push-plugins": "^2.1.2",
49
+ "parse-packagejson-name": "^1.0.1"
44
50
  },
45
51
  "devDependencies": {
46
- "@dword-design/base": "^8.0.0",
47
- "@dword-design/proxyquire": "^2.0.0",
52
+ "@dword-design/base": "^9.1.9",
48
53
  "@dword-design/puppeteer": "^5.0.0",
49
54
  "@dword-design/tester": "^2.0.0",
50
- "@dword-design/tester-plugin-nodemailer-mock": "^1.0.0",
51
- "@dword-design/tester-plugin-puppeteer": "^2.0.0",
55
+ "@dword-design/tester-plugin-nuxt-config": "^1.1.3",
56
+ "@dword-design/tester-plugin-puppeteer": "^2.1.22",
52
57
  "@nuxtjs/axios": "^5.13.1",
53
- "axios": "^0.25.0",
58
+ "axios": "^0.27.2",
54
59
  "depcheck-package-name": "^2.0.0",
55
- "nodemailer-mock": "^1.5.4",
56
- "nuxt": "^2.15.3",
57
- "output-files": "^2.0.0",
58
- "with-local-tmp-dir": "^4.0.0"
60
+ "smtp-tester": "^2.0.1"
59
61
  },
60
62
  "engines": {
61
- "node": ">=12"
63
+ "node": ">=14"
62
64
  },
63
65
  "publishConfig": {
64
66
  "access": "public"
package/dist/plugin.js DELETED
@@ -1,19 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _default = (context, inject) => inject('mail', {
9
- send: async config => {
10
- try {
11
- await context.app.$axios.$post('/mail/send', config);
12
- } catch (error) {
13
- throw new Error(error.response.data);
14
- }
15
- }
16
- });
17
-
18
- exports.default = _default;
19
- module.exports = exports.default;