trycode
Nodemailer - nodejs 로 메일 보내기 본문
728x90
반응형
Nodemailer
Nodemailer :: Nodemailer
Nodemailer Nodemailer is a module for Node.js applications to allow easy as cake email sending. The project got started back in 2010 when there was no sane option to send email messages, today it is the solution most Node.js users turn to by default. Nodem
nodemailer.com
Node.js 에서 메일을 쉽게 보내주게하는 모듈이다.
1. 메일 서버 이용
- 편의상 구글 계정으로 메일 서버로 고고
- 보통 계정당 500 명에게 보낼 수 있다 했는데 200명 정도가 max인듯
- 메일 서버로 사용할 구글 계정을 생성 했다면 아래 주소로 들어가 보안수준이 낮은앱 > '사용함' 으로 설정해야 테스트 메일을 받아볼 수 있다.
https://myaccount.google.com/lesssecureapps?pli=1
로그인 - Google 계정
하나의 계정으로 모든 Google 서비스를 Google 계정으로 로그인
accounts.google.com
2. 관련 모듈 설치
npm install nodemailer nodemailer-smtp-pool --save
3. mailer.js
var nodemailer = require('nodemailer');
//smtp 서버를 사용하기 위한 모듈이다.
var smtpPool=require('nodemailer-smtp-pool');
//nodemailer 의 createTransport는 transporter 객체를 만드는 메소드인데
//아래 메소드 참조값 변수 smtpTransport 는 nodemailer-smtp-pool 객체 인스턴스에 인자값으로 쓰인다.
var smtpTransport=nodemailer.createTransport(smtpPool( {
service:'Gmail',
host:'localhost',
port:'465',
tls:{
rejectUnauthorize:false
},
//이메일 전송을 위해 필요한 인증정보
//gmail 계정과 암호
auth:{
user:'지메일 계정주소',
pass:'비밀번호'
},
maxConnections:5,
maxMessages:10
}) );
var mailOpt={
from:'보내는 사람 주소@gmail.com',
to:'받는사람주소@gmail.com',
subject:'Nodemailer 테스트',
html:'<h1>우하하하 스팸 아님 테스트임</h1>'
}
smtpTransport.sendMail(mailOpt, function(err, res) {
if( err ) {
console.log(err);
}else{
console.log('Message send :'+ res);
}
smtpTransport.close();
})
4. 실행
node mailer
반응형
'Nodejs(+express)' 카테고리의 다른 글
express 기본 (0) | 2021.01.20 |
---|---|
express ( nodejs ) (0) | 2021.01.20 |
Comments