-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsendMail.js
52 lines (42 loc) · 1.24 KB
/
sendMail.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
import express from "express"
const router = express.Router()
// Import nodemailer
import nodemailer from 'nodemailer';
import smtpTransport from "nodemailer-smtp-transport";
//Post
router.post('/', async (req, res) => {
try {
const transporter = nodemailer.createTransport(smtpTransport({
service: 'gmail',
host: 'smtp.gmail.com',
auth: {
user: req.body.senderEmail,
pass: req.body.appPassword
}
}));
var mailOptions = {
from: req.body.senderEmail,
to: req.body.receiverEmail,
subject: req.body.subject,
html: req.body.bodyHTML
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error)
res.status(400).json({
status: 400,
message: error.response
})
} else {
res.status(200).json({
status: 200,
message: `Email sent successfully to ${req.body.receiverEmail}`
})
}
});
}
catch (err) {
console.log(err);
}
})
export default router