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 번 줄)에 오류가 있습니까? 나는
일반적으로 오류는 'app.js'의 3 행에 있다고 생각하지만, 구문 오류는 없습니다. 여기에 전체'app.js'를 포함 시켰습니까? –
예, 이것은 전체 "프로젝트"입니다. – Question3r
'mailer.js' 파일의 끝에있는'module.exports' 섹션이 문제입니다. 코드는 함수 선언을 시작하지만, 실제로는 객체 이니셜 라이저 인 것처럼 진행됩니다. – Pointy