-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmail.js
55 lines (41 loc) · 1.64 KB
/
mail.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const crypto = require("crypto")
const conn = require('./dbconnect')
const nodemailer = require('nodemailer')
const sendMa = function (email) {
// create a token n update it into db, n timestamp
const token = crypto.randomBytes(20).toString('hex') //save to user table on token column
timeStamp = Date.now() //save to user table on created_at colum
const update = `UPDATE users SET created_at = ${timeStamp}, token = "${token}" WHERE email = "${email}"`
conn.query(update, (error, result) => {
if (error) {
throw error
} else {
console.log("created_at and token updated")
// if true send a mail
const smtpTransporter = nodemailer.createTransport({
host: "smtp.mailtrap.io",
port: 2525,
auth: {
user: "36641322885dff",
pass: "106c91f997a45e"
}
});
const mailOptions = {
to: "[email protected]",
from: '[email protected]',
subject: 'password reset notification',
text: 'Hello, \n\n' +
'please follow the below step to reset your password ',
html: `<a href="http://localhost:8000/passwordreset"> enter the given link for password reset </a>`
};
smtpTransporter.sendMail(mailOptions, function (err, req) {
if (err) {
console.log(err)
} else {
console.log('mail sent successfully')
}
});
}
})
}
module.exports = sendMa;