-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodeMailer.js
46 lines (40 loc) · 1.13 KB
/
nodeMailer.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
const express = require("express");
const nodemailer = require("nodemailer");
const nodeMailer = express();
// Middleware to parse JSON body
nodeMailer.use(express.json());
// Endpoint to handle form submission
nodeMailer.post("/send-email", (req, res) => {
const { name, email, subject, message } = req.body;
// Create a transporter with your email service configuration
const transporter = nodemailer.createTransport({
host: "smtp.elasticemail.com",
port: 2525,
auth: {
user: "[email protected]",
pass: "894AE55CE2B0B826FB9C4529D6133356506F",
},
});
// Set up email content
const mailOptions = {
from: email,
to: "[email protected]",
subject,
text: `
Name: ${name}
Email: ${email}
Message: ${message}
`,
};
// Send the email
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error(error);
res.status(500).send("An error occurred while sending the email.");
} else {
console.log("Email sent: " + info.response);
res.send("Email sent successfully.");
}
});
});
module.exports = nodeMailer;