-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
69 lines (55 loc) · 1.71 KB
/
server.js
File metadata and controls
69 lines (55 loc) · 1.71 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import express from 'express';
import cors from 'cors';
import nodemailer from 'nodemailer';
import dotenv from 'dotenv';
dotenv.config();
const app = express();
const PORT = process.env.PORT || 5000;
app.use(cors(
{
origin: process.env.CORS_ORIGIN || '*',
methods: ['POST'],
credentials: true,
}
));
app.use(express.json());
app.post('/send-email', async (req, res) => {
const mailTo = req.body.mailTo;
if (!mailTo) {
return res.status(400).json({ success: false, message: 'No email address provided' });
}
const requestData = req.body;
let htmlContent = '<h2>New Submission</h2><ul>';
for (const key in requestData) {
if(key === 'mailTo' || key === 'resume' || key === 'jobId') continue;
if (key === 'resumeLink') {
htmlContent += `<li><strong>${key}:</strong> <a href="${requestData[key]}">Download File</a></li>`;
continue;
}
htmlContent += `<li><strong>${key}:</strong> ${requestData[key]}</li>`;
}
htmlContent += '</ul>';
const transporter = nodemailer.createTransport({
service: 'gmail', // or your SMTP
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});
const mailOptions = {
from: `"Mailer API" <${process.env.EMAIL_USER}>`,
to: mailTo,
subject: 'New Submission Received',
html: htmlContent,
};
try {
await transporter.sendMail(mailOptions);
res.status(200).json({ success: true, message: 'Email sent successfully' });
} catch (err) {
console.error('Email send failed:', err);
res.status(500).json({ success: false, message: 'Failed to send email' });
}
});
app.listen(PORT, () => {
console.log(`✅ Mail sender running in port: ${PORT}`);
});