-1

I am trying to use session.send instead of console.log in transporter.sendMail, so that the user knows when the Email was sent successfully, but it does not work.
The error is "session is not defined".
This is how my code looks like:

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);
}
);
Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
Alex
  • 267
  • 1
  • 3
  • 18

3 Answers3

1

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

You are not using a correct function definition, since the callback of transport.SendMail can only have two parameters (error & info). Have a look at the Nodemailer example.

In your case it would look like this. Just make sure you use this snippet where the session context is available.

transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        return session.error(error);
    }

    session.send('Message sent: ' + info.messageId);
});
Mick
  • 2,946
  • 14
  • 19
  • I wrote this snippet in a separated nodejs file where there is no session context. How can i define it please? – Alex Dec 03 '18 at 17:11
  • You should include `session` in the function you call, from where you have session context. It is hard to advise without seeing more code.. – Mick Dec 03 '18 at 17:42
  • 1
    you mean if i had the nodemailer in function sendemail(from , to ) should i include the session parameter in the function so that it looks like : function sendemail(from , to , session) and call it where i defined session ? – Alex Dec 03 '18 at 19:40
1

this is an example of how to doing it. Just make sure you call this method inside a session context like:

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;
0

I just ran this snippet replacing the username and password appropriately and ended up with this:

{ 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' }

Furthermore I received an email from 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.

Are you sure this is the code that you're running? The error you posted, "session is not defined" seems like a syntax error.

Mohd Ali
  • 21
  • 2
  • @Mohid Ali i am using this code and its working perfectly fine. But i want to send "message sent" to the bot so that the user knows when the email was sent successfully – Alex Nov 30 '18 at 23:46