Javascript 使用NodeJs创建邮件服务
我有两个文件,我的app.js和我的mailer模块mailer.js 我的app.js应该在启动应用程序时发送几封电子邮件Javascript 使用NodeJs创建邮件服务,javascript,node.js,email,Javascript,Node.js,Email,我有两个文件,我的app.js和我的mailer模块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>
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');
});
const express=require('express');
常量app=express();
const mailer=require('./服务器/mailer');
mailer.sendHtmlMail([“email1”、“email2”]、“测试电子邮件”、“成功!”);
应用程序监听(8888,函数(){
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);
}
});
}
};
const nodemailer=require('nodemailer');
const senderMail=“myEmail”;
const emailTransporter=nodeEmailer.createTransport({
服务:'雅虎',
认证:{
用户:senderMail,
通过:'pw'
}
});
函数getMailReceivers(mailReceivers){//将字符串数组转换为一个字符串
var=”;
对于(var i=0;i
我收到了这个错误消息
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)
SyntaxError:意外标记(
在createScript上(vm.js:80:10)
在Object.runInThisContext(vm.js:139:10)
在模块处编译(Module.js:599:28)
在Object.Module.\u extensions..js(Module.js:646:10)
在Module.load(Module.js:554:32)
在tryModuleLoad时(module.js:497:12)
在Function.Module.\u加载(Module.js:489:3)
at Module.require(Module.js:579:17)
根据需要(内部/module.js:11:18)
at对象。(C:\…\app.js:3:16)
但我不明白,是吗
at对象。(C:…\app.js:3:16)
假设我的邮件对象(第3行)在第16行有错误?我在那里找不到任何语法错误。我将从更正开始
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);
}
});
}
}
我会从纠正开始
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);
}
});
}
}
您应该导出这样的对象(不是函数): 现在,在app.js中,您可以导入sendHtmlMail
const { sendHtmlMail } = require('./mailer');
然后像这样使用它:
sendHtmlMail(["email1", "email2"], "Test Email", "<p>Success!</p>");
sendHtmlMail([“email1”、“email2”]、“测试电子邮件”、“成功!”);
或:
const mailer=require('./mailer');
mailer.sendHtmlMail([“email1”、“email2”]、“测试电子邮件”、“成功!”);
我希望您会发现这些信息很有用您应该导出这样的对象(而不是函数): 现在,在app.js中,您可以导入sendHtmlMail
const { sendHtmlMail } = require('./mailer');
然后像这样使用它:
sendHtmlMail(["email1", "email2"], "Test Email", "<p>Success!</p>");
sendHtmlMail([“email1”、“email2”]、“测试电子邮件”、“成功!”);
或:
const mailer=require('./mailer');
mailer.sendHtmlMail([“email1”、“email2”]、“测试电子邮件”、“成功!”);
我希望您会发现这些信息很有用您的mailer.js文件的结尾就是问题所在。您看到的例外情况来自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
属性的对象,因此该属性应为对象初始值设定项:
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);
}
});
}
};
mailer.js文件的结尾就是问题所在。您看到的异常来自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
属性的对象,因此该属性应为对象初始值设定项:
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);
}
});
}
};
通常我会认为错误在你的
app.js
的第3行,但据我所知没有语法错误。你是否在这里包含了你的完整app.js
?是的,这是整个“项目”问题出在mailer.js
文件末尾的module.exports
部分。您的代码启动函数声明,但随后继续,就好像它实际上是一个对象初始值设定项一样。很抱歉,但我如何解决此问题?我尝试了module.exports={
但是获取意外标识符
通常我会认为错误在你的app.js
的第3行,但据我所知没有语法错误。你在这里包括你的完整app.js
了吗?是的,这是整个“项目”问题出在mailer.js
文件末尾的module.exports
部分。您的代码启动函数声明,但随后继续,就好像它实际上是一个对象初始值设定项一样。很抱歉,但我如何解决此问题?我尝试了module.exports={
但是获取意外标识符
好的,我纠正了这个错误,现在获取了错误mailer.sendHtmlMail不是一个函数
对,我想应该是模块。导出
行