Front-End/Node.js

[Node.js] node.js를 사용하여 메일 보내기 실습 예제

챌링킴 2021. 8. 14. 16:17
반응형

1) nodemailer 모듈

- 메일서버를 통해 메일을 보낼 수 있도록 하는 모듈이다.

- cmd창에 npm i nodemailer 실행해주기

 

 

2) nodemailer 모듈 설정하기

createTransport({
	service: '메일서버',
    auth: {
    	user: '계정',
        pass: '비밀번호'
    },
    host: '메일서버도메인',
    port: '포트'
});

 

 

3) nodemailer 모듈 내용 설정하기

const 객체명 = {
	from: '이름<메일주소>',
    to: '이름<메일주소>',
    subject: '제목',
    text: '내용' //html 'html코드'
}

 

** 하단의 url 접속하여 메일을 보내주기 위한 보안설정 허용해주기

더보기

보안 수준이 낮은 앱의 액세스 https://myaccount.google.com/lesssecureapps

더보기

계정 액세스 사용을 허용 https://accounts.google.com/DisplayUnlockCaptcha

 

const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
	service: 'Gmail',
    auth: {
    	user: 'ssieru@gmail.com',
        pass: '비밀번호'
    },
    host: 'smtp.mail.com',
    port: '465'
});

const mailOption = {
	from: '착실한채린학생<ssieru@gmail.com>',
    to: '짱짱<ssieru@naver.com>',
    subject: '🎀node.js로 보내는 메일!!🎀',
    // text: '제곧내입니다.' //html 'html코드'
    html: "<h2>안녕하세요. node.js로 보내는 메일입니다.</h2><p style='font-size:15px; color:deeppink;'>안뇽</p>"
};

transporter.sendMail(mailOption, (err,info)=>{
    if(err){
        console.log(err);
    }else{
        console.log(info);
    }
    transporter.close();
})

 

 

4) 메일 보내기 실습

 

mail.html

 

action="/mail" method="post"

보내는 아이디[  ]

보내는 이메일[  ]

받는 아이디[  ]

받는 이메일[  ]

제목 [  ]

내용 [  ]

[보내기]

반응형