email.controller.js 1.64 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
2
import nodemailer from "nodemailer"

Kim, Subin's avatar
Kim, Subin committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const SendMail = async (req, res) => {
  const { email, name, nickname } = req.body.userData
  const { title, cinema, time, theater } = req.body
  const selectedSeats = req.body.reservationData.map(el => String.fromCharCode(el.row + 64) + el.col)
  const sendMail = async (email, title, cinema, theater, time, name, selectedSeats, nickname) => {
    const transporter = nodemailer.createTransport({
      host: 'smtp.gmail.com',
      port: 465,
      secure: true,
      auth: {
        type: "OAuth2",
        user: "angelayoon99@gmail.com",
        clientId: process.env.GMAIL_CLIENTID,
        clientSecret: process.env.GMAIL_CLIENTSECRET,
        refreshToken: process.env.GMAIL_REFRESH_TOKEN,
      },
      tls: {
        rejectUnauthorized: false,
      },
    });
Jiwon Yoon's avatar
Jiwon Yoon committed
23

Kim, Subin's avatar
Kim, Subin committed
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
    // 메일 옵션
    const mailOptions = {
      from: `${cinema} <angelayoon99@gmail.com>`,
      to: `${email}`,
      subject: `${cinema} 예매확인내역: ${title}`,
      html: `<div>
        <h2>
          ${name || nickname}님의 예매
        </h2>
        <div>
          영화: ${title}
        </div>
        <div>
          장소:  ${cinema}  ${theater}
        </div>
        <div>
          일시 및 좌석: ${time} / ${selectedSeats.map(el => el + ' ')}
        </div>
      </div>`
    };
    try {
      const mailResult = await transporter.sendMail(mailOptions);
      console.log(`Mail sent - ID : ${mailResult.messageId}`);
    } catch (err) {
      console.log("Mail Sending Failuer.");
      console.log(err);
    }
  }
  sendMail(email, title, cinema, theater, time, name, selectedSeats, nickname);
}
Jiwon Yoon's avatar
Jiwon Yoon committed
54
55

export default { SendMail }