【问题标题】:Session.send() does not work: "session is not defined"Session.send() 不起作用:“会话未定义”
【发布时间】:2018-11-30 23:27:38
【问题描述】:

我正在尝试在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);
}
);

【问题讨论】:

    标签: javascript node.js nodemailer web-development-server


    【解决方案1】:

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

    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;

    【讨论】:

      【解决方案2】:

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

      您没有使用正确的函数定义,因为 transport.SendMail 的回调只能有两个参数(错误和信息)。看看Nodemailer example

      在您的情况下,它看起来像这样。只要确保在session 上下文可用的地方使用这个sn-p。

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

      【讨论】:

      • 我把这个 sn-p 写在一个没有会话上下文的单独的 nodejs 文件中。请问如何定义?
      • 你应该在你调用的函数中包含session,从那里你有会话上下文。如果没有看到更多代码,很难给出建议..
      • 你的意思是如果我在函数 sendemail(from , to ) 中有 nodemailer,我应该在函数中包含 session 参数,使它看起来像:函数 sendemail(from , to , session) 并调用它我在哪里定义了会话?
      【解决方案3】:

      我刚刚运行了这个 sn-p 适当地替换了用户名和密码,结果是这样的:

      { 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:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.js:605:19)
          at SMTPConnection._actionAUTHComplete (C:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.js:1340:34)
          at SMTPConnection._responseActions.push.str (C:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.js:378:26)
          at SMTPConnection._processResponse (C:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.js:764:20)
          at SMTPConnection._onData (C:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.js:570:14)
          at TLSSocket._socket.on.chunk (C:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.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 via\n534-5.7.14 your web browser and then try again.\n534-5.7.14  Learn more at\n534 5.7.14  https://support.google.com/mail/answer/78 - gsmtp',
        responseCode: 534,
        command: 'AUTH PLAIN' }

      此外,我还收到了来自 Google 的电子邮件:

      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.

      您确定这是您正在运行的代码吗?您发布的错误“会话未定义”似乎是语法错误。

      【讨论】:

      • @Mohid Ali 我正在使用这段代码,它工作得很好。但我想向机器人发送“已发送消息”,以便用户知道电子邮件何时发送成功
      猜你喜欢
      • 2014-04-08
      • 1970-01-01
      • 2016-07-06
      • 1970-01-01
      • 2018-10-30
      • 2017-09-02
      相关资源
      最近更新 更多