Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Callback for sendmail transport never called #150

Closed
tunix opened this Issue Apr 5, 2013 · 13 comments

Comments

Projects
None yet
5 participants

tunix commented Apr 5, 2013

Hello,

I'm sending e-mails from the server via relay and it works successfully with bash scripts which use mutt as client. Currently I'm writing a mocha reporter script which uses nodemailer to send test results as e-mail and I'm trying to use sendmail as the transport. However mail isn't sent and callback function is never called despite the systemd logs say:

Apr 05 11:15:49 sel1 postfix/pickup[11879]: A042E41D6B: uid=1000 from=<xxx@xxx.com>
Apr 05 11:15:49 sel1 postfix/cleanup[12874]: A042E41D6B: message-id=<20130405081549.A042E41D6B@sel1.localdomain>
Apr 05 11:15:49 sel1 postfix/qmgr[4141]: A042E41D6B: from=<xxx@xxx.com>, size=245, nrcpt=1 (queue active)
Apr 05 11:15:50 sel1 postfix/smtp[12877]: A042E41D6B: to=<xxx@xxx.com>, relay=ASPMX.L.GOOGLE.com[173.194.67.26]:25, delay=0.81, delays=0.05/0.02/0.16/0.58, dsn=2.0.0, status=sent (250 2.0.0 OK 1365149750 fa1si665103wid.95 - gsmtp)
Apr 05 11:15:50 sel1 postfix/qmgr[4141]: A042E41D6B: removed

Code is like the following:

var transport = nodemailer.createTransport("sendmail");

console.log('Sending message..');

var mailOptions = {
    from: "xxx@xxx.com",
    to: "xxx@xxx.com",
    subject: "Hello world!",
    text: "Plaintext body"
};

transport.sendMail(mailOptions, function(e) {
    if (e) { console.log(e); }

    console.log('Success!');
});
Owner

andris9 commented Apr 8, 2013

Hi, by running your code on my own machine, everything works. What version of node and nodemailer are you using? Additionally - are you using "real" sendmail or is it actually postfix replacement. Couldn't you use SMTP to localhost instead?

tunix commented Apr 8, 2013

Hi @andris9,

Actually I'd like to use relay because it's much easier to setup. I'm using postfix's sendmail so don't know if it's related. Server is an Arch Linux box btw.

$ node --version
v0.10.2

NodeMailer: 0.4.1

I'd kill to make this work because I want to send e-mails conditionally. Currently I'm using dumb cronjob and don't want to do tricks like grep and awk.. Thanks for the great library btw!

Owner

andris9 commented Apr 8, 2013

Could you run the following code snippet with node filename.jsand return the console output here. If everything works, an e-mail should be sent to "to" address.

// EDIT THESE VALUES

var from = "xxx@xxx.com",
    to = "yyy@yyy.com",
    sendmail_command = "sendmail";

// NO ADDITIONAL EDITING NEEDED

var spawn = require("child_process").spawn;
var mail = 'MIME-Version: 1.0\n'+
'X-Mailer: Nodemailer (0.4.1; +http://www.nodemailer.com/)\n'+
'Date: Mon, 08 Apr 2013 07:13:09 GMT\n'+
'Message-Id: <1365405189512.1bb25cd7@Nodemailer>\n'+
'From: ' + from + '\n'+
'To: ' + to + '\n'+
'Subject: Hello world!\n'+
'Content-Type: text/plain; charset=utf-8\n'+
'Content-Transfer-Encoding: quoted-printable\n'+
'\n'+
'Plaintext body\n'+
'Success!';

var sendmail = spawn(sendmail_command, ['-i', '-f', from, to]);

sendmail.stdout.pipe(process.stdout);
sendmail.stderr.pipe(process.stderr);

sendmail.on('exit', function(code){
    console.log("Sendmail exited with " + code);
});

sendmail.stdin.write(mail);
sendmail.stdin.end();

tunix commented Apr 8, 2013

@andris9 - it worked! how come?

Owner

andris9 commented Apr 8, 2013

In this case, I have no idea, why Nodemailer is not working. Only difference between this snipped and Nodemailer is that Nodemailer does not pipe sendmail stdout and sterr to console.

Additionally, when I use your provided code sample, then the mail gets sent as expected.

tunix commented Apr 8, 2013

@andris9 - i tried my code right after yours and again no mail was sent despite it was the opposite according to the logs. Can't understand why this happens. Could it be related with the cli parameters like -f and -i ?

Owner

andris9 commented Apr 9, 2013

Try editing node_modules/nodemailer/lib/engines/sendmail.js and add the following code to line 48

emailMessage.pipe(process.stdout);
sendmail.stdout.pipe(process.stdout);
sendmail.stderr.pipe(process.stderr);

This way all output from the sendmail command will be printed to console. Maybe there is some kind of error.

tunix commented Apr 9, 2013

@andris9 - nothing has changed. I can't see any additional output when I run the tests..

Owner

andris9 commented Jul 30, 2014

Closing as the sendmail module has been changed a lot in the past year and no one else has complained about it in the meantime

@andris9 andris9 closed this Jul 30, 2014

FYI- I'm seeing this same behavior... works outside of Mocha, but in Mocha tests, sendMail callback is not executed.

sabov commented Feb 28, 2015

I have the same issue with sendMail callback in Mocha test

sabov commented Feb 28, 2015

@SteveNewhouse In my case the issue was due to the fact that my test finished work before the connection with the SMTP server has been established. Try to call your done() function after sendMail callback.

I know time has passed since this thread was active but i would like to supplement @sabov answer for the benefits of future reader.
In addition to call done() callback but also increase timeout on your mocha test to allow test to wait for nodemailer to connect and send email. In my case i set timeout equal to 10000 and it has worked like a charm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment