-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
54 lines (47 loc) · 1.44 KB
/
server.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
//import modules installed at the previous step. We need them to run Node.js server and send emails
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const nodemailer = require("nodemailer");
// create a new Express application instance
const app = express();
//configure the Express middleware to accept CORS requests and parse request body into JSON
app.use(origin = "*" );
app.use(bodyParser.json());
//start application server on port 3000
app.listen(3000, () => {
console.log("The server started on port 3000");
});
// define a sendmail endpoint, which will send emails and response with the corresponding status
app.post("/sendmail", (req, res) => {
console.log("request came");
let user = req.body;
sendMail(user, (err, info) => {
if (err) {
console.log(err);
res.status(400);
res.send({ error: "Failed to send email" });
} else {
console.log("Email has been sent");
res.send(info);
}
});
});
const sendMail = (user, callback) => {
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 587,
secure: false,
auth: {
user: "<sender email>",
pass: "<password>"
}
});
}
const mailOptions = {
from: `"Marina", "[email protected]"`,
to: `<${user.email}>`,
subject: "Novo membro Aviva",
html: "<h1>And here is the place for HTML</h1>"
};
transporter.sendMail(mailOptions, callback);