-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiweb
More file actions
56 lines (47 loc) · 1.21 KB
/
Copy pathapiweb
File metadata and controls
56 lines (47 loc) · 1.21 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
import nodemailer from "nodemailer";
import formidable from "formidable";
import fs from "fs";
export const config = {
api: {
bodyParser: false,
},
};
export default async function handler(req, res) {
if (req.method !== "POST") return res.status(405).end();
const form = formidable({ multiples: false });
form.parse(req, async (err, fields, files) => {
if (err) return res.status(500).send("Form error");
const { name, email, message } = fields;
const cv = files.cv;
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});
const mailOptions = {
from: process.env.EMAIL_USER,
to: process.env.TO_EMAIL,
subject: `New Application from ${name}`,
text: `
Name: ${name}
Email: ${email}
Message:
${message}
`,
attachments: [
{
filename: cv.originalFilename,
content: fs.readFileSync(cv.filepath),
},
],
};
try {
await transporter.sendMail(mailOptions);
res.status(200).json({ success: true });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
}