관리 메뉴

JIHYUN JEONG

[Node.js]Nodemailer 모듈 (3) – 메일보내기(첨부파일) 본문

Information Technology/Node.js

[Node.js]Nodemailer 모듈 (3) – 메일보내기(첨부파일)

StopHyun 2018. 1. 10. 14:54


 

 이번에는 첨부파일을 보내보도록 하겠습니다.





 

첨부파일을 보내기 위해서 우선 해당 소스 경로에 ‘attachment_test’ 라는 엑셀파일을 우선 생성합니다. 꼭 해당 소스파일과 같은 경로에 생성해야 합니다.



 

안에 내용은 간단하게  아래와 같이 작성했습니다. (임의대로 해도 상관없습니다.)


 

그리고 나서 mailOptions 안에 아래의 attachment 파일에 대한 파일명과 첨부할 파일 경로를 지정해줍니다.

  // attachment configuration

  attachments: [

    {

      filename: 'attachment_test.xlsx',

      path: 'attachment_test.xlsx',

    },

  ],

};

 

그리고 소스코드를 실행시키면 아래와 같이 결과 값이 뜰 경우 정상적으로 첨부파일이 발송되었습니다.




Message sent: 250 2.0.0 OK 1513750868 i27sm1485172iod.29 - gsmtp

[ { filename: 'attachment_test.xlsx',

    path: 'attachment_test.xlsx' } ]

 


참고사항: 더 많은 첨부사항 기능과 관련해서는 

https://nodemailer.com/message/attachments/ 를 확인하시기 바랍니다.




1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

const nodemailer = require('nodemailer');

 

const transporter = nodemailer.createTransport({

  service: 'Gmail',

  auth: {

    user: '지메일아이디@gmail.com',

    pass: '비밀번호',

  },

});

 

// setup email data with unicode symbols

const mailOptions = {

 

  from: '지메일아이디@gmail.com'// sender address

  to: '지메일아이디@gmail.com'// list of receivers

  subject: 'Hello attachment'// Subject line

  // text: 'Hello world?', // plain text body

 

  // html body

  html: '<h1>Hello Attachment</h1><a href="http://www.infopub.co.kr">' +

    '<img src="http://www.infopub.co.kr/pdspool/common/main_top/2016-11-02.jpg"/></p></a>',

 

  // attachment configuration

  attachments: [

    {

      filename: 'attachment_test.xlsx',

      path: 'attachment_test.xlsx',

    },

  ],

};

 

// send mail with defined transport object

transporter.sendMail(mailOptions, (error, info) => {

  if (error) {

    console.log(error);

  } else {

    console.log(`Message sent: ${info.response}`);

    console.log(mailOptions.attachments);

  }

  transporter.close();

});






In preparing for battle I have always found that plans are useless, but planning is indispensable.

나는 전투를 준비하면서 계획은 무용하나 계획하는 것은 꼭 필요함을 항상 발견해왔다.





Comments