Luc Luc - 1 year ago 108
Node.js Question

send email from node.js app using nodemailer

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

echo "hello" | mail -s 'test email'

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'
sender: '',
html: 'test',
function(error, 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,
syscall: 'connect' }

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

Answer Source

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
Connected to localhost.
Escape character is '^]'.
220 ESMTP Postfix
221 2.0.0 Bye
Connection closed by foreign host.

here is no server running:

$ telnet localhost 25
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;


nodemailer.sendmail = '/path/to/sendmail';

If sendmail is set, then SMTP options are discarded.