diff --git a/server/Controllers/contact.js b/server/Controllers/contact.js index 69dcfe4..fa9c713 100644 --- a/server/Controllers/contact.js +++ b/server/Controllers/contact.js @@ -1,4 +1,5 @@ const ContactForm = require('../Models/Contact'); +const { sendMailToAdmin } = require('../sendMail'); const submitQueery = async (req, res) => { // Extract data from the request body @@ -15,6 +16,8 @@ const submitQueery = async (req, res) => { msg: message, createdAt: new Date(), }; + // console.log(contactData) + sendMailToAdmin(contactData) try { // Declare the variable properly diff --git a/server/sendMail.js b/server/sendMail.js new file mode 100644 index 0000000..911f841 --- /dev/null +++ b/server/sendMail.js @@ -0,0 +1,57 @@ +const nodemailer = require('nodemailer'); +require("dotenv").config(); + +const sendMailToAdmin = (userData) => { + const transporter = nodemailer.createTransport({ + service: "gmail", + host: "smtp.gmail.com", + port: 587, + secure: false, // Use `true` for port 465, `false` for other ports + auth: { + user: process.env.EMAIL_USER, // Your email address + pass: process.env.EMAIL_PASS, // Your email password or app-specific password + }, + }); + + + async function main() { + await transporter.sendMail({ + from: { + name: `Bitbox Contact - ${new Date().toLocaleString()}`, + address: process.env.EMAIL_USER, + }, + to: process.env.ADMIN_EMAIL_ID, // Admin email address + subject: "New Contact Form Submission from Bitbox ✔", // Email subject + text: "Bitbox Contact Form", // Plain text body + html: `
+

Bitbox Contact Form Details

+ + + + + + + + + + + + + + + + + + + + + +
FieldValue
Name${userData.name}
Email${userData.email}
Message${userData.msg}
Submitted At${new Date(userData.createdAt).toLocaleString()}
+
`, // HTML body content + }); + } + + main().catch(console.error); +}; + +module.exports = { sendMailToAdmin };