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

3
const SendMail = async (req, res) => {
4
5
6
7
8
  // const { email, title, cinema, theater, time, name, nickname } = req.body.userData
  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 + 65) + el.col)
  const sendMail = async (email, title, cinema, theater, time, name, selectedSeats, nickname) => {
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    // 메일을 전달해줄 객체
    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,
      },
    });

    // 메일 옵션
    const mailOptions = {
      from: `${cinema} <angelayoon99@gmail.com>`,
      to: `${email}`,
      subject: `${cinema} 예매확인내역: ${title}`,
      html: `<div>
        <h2>
33
          ${name || nickname}님의 예매
34
35
36
37
38
        </h2>
        <div>
          영화: ${title}
        </div>
        <div>
39
          장소:  ${cinema}  ${theater}
40
41
        </div>
        <div>
42
          일시 및 좌석: ${time} / ${selectedSeats.map(el => el+ ' ')}
43
44
45
46
47
48
49
50
51
52
53
54
55
56
        </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);
    }
  }

57
  sendMail(email, title, cinema, theater, time, name, selectedSeats, nickname);
Jiwon Yoon's avatar
Jiwon Yoon committed
58
59
60
61
}


export default { SendMail }