Session.send() 不起作用:"session is not defined"



我正在尝试在transporter.sendMail中使用session.send而不是console.log,这样用户就可以知道电子邮件何时成功发送,但它不起作用
错误为"会话未定义">
我的代码是这样的:

var nodemailer = require('nodemailer');
// Create the transporter with the required configuration for Gmail
// change the user and pass !
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true, // use SSL
auth: {
user: 'myemail@gmail.com',
pass: 'myPassword'
}
});
// setup e-mail data
var mailOptions = {
from: '"Our Code World " <myemail@gmail.com>', // sender address (who sends)
to: 'mymail@mail.com, mymail2@mail.com', // list of receivers (who receives)
subject: 'Hello', // Subject line
text: 'Hello world ', // plaintext body
html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info,session){
if(error){
return console.log(error);
}
session.send('Message sent: ' + info.response);
}
);

这是一个如何做到这一点的例子。只需确保在会话上下文中调用此方法,如:

const sendmail = require('./email'); // in case you have the class called email
bot.dialog('/', function(session) {
sendmail.sendmail(session);
session.send("hello")
});

function sendmail(session){
var nodemailer = require('nodemailer');
// Create the transporter with the required configuration for Outlook
// change the user and pass !
var transport = nodemailer.createTransport( {
service: "hotmail",
auth: {
user: "",
pass: ""
}
});
// setup e-mail data, even with unicode symbols
var mailOptions = {
from: '"Our Code World " <shindar902009@hotmail.com>', // sender address (who sends)
to: 'shindar902009@hotmail.com', // list of receivers (who receives)
subject: 'Hello ', // Subject line
text: 'Hello world ', // plaintext body
html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js' // html body
};
// send mail with defined transport object
transport.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
session.send('Message sent');
});
}
module.exports.sendmail = sendmail;

transporter.sendMail(mailOptions, function(error, info , session)

由于传输的回调,您没有使用正确的函数定义。SendMail只能有两个参数(错误和信息(。看看Nodemailer的例子。

在你的情况下,它会是这样的。只需确保在session上下文可用的地方使用此代码段。

transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return session.error(error);
}
session.send('Message sent: ' + info.messageId);
});

我刚刚运行了这个片段,适当地替换了用户名和密码,结果是:

{ Error: Invalid login: 534-5.7.14 <https://accounts.google.com/signin/continu> Please log in via
534-5.7.14 your web browser and then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/ - gsmtp
at SMTPConnection._formatError (C:projectsnodemailertestnode_modulesnodemailerlibsmtp-connectionindex.js:605:19)
at SMTPConnection._actionAUTHComplete (C:projectsnodemailertestnode_modulesnodemailerlibsmtp-connectionindex.js:1340:34)
at SMTPConnection._responseActions.push.str (C:projectsnodemailertestnode_modulesnodemailerlibsmtp-connectionindex.js:378:26)
at SMTPConnection._processResponse (C:projectsnodemailertestnode_modulesnodemailerlibsmtp-connectionindex.js:764:20)
at SMTPConnection._onData (C:projectsnodemailertestnode_modulesnodemailerlibsmtp-connectionindex.js:570:14)
at TLSSocket._socket.on.chunk (C:projectsnodemailertestnode_modulesnodemailerlibsmtp-connectionindex.js:522:47)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
code: 'EAUTH',
response: '534-5.7.14 <https://accounts.google.com/signin/continue?..._sniped> Please log in vian534-5.7.14 your web browser and then try again.n534-5.7.14  Learn more atn534 5.7.14  https://support.google.com/mail/answer/78 - gsmtp',
responseCode: 534,
command: 'AUTH PLAIN' }

此外,我收到了一封来自谷歌的电子邮件:

Someone just used your password to try to sign in to your account from a non-Google app. Google blocked them, but you should check what happened. Review your account activity to make sure no one else has access.

你确定这就是你正在运行的代码吗?您发布的错误"会话未定义"似乎是一个语法错误。

最新更新