Search code examples
node.jsnodemailer

Why 'nodemailer' does not send any message?


Here is the code:

var nodemailder = require('nodemailer');
var from = nodemailder.createTransport({
    service: "gmail",
    auth:{
        user: 'user***@gmail.com',
        pass: 'pass***'
    }
});
var options = {
     from: 'user***@gmail.com',
     to: 'user***@gmail.com',
     subject: 'sometext',
     text: 'messageContext',
};
from.sendMail(options, function (error, info) {
     if (error) {
            console.log(error);
     } else {
            console.log('Email sent: ' + info.response);
     }
});

If I use Windows - it works, when I tried to start on Ubuntu (16) - I dont get any error message etc., but the mail was not received.


Solution

  • If that is the case, I believe SMTP service is unable to make connection to connection on port 25.

    Running server will look like:

    $ telnet localhost 25
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    220 xxxx.de ESMTP Postfix
    QUIT
    221 2.0.0 Bye
    Connection closed by foreign host.
    
    

    If there is no server listening on this port, it will like this:

    $ telnet localhost 25
    Trying 127.0.0.1...
    telnet: Unable to connect to remote host: Connection refused
    
    

    This means your services might not be running or enabled. You need to first enable/run this service on port 25.

    Also, according to the nodemail documentation you can use the sendmail binary as well:

    'sendmail' alternative
    
    Alternatively if you don't want to use SMTP but the sendmail
    
    

    command then set property sendmail to true (or as the path to sendmail if the command is not in default path).

    nodemailer.sendmail = true;
    
    or
    
    nodemailer.sendmail = '/path/to/sendmail';
    
    If sendmail is set, then SMTP options are discarded. 
    

    for reference, https://nodemailer.com/transports/sendmail/