2024-01-23[TIL]

jenna·2024년 1월 22일
0

TIL/WIL

목록 보기
57/59

Nodemailer

: node.js 환경에서 이메일을 쉽게 보낼 수 있도록 도와주는 모듈

  1. nodemailer 설치
yarn add nodemailer
  1. smtpTransport 객체
let smtpTransport = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'your-email@gmail.com',
    pass: 'your-password'
  },
  tls: {
      rejectUnauthorized: false, //서버의 SSL 인증서를 검증할지 여부를 결정(보안)
   }
});
  1. mailOptions 설정( 보내는 사람, 받는 사람, 제목, 본문 등 지정)
let mailOptions = {
  from: 'your-email@gmail.com',
  to: 'receiver-email@example.com',
  subject: 'Hello',
  text: 'Hello world!'
};
  1. 설정한 transporter, mailOptions을 사용해 이메일 보내기
smtpTransport.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
  smtpTransport.close();
});

aws ses 적용해보기

  1. aws-sdk 설치
yarn add aws-sdk
  1. aws ses 사용 설정 및 nodemailer 이메일 전송객체 생성
AWS.config.update({
   accessKeyId: process.env.AWS_SES_ACCESS_KEY,
   secretAccessKey: process.env.AWS_SES_SECRET_KEY,
   region: process.env.AWS_SES_REGION,
});

const sesMailer = nodemailer.createTransport({
   SES: new AWS.SES({
      apiVersion: '2010-12-01',
   }),
});
  • nodemailer의 createTransport 메서드를 사용해 aws ses 설정해서 sendMail 메서드를 통해 이메일을 보낼 수 있다
  • new AWS.SES({ apiVersion: '2010-12-01' })는 AWS SES 서비스 객체를 생성하는 부분이다
  1. mailOptions 설정( 보내는 사람, 받는 사람, 제목, 본문 등 지정)
let mailOptions = {
  from: 'your-email@gmail.com',
  to: 'receiver-email@example.com',
  subject: 'Hello',
  text: 'Hello world!'
};
  1. 설정한 transporter, mailOptions을 사용해 이메일 보내기
sesMailer.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
  smtpTransport.close();
});
profile
https://github.com/jennaaaaaaaaa

0개의 댓글