본문 바로가기
NODE.JS

Node.js - nodemailer 를 이용한 메일 보내기

by devLog by Ronnie's 2021. 5. 29.

Node.js - nodemailer 를 이용한 메일 보내기

 

1. nodemailer install

정상 설치가 되었다면 해당 프로젝트에 노드 모듈이 설치된 것을 확인할 수 있다.

2. code (nodemailer.js 생성)

이때 require에 Unresolved function or method require() 라는 에러가 나는 경우에는 preferences에 node.js and npm을 검색해서 아래 네모체크박스 안을 체크해주면 된다.

메일 서버에 대한 정보를 입력한다. 메일 서버로는 google, naver 등을 이용할수 있다. 필자는 mailtrap이라는 사이트를 이용하여 테스트를 진행하였다. smtp에 대한 설정에 가서 nodemailer 선택 후 값을 가져온다.

화살표 함수를 사용하여 함수 표현식보다 단순하고 간결한 문법으로 함수를 만들수 있다. 

ex1과 ex2는 동일 코드이다.

ex1)

ex2)

위에서 선언한 send 메서드안에 option 값으로 들어가게 될 메일에 대한 정보를 기술한다.

 

실행을 해보면 메일이 보내진 것을 확인할 수 있다.

 

참고

https://ko.javascript.info/arrow-functions-basics

 

화살표 함수 기본

 

ko.javascript.info

https://nodemailer.com/message/

 

Message configuration :: Nodemailer

\ Message configuration The following are the possible fields of an email message: Common fields from - The email address of the sender. All email addresses can be plain ‘[email protected]’ or formatted '“Sender Name” [email protected]', see Addr

nodemailer.com

 

댓글