Question

From my ubuntu (10.04) box, I have no problem send email with:

echo "hello" | mail -s 'test email' my_gmail_nickname@gmail.com

When I try to send an email from a node.js app running on the same machine, it does not work.

var nodemailer = require('nodemailer');
nodemailer.SMTP = {
  host: 'localhost'
}
nodemailer.send_mail(
{
    sender: 'me@example.com',
    to:'my_gmail_nickname@gmail.com',
    subject:'Hello!',
    html: 'test',
    body:'test'
},
function(error, success){
    console.log(error);
    console.log(success);
    console.log('Message ' + success ? 'sent' : 'failed');
});

I have the error message:

me@luc:~/gridteams/services/gpshop$ cat nohup.out 
{ stack: [Getter/Setter],
  arguments: undefined,
  type: undefined,
  message: 'ECONNREFUSED, Connection refused',
  errno: 111,
  code: 'ECONNREFUSED',
  syscall: 'connect' }
null
sent

I see the connection refused but do not understand why I get this error. What do you think is the missing piece ?

Was it helpful?

Solution

I think your problem is like this:

The commandline program mail uses a binary called /usr/sbin/sendmail for sending mail. sendmail is a commandline programm which will try to deliver the mail. It uses a local connection to the mail infrastructure.

The node nodemailer will try to connect to a SMTP server on the host localhost on TCP port 25 which doesn't exist. Just try to get a connection using the telnet program for verification.

Here is a server running:

$ 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.

here is no server running:

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

If you get the second, you have a problem with your SMTP which is not started/enabled listening on port 25 - which is the default (for security reasons). You need to configure it first.

Or - 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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top