otp-mail-service 1.0.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 +58 -0
- package/index.cjs +50 -0
- package/index.js +51 -0
- package/package.json +23 -0
- package/services/sendMail.js +19 -0
- package/utils/generateOtp.js +8 -0
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# OTP Mail Service
|
|
2
|
+
|
|
3
|
+
Reusable OTP and Reset Email Service using Nodemailer.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install otp-mail-service
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { sendEmail } from "otp-mail-service";
|
|
15
|
+
|
|
16
|
+
for Otp
|
|
17
|
+
|
|
18
|
+
await sendEmail({
|
|
19
|
+
from: process.env.EMAIL_USER,
|
|
20
|
+
pass: process.env.EMAIL_PASS,
|
|
21
|
+
to: "user@gmail.com",
|
|
22
|
+
type: "otp",
|
|
23
|
+
subject: "Email Verification OTP",
|
|
24
|
+
html: `
|
|
25
|
+
<h3>Your OTP is {{otp}}</h3>
|
|
26
|
+
`
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
for Reset link
|
|
30
|
+
|
|
31
|
+
const reset_link = `http://localhost:****/reset-password?token=${token}`;
|
|
32
|
+
|
|
33
|
+
await sendEmail({
|
|
34
|
+
from: process.env.EMAIL_USER,
|
|
35
|
+
pass: process.env.EMAIL_PASS,
|
|
36
|
+
to: email,
|
|
37
|
+
type: "reset",
|
|
38
|
+
subject: "Email Verification OTP",
|
|
39
|
+
html: `
|
|
40
|
+
<h3>Password Reset</h3>
|
|
41
|
+
<p>Click below to reset your password:</p>
|
|
42
|
+
<a href="{{resetLink}}">Reset Password</a>
|
|
43
|
+
`,
|
|
44
|
+
resetLink: reset_link
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
## Required Fields
|
|
52
|
+
|
|
53
|
+
- from → Sender email address
|
|
54
|
+
- pass → App password of email
|
|
55
|
+
- to → Receiver email
|
|
56
|
+
- type → "otp" or "reset"
|
|
57
|
+
- subject → Email subject
|
|
58
|
+
- html → HTML content
|
package/index.cjs
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const { sendMail } = require("./services/sendMail.cjs");
|
|
2
|
+
const { generateOtp } = require("./utils/generateOtp.cjs");
|
|
3
|
+
|
|
4
|
+
async function sendEmail({
|
|
5
|
+
from,
|
|
6
|
+
pass,
|
|
7
|
+
to,
|
|
8
|
+
type,
|
|
9
|
+
subject,
|
|
10
|
+
html,
|
|
11
|
+
resetLink
|
|
12
|
+
}) {
|
|
13
|
+
|
|
14
|
+
if (!subject || !html) {
|
|
15
|
+
throw new Error("Subject and HTML are required");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (type === "otp") {
|
|
19
|
+
const otp = generateOtp();
|
|
20
|
+
const finalHtml = html.replace("{{otp}}", otp);
|
|
21
|
+
|
|
22
|
+
await sendMail({
|
|
23
|
+
from,
|
|
24
|
+
pass,
|
|
25
|
+
to,
|
|
26
|
+
subject,
|
|
27
|
+
html: finalHtml
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
return otp;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (type === "reset") {
|
|
34
|
+
const finalHtml = html.replace("{{resetLink}}", resetLink);
|
|
35
|
+
|
|
36
|
+
await sendMail({
|
|
37
|
+
from,
|
|
38
|
+
pass,
|
|
39
|
+
to,
|
|
40
|
+
subject,
|
|
41
|
+
html: finalHtml
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
throw new Error("Invalid email type");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
module.exports = { sendEmail };
|
package/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
|
|
2
|
+
import { sendMail } from "./services/sendMail.js";
|
|
3
|
+
import { generateOtp } from "./utils/generateOtp.js";
|
|
4
|
+
|
|
5
|
+
export async function sendEmail({
|
|
6
|
+
from,
|
|
7
|
+
pass,
|
|
8
|
+
to,
|
|
9
|
+
type,
|
|
10
|
+
subject,
|
|
11
|
+
html,
|
|
12
|
+
resetLink
|
|
13
|
+
}) {
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
if (!subject || !html) {
|
|
17
|
+
throw new Error("Subject and HTML are required");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (type === "otp") {
|
|
21
|
+
|
|
22
|
+
const otp = generateOtp();
|
|
23
|
+
const finalHtml = html.replace("{{otp}}", otp);
|
|
24
|
+
await sendMail({
|
|
25
|
+
from,
|
|
26
|
+
pass,
|
|
27
|
+
to,
|
|
28
|
+
subject,
|
|
29
|
+
html: finalHtml
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
return otp;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (type === "reset") {
|
|
36
|
+
|
|
37
|
+
const finalHtml = html.replace("{{resetLink}}", resetLink);
|
|
38
|
+
|
|
39
|
+
await sendMail({
|
|
40
|
+
from,
|
|
41
|
+
pass,
|
|
42
|
+
to,
|
|
43
|
+
subject,
|
|
44
|
+
html: finalHtml
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
throw new Error("Invalid email type");
|
|
51
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "otp-mail-service",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Reusable OTP and Reset Email Service using Nodemailer",
|
|
5
|
+
"main": "index.cjs",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
"import": "./index.js",
|
|
9
|
+
"require": "./index.cjs"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"otp",
|
|
13
|
+
"email",
|
|
14
|
+
"nodemailer",
|
|
15
|
+
"reset-password",
|
|
16
|
+
"email-service"
|
|
17
|
+
],
|
|
18
|
+
"author": "Vijin Raj",
|
|
19
|
+
"license": "ISC",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"nodemailer": "^8.0.1"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import nodemailer from "nodemailer";
|
|
2
|
+
|
|
3
|
+
export async function sendMail({from, pass, to, subject, html }) {
|
|
4
|
+
|
|
5
|
+
const transporter = nodemailer.createTransport({
|
|
6
|
+
service: "gmail",
|
|
7
|
+
auth: {
|
|
8
|
+
user: from,
|
|
9
|
+
pass: pass,
|
|
10
|
+
},
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
await transporter.sendMail({
|
|
14
|
+
from: from,
|
|
15
|
+
to,
|
|
16
|
+
subject,
|
|
17
|
+
html
|
|
18
|
+
});
|
|
19
|
+
}
|