Skip to content

Commit

Permalink
Merge pull request #343 from Sawan-Kushwah/feat/sendMail
Browse files Browse the repository at this point in the history
Added send mail feature to contact us form for instant reply to user #343
  • Loading branch information
Anuj3553 authored Nov 2, 2024
2 parents 13c60c8 + 0656c66 commit 727436a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server/Controllers/contact.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ContactForm = require('../Models/Contact');
const { sendMailToAdmin } = require('../sendMail');

const submitQueery = async (req, res) => {
// Extract data from the request body
Expand All @@ -15,6 +16,8 @@ const submitQueery = async (req, res) => {
msg: message,
createdAt: new Date(),
};
// console.log(contactData)
sendMailToAdmin(contactData)

try {
// Declare the variable properly
Expand Down
57 changes: 57 additions & 0 deletions server/sendMail.js
Original file line number Diff line number Diff line change
@@ -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: `<div style="background: #282c34; color: white; padding: 20px; font-family: Arial, sans-serif;">
<h2 style="text-align: center; color: #61dafb;">Bitbox Contact Form Details</h2>
<table style="width: 100%; max-width: 600px; margin: 20px auto; border-collapse: collapse;">
<tr>
<th style="padding: 10px; background-color: #0076b4; color: white; border: 1px solid #ddd;">Field</th>
<th style="padding: 10px; background-color: #0076b4; color: white; border: 1px solid #ddd;">Value</th>
</tr>
<tr>
<td style="padding: 10px; border: 1px solid #ddd;">Name</td>
<td style="padding: 10px; border: 1px solid #ddd;">${userData.name}</td>
</tr>
<tr>
<td style="padding: 10px; border: 1px solid #ddd;">Email</td>
<td style="padding: 10px; border: 1px solid #ddd;">${userData.email}</td>
</tr>
<tr>
<td style="padding: 10px; border: 1px solid #ddd;">Message</td>
<td style="padding: 10px; border: 1px solid #ddd;">${userData.msg}</td>
</tr>
<tr>
<td style="padding: 10px; border: 1px solid #ddd;">Submitted At</td>
<td style="padding: 10px; border: 1px solid #ddd;">${new Date(userData.createdAt).toLocaleString()}</td>
</tr>
</table>
</div>`, // HTML body content
});
}

main().catch(console.error);
};

module.exports = { sendMailToAdmin };

0 comments on commit 727436a

Please sign in to comment.