· 7 years ago · Feb 01, 2019, 10:52 AM
1const nodemailer = require("nodemailer");
2 const smtpTransport = nodemailer.createTransport({
3 service: "Gmail",
4 auth: {
5 user: "...@gmail.com",
6 pass: "..."
7 }
8 });
9
10 router.post('/...', (req, res) => {
11 ...
12 rand = Math.floor((Math.random() * 100) + 54);
13 host = req.get('host');
14 link = "http://" + host + "/api/users/verify?id=" + rand;
15 let mailOptions = {
16 from: smtpTransport.user,
17 to: req.body.user_email,
18 subject: "Please confirm your Email account",
19 html: "Hello,<br> Please Click on the link to verify your email.<br><a href=" + link + ">Click here to verify</a>"
20 }
21 smtpTransport.sendMail(mailOptions, (err, res) => {
22 if (!err) {
23 res.status(200).json({
24 message: 'Email sent successfully',
25 });
26 } else {
27 return res.status(500).json({
28 error: err
29 });
30 }
31 });
32 ...
33 });
34
35router.get('/...', (req, res) => {
36 if ((req.protocol + "://" + req.get('host')) == ("http://" + host)) {
37 console.log("Domain is matched. Information is from Authentic email");
38 if (req.query.id == rand) {
39 console.log("email is verified");
40 res.end("<h1>Email " + mailOptions.to + " is been Successfully verified");
41 } else {
42 console.log("email is not verified");
43 res.end("<h1>Bad Request</h1>");
44 }
45 } else {
46 res.end("<h1>Request is from unknown source");
47 }
48});