-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (60 loc) · 1.75 KB
/
app.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
const express = require("express");
const cors = require("cors");
const dotenv = require("dotenv");
const nodemailer = require("nodemailer")
dotenv.config();
const port = process.env.PORT || 3000;
const app = express();
app.use(cors());
app.use(express.urlencoded());
app.use(express.json());
app.listen(port, () => {
console.log(`Server hosted on http://localhost:${port}`);
});
app.get("/", (req, res) => {
res.json({ name: "Express Mailer !!" });
});
app.post("/send-mail", (req, res) => {
const output = `
<p>You have a new Mail by the Website</p>
<p>${req.body.subject}</p>
<h3>Contact Details</h3>
<pre>
Name: ${req.body.name}
Email: ${req.body.email}
</pre>
<h3>Subject</h3>
<p>${req.body.subject}</p>
<h3>Message</h3>
<p>${req.body.message}</p>
`;
// Transporter for mail
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 587,
secure: false,
auth: {
user: process.env.EMAIL_USERNAME, // enter the sender email address in .env file
pass: process.env.EMAIL_PASSWORD, // enter the sender email password in .env file
},
tls: {
rejectUnauthorized: false,
},
});
// setup email data
let mailOptions = {
from: '"New Mail from Website" <yoursendermail@mail.com>', // sender address
to: "ris8645208@gmail.com", // list of receivers
subject: `${req.body.subject}`, // Subject line
html: output, // html body
};
// send mail
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
res.json({ msg: "Email has been sent" });
});
});