nodemailer

👀·2024년 6월 12일
0

nodemailer

구글 앱 비밀번호 발급

구글 계정 설정 => Google-Account

2단계 인증

구글 앱 비밀번호 발급

예시

// api/transporter.ts
import nodemailer from "nodemailer";

export const transporter = nodemailer.createTransport({
  service: "gmail",
  host: 'smtp.gmail.com',
  port: 465, // SSL 
  // port: 587, // TLS
  secure: true, // SSL은 true TLS는 false
  requireTLS: false,
  auth: {
    user: "TEST_SENDER@gmail.com", //구글 메일 주소
    pass: "", //구글 앱 비밀번호
  },
});
// api/mail.ts
import { NextRequest, NextResponse } from "next/server";
import { transporter } from "..";

export async function POST(req: NextRequest) {
  try {
    const { to, subject, html } = await req.json();

    const mailOptions = {
      from: `발신자 이름 <TEST_SENDER@gmail.com>`, // 발신자
      to: to, // 수신자
      subject: subject, // 제목 ex) '인증 메일 도착!'
      html: html, // mail의 내용을 html형식으로 작성
      // attachments:["첨부파일"]
    };
    await transporter.sendMail(mailOptions);
    return NextResponse.json({ ok: true, msg: "인증 메일 전송 성공" }, { status: 200 });
  } catch (e: any) {
    return NextResponse.json({ ok: false, msg: "인증 메일 전송 실패!" }, { status: e.responseCode });
  }
}

0개의 댓글