nuxt-mail 3.1.0 → 3.1.2
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 +90 -31
- package/dist/composable.js +2 -0
- package/dist/index.js +5 -1
- package/dist/plugin-nuxt3.js +14 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -67,13 +67,14 @@ $ yarn add nuxt-mail
|
|
|
67
67
|
```
|
|
68
68
|
<!-- /INSTALL -->
|
|
69
69
|
|
|
70
|
-
##
|
|
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
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
|
|
121
|
+
### Via injected variable
|
|
114
122
|
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 `
|
|
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
|
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import some from "@dword-design/functions/dist/some.js";
|
|
2
|
-
import { addServerHandler, addTemplate, createResolver, isNuxt3 as isNuxt3Try } from '@nuxt/kit';
|
|
2
|
+
import { addImports, addServerHandler, addTemplate, createResolver, isNuxt3 as isNuxt3Try } from '@nuxt/kit';
|
|
3
3
|
import express from 'express';
|
|
4
4
|
import fs from 'fs-extra';
|
|
5
5
|
import nodemailer from 'nodemailer';
|
|
@@ -50,6 +50,10 @@ export default function (moduleOptions, nuxt) {
|
|
|
50
50
|
handler: resolver.resolve('./server-handler.post.js'),
|
|
51
51
|
route: '/mail/send'
|
|
52
52
|
});
|
|
53
|
+
addImports([{
|
|
54
|
+
from: resolver.resolve('./composable.js'),
|
|
55
|
+
name: 'useMail'
|
|
56
|
+
}]);
|
|
53
57
|
} else {
|
|
54
58
|
const app = express();
|
|
55
59
|
const transport = nodemailer.createTransport(options.smtp);
|
package/dist/plugin-nuxt3.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
import { useFetch } from '#app';
|
|
2
|
-
export default ((
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { defineNuxtPlugin, useFetch } from '#app';
|
|
2
|
+
export default defineNuxtPlugin(() => ({
|
|
3
|
+
provide: {
|
|
4
|
+
mail: {
|
|
5
|
+
send: async config => {
|
|
6
|
+
try {
|
|
7
|
+
await useFetch('/mail/send', {
|
|
8
|
+
body: config,
|
|
9
|
+
method: 'POST'
|
|
10
|
+
});
|
|
11
|
+
} catch (error) {
|
|
12
|
+
throw new Error(error.response.data);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
11
15
|
}
|
|
12
16
|
}
|
|
13
17
|
}));
|
package/package.json
CHANGED