-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (109 loc) · 3.36 KB
/
index.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const express = require("express");
const app = express();
require("dotenv").config();
const { body, validationResult } = require("express-validator");
const port = 8000;
var cors = require('cors')
app.use(cors())
app.use(express.urlencoded());
//app.use(express.json());
const expressLayouts = require("express-ejs-layouts");
const nodemailer = require("nodemailer");
const mailGun = require("nodemailer-mailgun-transport");
const auth = {
auth: {
api_key: process.env.API_KEY , // TODO: Replace with your mailgun API KEY
domain: process.env.DOMAIN, // TODO: Replace with your mailgun DOMAIN
},
};
const transporter = nodemailer.createTransport(mailGun(auth));
app.use(expressLayouts);
//app.use(express.static("./assets"));
// extract style and scripts from sub pages into the layout
app.set("layout extractStyles", true);
app.set("layout extractScripts", true);
//app.use(expressLayouts);
app.set("view engine", "ejs");
app.set("views", "./views");
app.get("/", (req, res) => {
res.render("form");
});
app.post(
"/form",
[
body("name")
.trim()
.isAlpha()
.withMessage("name error>>not alpabhet")
.notEmpty()
.withMessage("name error>>space is empty")
.isLength({ min: 3, max: 6 })
.withMessage(
"name error>> name should have minimum letters of 3 and maximum letters of 6"
),
body("email")
.notEmpty()
.withMessage("email error>>empty space")
.isEmail()
.withMessage("email error>>not a valid email")
.contains("gmail")
.withMessage("email error>> only gmails are allowed"),
body("number")
.notEmpty()
.withMessage("phone error>>space is empty")
.isNumeric()
.withMessage("phone error>>only number are allowed")
.isLength({ min: 10, max: 10 })
.withMessage("phone error>> phone should have 10 numbers"),
],
(req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
//console.log(errors)
//return res.send(errors)
var arr = [];
errors.array().forEach((error) => {
console.log(error.msg);
arr.push(error.msg);
});
return res.render("error", {
data: arr,
});
}
console.log(req.body);
const { name, number, email } = req.body;
try {
transporter.sendMail(
{
from: "[email protected]",
to: "[email protected]", // An array if you have multiple recipients.
//cc:'[email protected]',
//bcc:'[email protected]',
subject: "form data",
//'replyTo': '[email protected]',
//You can use "html:" to send HTML email content. It's magic!
//html: '<b>Wow Big powerful letters</b>',
//You can use "text:" to send plain-text content. It's oldschool!
text: ` name : ${name} , phone : ${number} , email: ${email}`,
},
(err, info) => {
if (err) {
console.log(`Error: ${err}`);
} else {
console.log(`data sent successfully`);
return res.render("success");
}
}
);
} catch (error) {
console.log(error)
}
//console.log("assume email sent");
}
);
app.listen(port, function (err) {
if (err) {
console.log(`Error in running the server: ${err}`);
}
console.log(`Server is running on port: ${port}`);
});