0. 사전준비 사항


메일 발송하는 계정은 테스트 계정으로 새로 만들어서 테스트 하시는것을 추천합니다.

1) 보안 수준이 낮은 앱 엑세스 허용

보안 수준이 낮은 앱 허용을 해줘야 테스트 하는 어플리케이션에서 메일 발송이 가능합니다.

 

아래 URL을 클릭하시면 로그인 되어있는 계정으로 바로 설정 가능합니다.

 

https://myaccount.google.com/lesssecureapps

2) 내 Google 계정에 대한 액세스 허용

내 Google 계정에 대한 액세스 허용화면에서 계속 버튼을 클릭 해줍니다.

 

 

아래 URL을 클릭하시면 로그인 되어있는 계정으로 바로 설정 가능합니다.

https://accounts.google.com/DisplayUnlockCaptcha

1. nodemailer 모듈


nodemailer는 node 서버에서 메일을 보낼 수 있는 메일 전송 모듈입니다. 사용법이 간편하다는 장점이 있습니다.

nodemailer 공식문서는 하단 URL을 참고해주세요

https://nodemailer.com/about/

npm을 사용하시는 분들은 npm install nodemailer --save yarn을 사용하시는 분들은
yarn add nodemailer 명령으로 설치 가능합니다.

2. nodemailer을 이용한 발송 코드


1) nodemailer 발송 모듈

// nodemailer 모듈 요청
var nodemailer = require('nodemailer');

// 메일발송 객체
var mailSender = {
    // 메일발송 함수
    sendGmail : function(param){
        var transporter = nodemailer.createTransport({
            service: 'gmail'
            ,prot : 587
            ,host :'smtp.gmlail.com'
            ,secure : false
            ,requireTLS : true
            , auth: {
              user: '발송할 메일@gmail.com'
              ,pass: '패스워드'
            }
        });
        // 메일 옵션
        var mailOptions = {
                from: '발송할 메일@gmail.com',
                to: param.toEmail, // 수신할 이메일
                subject: param.subject, // 메일 제목
                text: param.text // 메일 내용
            };
        // 메일 발송    
        transporter.sendMail(mailOptions, function(error, info){
            if (error) {
            console.log(error);
            } else {
            console.log('Email sent: ' + info.response);
            }
        });

    }
}
// 메일객체 exports
module.exports = mailSender;

2) 메일 발송 함수 사용

let emailParam = {
    toEmail : loginId
    ,subject  : '메일 제목'
    ,text : '메일 내용'
};
mail.sendGmail(emailParam);

3.관련 오류

0. 사전 준비 사항을 하지 않아서 발생한 오류입니다.

Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at

2019-06-02T07:31:57.851494+00:00 app[web.1]: { Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at
2019-06-02T07:31:57.851514+00:00 app[web.1]: 535 5.7.8  https://support.google.com/mail/?p=BadCredentials n7sm6847779qkd.53 - gsmtp
2019-06-02T07:31:57.851516+00:00 app[web.1]: at SMTPConnection._formatError (/app/node_modules/nodemailer/lib/smtp-connection/index.js:781:19)
2019-06-02T07:31:57.851518+00:00 app[web.1]: at SMTPConnection._actionAUTHComplete (/app/node_modules/nodemailer/lib/smtp-connection/index.js:1516:34)
2019-06-02T07:31:57.851520+00:00 app[web.1]: at SMTPConnection._responseActions.push.str (/app/node_modules/nodemailer/lib/smtp-connection/index.js:554:26)
2019-06-02T07:31:57.851522+00:00 app[web.1]: at SMTPConnection._processResponse (/app/node_modules/nodemailer/lib/smtp-connection/index.js:940:20)essResponse (/app/node_modules/nodemailer/lib/smtp-connection/index.ta (/app/node_modules/nodemailer/lib/smtp-connection/index.js:746:14)js:940:20)                                                          tion._onSocketData (/app/node_modules/nodemailer/lib/smtp-connection/index.js:189:46)
2019-06-02T07:31:57.851524+00:00 app[web.1]: at SMTPConnection._onDats.js:198:13)ta (/app/node_modules/nodemailer/lib/smtp-connection/index.js:746:14adable.js:288:12))                                                                   tream_readable.js:269:11)
2019-06-02T07:31:57.851526+00:00 app[web.1]: at TLSSocket.SMTPConnecush (_stream_readable.js:224:10)tion._onSocketData (/app/node_modules/nodemailer/lib/smtp-connection [as onread] (internal/stream_base_commons.js:94:17)/index.js:189:46)
2019-06-02T07:31:57.851528+00:00 app[web.1]: at TLSSocket.emit (events.js:198:13)                                                        Password not accepted. Learn more at\n535 5.7.8  https://support.google.com/mail/?p=BadCredentials n7sm6842019-06-02T07:31:57.851530+00:00 app[web.1]: at addChunk (_stream_readable.js:288:12)
2019-06-02T07:31:57.851533+00:00 app[web.1]: at readableAddChunk (_stream_readable.js:269:11)
2019-06-02T07:31:57.851535+00:00 app[web.1]: at TLSSocket.Readable.push (_stream_readable.js:224:10)
2019-06-02T07:31:57.851537+00:00 app[web.1]: at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
2019-06-02T07:31:57.851540+00:00 app[web.1]: code: 'EAUTH',
2019-06-02T07:31:57.851542+00:00 app[web.1]: response:
2019-06-02T07:31:57.851545+00:00 app[web.1]: '535-5.7.8 Username and Password not accepted. Learn more at\n535 5.7.8  https://support.google.com/mail/?p=BadCredentials n7sm6847779qkd.53 - gsmtp',
2019-06-02T07:31:57.851547+00:00 app[web.1]: responseCode: 535,
2019-06-02T07:31:57.851549+00:00 app[web.1]: command: 'AUTH PLAIN' }

Error: Invalid login: 534-5.7.14 ...

2019-06-02T08:43:58.332064+00:00 app[web.1]: { Error: Invalid login: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtx
2019-06-02T08:43:58.332078+00:00 app[web.1]: 534-5.7.14 eGlXewx2NWs2Zu-3kdbaSLCHppRwhG4UkU5oG1lS7Tz0CAbeu2Y16L6sCdjSCqUtjye2TW
2019-06-02T08:43:58.332081+00:00 app[web.1]: 534-5.7.14 QvrjSZZGajjBg8AB6CE4l49G2Rn5Voeky6d1T81hhfx8RkcRI6dmf1nDBbYr7U> Please
2019-06-02T08:43:58.332084+00:00 app[web.1]: 534-5.7.14 log in via your web browser and then try again.
2019-06-02T08:43:58.332086+00:00 app[web.1]: 534-5.7.14  Learn more at
2019-06-02T08:43:58.332088+00:00 app[web.1]: 534 5.7.14  https://support.google.com/mail/answer/78754 a6sm2076445qkn.14 - gsmtp
2019-06-02T08:43:58.332090+00:00 app[web.1]: at SMTPConnection._formatError (/app/node_modules/nodemailer/lib/smtp-connection/index.js:781:19)
2019-06-02T08:43:58.332092+00:00 app[web.1]: at SMTPConnection._actionAUTHComplete (/app/node_modules/nodemailer/lib/smtp-connection/index.js:1516:34)
2019-06-02T08:43:58.332095+00:00 app[web.1]: at SMTPConnection._responseActions.push.str (/app/node_modules/nodemailer/lib/smtp-connection/index.js:554:26)
2019-06-02T08:43:58.332098+00:00 app[web.1]: at SMTPConnection._processResponse (/app/node_modules/nodemailer/lib/smtp-connection/index.js:940:20)
2019-06-02T08:43:58.332100+00:00 app[web.1]: at SMTPConnection._onData (/app/node_modules/nodemailer/lib/smtp-connection/index.js:746:14)
2019-06-02T08:43:58.332102+00:00 app[web.1]: at TLSSocket.SMTPConnection._onSocketData (/app/node_modules/nodemailer/lib/smtp-connection/index.js:189:46)
2019-06-02T08:43:58.332104+00:00 app[web.1]: at TLSSocket.emit (events.js:198:13)
2019-06-02T08:43:58.332106+00:00 app[web.1]: at addChunk (_stream_readable.js:288:12)
2019-06-02T08:43:58.332109+00:00 app[web.1]: at readableAddChunk (_stream_readable.js:269:11)
2019-06-02T08:43:58.332111+00:00 app[web.1]: at TLSSocket.Readable.push (_stream_readable.js:224:10)
2019-06-02T08:43:58.332113+00:00 app[web.1]: at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
2019-06-02T08:43:58.332115+00:00 app[web.1]: code: 'EAUTH',
2019-06-02T08:43:58.332118+00:00 app[web.1]: response:
2019-06-02T08:43:58.332123+00:00 app[web.1]: '534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtx\n534-5.7.14 eGlXewx2NWs2Zu-3kdbaSLCHppRwhG4UkU5oG1lS7Tz0CAbeu2Y16L6sCdjSCqUtjye2TW\n534-5.7.14 QvrjSZZGajjBg8AB6CE4l49G2Rn5Voeky6d1T81hhfx8RkcRI6dmf1nDBbYr7U> Please\n534-5.7.14 log in via your web browser and then try again.\n534-5.7.14  Learn more at\n534 5.7.14  https://support.google.com/mail/answer/78754 a6sm2076445qkn.14 - gsmtp',
2019-06-02T08:43:58.332126+00:00 app[web.1]: responseCode: 534,
2019-06-02T08:43:58.332128+00:00 app[web.1]: command: 'AUTH PLAIN' }

'언어 > NodeJS' 카테고리의 다른 글

Express를 이용한 NodeJS 웹서버 구축  (0) 2018.12.07

+ Recent posts