gpt4 book ai didi

javascript - 使用 NodeJs 创建邮件服务

转载 作者:太空宇宙 更新时间:2023-11-04 03:23:12 24 4
gpt4 key购买 nike

我有两个文件,我的 app.js 和名为 mailer.js 的邮件程序模块。

启动应用程序时,我的 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)

但是我不明白,是吗

at Object. (C:...\app.js:3:16)

说我的邮件程序对象(第 3 行)第 16 行有错误?我在那里找不到任何语法错误..

最佳答案

mailer.js 文件的末尾就是问题所在。您看到的异常来自 app.js 的第 3 行,因为这是您 require() 另一个文件的地方。

问题是您混淆了函数实例化和对象初始化:

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);
}
});
}
};

关于javascript - 使用 NodeJs 创建邮件服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47927401/

24 4 0
文章推荐: c - 有多少单词使 memcpy 值得?
文章推荐: python - 将 pandas DataFrame 行与键/值对的字典匹配
文章推荐: html -
文章推荐: c - 结构不取值
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com