2017-12-21 20 views
0

mailer.jsという2つのファイル、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) 

を取得するが、私はそれを理解していない、

を行いますオブジェクトで(C:... \ app.js:3:16)

16行目の私のメーラーオブジェクト(3行目)にエラーがありますか?私は

+0

通常、エラーはあなたの 'app.js'の行3にあると思いますが、私が見る限り、構文エラーはありません。完全な 'app.js'をここに入れましたか? –

+0

はい、これは "project"全体です – Question3r

+0

'mailer.js'ファイルの最後にある' module.exports'セクションが問題です。コードは関数の宣言を開始しますが、実際にはオブジェクトの初期化子のように処理されます。 – Pointy

答えて

1

あなたmailer.jsファイルの末尾に有用この情報を見つけることを願っています

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

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

を問題がどこにあるのか。その例外はapp.jsの3行目にあります。なぜなら、それは他のファイルrequire()です。

問題は、あなたがオブジェクトinitiailzationと機能のインスタンス化を混ぜたことである:

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プロパティを持つオブジェクトをエクスポートする予定なので、それはオブジェクト初期化する必要があります:

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); 
     } 
    }); 
    } 
}; 
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、 'Unexpected identifier'を得ました:/ – 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'のコードは、mailerモジュールが" sendHtmlMail "という名前のプロパティを持つオブジェクトをエクスポートすることを期待しています。 – Pointy

+0

編集済み!今それは正常に動作するはずです – Sergito

関連する問題