2017-12-21 1 views
0

mailer.js라는 두 파일, my app.js 및 mailer 모듈이 있습니다.NodeJs를 사용하여 메일 서비스 만들기

내 app.js는 응용 프로그램을 시작할 때 여러 개의 전자 메일을 보내야합니다.

const express = require('express'); 
const app = express(); 
const mailer = require('./Server/mailer'); 

mailer.sendHtmlMail(["email1", "email2"], "Test Email", "<p>Success!</p>"); 

app.listen(8888, function() { 
    console.log('Server running on port 8888'); 
}); 

그리고 내 mailer.js

const nodemailer = require('nodemailer'); 

    const senderMail = "myEmail"; 

    const emailTransporter = nodemailer.createTransport({ 
     service: 'yahoo', 
     auth: { 
     user: senderMail, 
     pass: 'pw' 
     } 
    }); 

    function getMailReceivers(mailReceivers){ // convert the string array to one string 
     var receivers = ""; 

     for(var i = 0; i < mailReceivers.length; i++){ 
     receivers += mailReceivers[i]; 

     if(i < mailReceivers.length - 1) 
      receivers += ", "; 
     } 

     return receivers; 
    } 

    function getMailOptions(mailReceivers, subject, html){ // set the mail options and return them 
     return { 
     from: senderMail, 
     to: getMailReceivers(mailReceivers), 
     subject: subj, 
     html: content 
     }; 
    } 

    module.exports = function() { // export the sendMail function here 

    sendHtmlMail: function(mailReceivers, subject, html){ // send the email 
     emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function(error, info){ 
      if (error) { 
      throw error; 
      } else { 
      console.log(info.response); 
      } 
     }); 
     } 

    }; 

내가

SyntaxError: Unexpected token (
    at createScript (vm.js:80:10) 
    at Object.runInThisContext (vm.js:139:10) 
    at Module._compile (module.js:599:28) 
    at Object.Module._extensions..js (module.js:646:10) 
    at Module.load (module.js:554:32) 
    at tryModuleLoad (module.js:497:12) 
    at Function.Module._load (module.js:489:3) 
    at Module.require (module.js:579:17) 
    at require (internal/module.js:11:18) 
    at Object.<anonymous> (C:\...\app.js:3:16) 

는하지만 난 그것을 이해하지 못하는이 오류 메시지를받을 메일 서비스를 실행

을한다 Object에서. (C : ... \ app.js : 3 : 16)

16 번 줄에있는 메일러 객체 ​​(3 번 줄)에 오류가 있습니까? 나는

+0

일반적으로 오류는 'app.js'의 3 행에 있다고 생각하지만, 구문 오류는 없습니다. 여기에 전체'app.js'를 포함 시켰습니까? –

+0

예, 이것은 전체 "프로젝트"입니다. – Question3r

+0

'mailer.js' 파일의 끝에있는'module.exports' 섹션이 문제입니다. 코드는 함수 선언을 시작하지만, 실제로는 객체 이니셜 라이저 인 것처럼 진행됩니다. – Pointy

답변

1

당신의 mailer.js 파일의 끝 유용한 정보를 찾을 희망

const mailer = require('./mailer'); 

mailer.sendHtmlMail(["email1", "email2"], "Test Email", "<p>Success!</p>"); 

을 문제가있는 곳입니다. 그 예외는 다른 파일 인 require()이 있기 때문에 app.js의 3 행에서 발생합니다.

: 그 개체 초기화해야하므로,

module.exports = function() { // export the sendMail function here 

sendHtmlMail: function(mailReceivers, subject, html){ // send the email 
    emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function(error, info){ 
     if (error) { 
     throw error; 
     } else { 
     console.log(info.response); 
     } 
    }); 
    } 

}; 

귀하의 코드는 sendHtmlMail 속성 개체를 내보낼 해당 모듈을 기대 :

문제는 당신이 객체 initiailzation와 기능 인스턴스화 혼합 한 것입니다

1

내가

function sendHtmlMail(mailReceivers, subject, html){ 

sendHtmlMail: function(mailReceivers, subject, html){ 

를 수정에서 시작하거나, 당신이 정말로 객체 리터럴 구문을 필요로하는 경우, 유효한 장소를 찾을 것입니다 .. 거기에 어떤 구문 오류를 찾을 수 없습니다 이것이 허용되는 코드에서 - 그것은 정의 된 범위에서 오류입니다.

편집 : 당신이 객체를 반환하는 함수를 수출하고 싶었 가능성, 즉

module.exports = function() { // export the sendMail function here 

    return { 
    sendHtmlMail: function (mailReceivers, subject, html) { // send the email 
    emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function (error, info) { 
     if (error) { 
      throw error; 
     } else { 
      console.log(info.response); 
     } 
     }); 

    } 
    } 

하거나 당신은 같은 객체를 내 보내야

module.exports = { // export the sendMail function here 

    sendHtmlMail: function (mailReceivers, subject, html) { // send the email 
    emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function (error, info) { 
     if (error) { 
      throw error; 
     } else { 
      console.log(info.response); 
     } 
     }); 

    } 
} 
+0

ok 이것을 수정하고 이제'mailer.sendHtmlMail은 함수가 아닙니다 .' – Question3r

+2

대신'module.exports' 행은' module.exports = {' – Pointy

+0

hm, '예상치 못한 식별자'가 생겼습니다 :/ – Question3r

1

기능을 가진 객체가 될 것입니다 (함수가 아님) :

module.exports = { 

    sendHtmlMail: function(mailReceivers, subject, html){ // send the email 
     emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function(error, info){ 
     if (error) { 
      throw error; 
     } else { 
      console.log(info.response); 
     } 
     }); 
    } 

} 

이제 app.js에서 sendHt mlMail

const { sendHtmlMail } = require('./mailer'); 

그리고 다음과 같이 사용 :

sendHtmlMail(["email1", "email2"], "Test Email", "<p>Success!</p>"); 

또는 : 나는 당신이

+0

왜? 그건 도움이되지 않습니다. 'app.js'의 코드는 메일러 모듈이 "sendHtmlMail"이라는 속성을 가진 객체를 내보낼 것을 기대합니다. – Pointy

+0

수정 됨! 이제는 잘 작동합니다. – Sergito

관련 문제