我正在尝试使用session.send
而不是console.log
in transporter.sendMail
,以便用户知道电子邮件发送成功的时间,但是它不起作用。
错误是“未定义会话”。
这是我的代码的样子:
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: '[email protected]',
pass: 'myPassword'
}
});
// setup e-mail data
var mailOptions = {
from: '"Our Code World " <[email protected]>', // sender address (who sends)
to: '[email protected], [email protected]', // 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 " <[email protected]>', // sender address (who sends)
to: '[email protected]', // 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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句