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行目)にエラーがありますか?私は
通常、エラーはあなたの 'app.js'の行3にあると思いますが、私が見る限り、構文エラーはありません。完全な 'app.js'をここに入れましたか? –
はい、これは "project"全体です – Question3r
'mailer.js'ファイルの最後にある' module.exports'セクションが問題です。コードは関数の宣言を開始しますが、実際にはオブジェクトの初期化子のように処理されます。 – Pointy