Next.js로 블로그 같은 것을 만든 뒤 contact 메일을 보낼 수 있도록 구성을 해보자
import nodemailer from "nodemailer";
export type EmailData = {
from: string;
subject: string;
message: string;
};
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: process.env.AUTH_USER,
pass: process.env.AUTH_PASS,
},
});
export async function sendEmail({ subject, from, message }: EmailData) {
const mailData = {
to: process.env.AUTH_USER,
subject: `[BLOG] ${from}: ${subject}`,
from,
html: `
<h1>${subject}</h1>
<div>${message}</div>
<br/>
<p>보낸 사람: ${from}</p>
`,
};
const info = await transporter.sendMail(mailData);
console.log("Message sent: %s", info.messageId);
return info;
}