Bardelman
Bardelman

Reputation: 2298

Can Nodemailer be a standalone SMTP server?

i was reading about that Nodemailer is using SMTP or sendmail or Amazon SES and i also discovered more smtp servers so i was assuming that aside of my nodemailer application i have to make one of those SMTP servers running and make my app connect to it but someone told me that Nodemailer can be itself an autonomous (or maybe a standlaone) SMTP server "Nodemailer is autonomous to send mails (it is not necessary to go through a server / SMTP service)".

So i was reading again its documentation and i found that the Nodemailer transport object uses the nodemailer-smtp-transport module as default

var transporter = nodemailer.createTransport(smtpTransport(options)) (1)

or (by using smtpTransport as default)

var transporter = nodemailer.createTransport(options) (2)

(which i suppose it means that (1) is equivalent to (2))

So i was thiking that maybe it's the nodemailer-smtp-transport which makes Nodemailer to be a standalone smtp server and then i tried this code for defining the Nodemailer transporter and to make it sending the email with sendMail: (let's call this snippet1)

var transporter = nodemailer.createTransport(smtpTransport({
    host: 'localhost',
    port: 465,
    auth: {
        user: 'username',
        pass: 'pass'
    }
}));

// NB! No need to recreate the transporter object. You can use 
// the same transporter object for all e-mails 

// setup e-mail data with unicode symbols 
var mailOptions = {
    from: 'Ahmed Feki ✔ <[email protected]>', // sender address 
    to: '[email protected], [email protected]', // list of receivers 
    subject: 'Hello ✔', // Subject line 
    text: 'Hello world ✔', // plaintext body 
    html: '<b>Hello world from nodemailer test ✔</b>' // html body 
};


// send mail with defined transport object 
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log('####  sendMail error : ' + error);
    }
    console.log('####  Message sent: ' + info.response);

});

and when i was running the app i got this error :

sendMail error : Error: connect ECONNREFUSED 127.0.0.1:25

So i decided to use the smtp-server in the same application using this code :

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

var server = new SMTPServer({
    onConnect: function(session, callback){
        if(session.remoteAddress === '127.0.0.1'){
            return callback(new Error('No connections from localhost allowed'));
        }
        return callback(); // Accept the connection
    },
    onError:function(err){
        console.log('Error onError %s', err.message);
    }
});

/*
        onError wasn't mentioned on the documentation i just wrote it myself 
        as to replace the error on listener :

          server.on('error', function(err){
             console.log('Error %s', err.message);
          }); 
        and i don't know if it would work or not.. i actually kept both of them      and just modified the log for i can know from where it would come.
*/

server.listen(465, onConnectCb);

and inside the onConnectCb definition (witten in the above last line) i wrote all the snippet1's code and results where to much better as now i got the following log :

[2015-11-02 15:33:11] INFO: Connection from ::ffff:127.0.0.1]
[2015-11-02 15:33:11] DEBUG: S: 220 Fekis-PC ESMTP
[2015-11-02 15:33:11] DEBUG: C: EHLO [127.0.0.1]
[2015-11-02 15:33:11] DEBUG: S: 250-OK: Nice to meet you [::ffff:127.0.0.1]
[2015-11-02 15:33:11] DEBUG: [mhJFwLqR43aX] C: STARTTLS
[2015-11-02 15:33:11] DEBUG: [mhJFwLqR43aX] S: 220 Ready to start TLS
[2015-11-02 15:33:11] INFO: [mhJFwLqR43aX] Connection upgraded to TLS
####  sendMail error : Error: self signed certificate
[2015-11-02 15:33:11] INFO: Connection closed to [::ffff:127.0.0.1]

which is too much better even the email wasn't sent as there is still an error on it

'self signed certificate'

which i suppose it happens because i still didn't set the key and the cert as options in the SMTPServer object (as i still don't know how to get them).

var server = new SMTPServer({
    /*secure: true,
    key: fs.readFileSync('private.key'),
    cert: fs.readFileSync('server.crt')*/
});

So i asked this question to get a confirmation that Nodemailer can't be a standalone SMTP server.

Upvotes: 3

Views: 6362

Answers (2)

pingle60
pingle60

Reputation: 776

To fix the self-signed certificate error, add the following to the smtpTransport options,

tls: {
   rejectUnauthorized: false
},

Upvotes: 0

Andris
Andris

Reputation: 27875

Nodemailer does not act as a server itself but it is able to send mail without a relaying SMTP using direct-transport method https://github.com/andris9/nodemailer-direct-transport

Sending mail without a relay is not recommended though as there is much higher chance of being rejected or put into spam folder as the sending host might not be trusted enough by the receiving host

Upvotes: 1

Related Questions