5

I'm using nodemailer to sends emails:

var nodemailer = require('nodemailer'),
config = require('./mailer.conf');

var smtpTransport;

console.log('Creating Transport');

//smtp transport configuration
var smtpTransport = nodemailer.createTransport({
    host: config.host,
    port: config.port,
    auth: {
        user: config.email,
        pass: config.password
    }
});

//Message
var message = {
    from: "me@localhost.com",
    replyTo: "me@localhost.com",
    to: "me@localhost",
    subject: "hello"
};

console.log('Sending Mail');
// Send mail
smtpTransport.sendMail(message, function(error, info) {
    if (error) {
        console.log(error);
    } else {
        console.log('Message sent successfully!');
        console.log('Server responded with "%s"', info.response);
    }
    console.log('Closing Transport');
    smtpTransport.close();
});

I have also a local smtp server using smtp-server:

var SMTPServer = require('smtp-server').SMTPServer;

var server = new SMTPServer({
    onData: function(stream, session, callback) {
        console.log('received');
    }
});

server.listen(465);
console.log('listening');

I don't see "received" when I send emails to my localhost smtp server (note to: "me@localhost" in the client code).

What am I missing to make it work?

Rafael Angarita
  • 777
  • 10
  • 27

1 Answers1

2

You may need to define the authentication handler on your options

var server = new SMTPServer({
    onAuth: function(auth, session, callback) {
        callback(null, { user : auth });
    },
    onData: function(stream, session, callback) {
        console.log('received');
    },
});

Learn more about onAuth params

If you get a the self signed certificateerror, you may need to include this before sending your email:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
Khalid
  • 4,730
  • 5
  • 27
  • 50
  • Setting `NODE_TLS_REJECT_UNAUTHORIZED` is a bad idea since it applies to the entire node app. If you're running into `self signed certificate` errors as @Khalid mentioned, you can add `tls: { rejectUnauthorized: false }` to the `createTransport ` options object. – kingliam Sep 19 '18 at 00:15