yarn add nodemailer
yarn add nodemailer-sendgrid-transport

src/server.js

...
...
import { sendSecretMail } from "./utils";

sendSecretMail("itnicolasme@gmail.com", "123");
...
...

계정 아이디 비밀 번호는 .env 파일에 저장한다.

.env

PORT=4000
SENDGRID_USERNAME="ryan"
SENDGRID_PASSWORD="cute123"

src/utils.js

import dotenv from "dotenv";
import path from "path";
dotenv.config({ path: path.resolve(__dirname, ".env") });

import nodemailer from "nodemailer";
import sgTransport from "nodemailer-sendgrid-transport";

const sendMail = email => {
  const options = {
    auth: {
      api_user: process.env.SENDGRID_USERNAME,
      api_key: process.env.SENGRID_PASSWORD
    }
  };
  const client = nodemailer.createTransport(sgTransport(options));
  return client.sendMail(email);
};

export const sendSecretMail = (adress, secret) => {
  const email = {
    from: "cuteryan@kakao.com",
    to: adress,
    subject: "이편지는2119년에서보내져왔습니다.",
    html: "Hello! 이건 스팸메일이얌"
  };
  return sendMail(email);
};

+ 따끈한 최근 게시물