Nodemailer: return a promise if no callback is supplied

Created on 19 Jul 2014  路  12Comments  路  Source: nodemailer/nodemailer

specifically for .sendMail()

Most helpful comment

Added a Promise wrapper with b88e7a655e609b0580fff5ec861e6f247755fda1 (v1.9.0). If callback is not defined for sendMail then the method returns a native Promise object instead

transport.sendMail(options).then(function(info){
    console.log(info);
}).catch(function(err){
    console.log(err);
});

All 12 comments

I'm not really into promises. You could create a wrapper if you need it.

@jonathanong, how hard would it be to put together a 'koa-nodemailer' wrapper? I'd like to see one, but I don't think I'm yet up to putting one together. @andris9, it would be better if the core nodemailer could provide direct support for Koa.

no, don't make koa-specific wrapper. make a nodemailer-then wrapper.

I think I'll live with callbacks just for the moment :)

+1 on nodemailer-then wrapper, or integrating promises directly. ES6 is here, promises are (either fortunately or unfortunately, depending on your position) going to become standard as we creep towards ES7 and async/await.

+1 for Promises.
0.12 and 4 support them natively
For 0.10 developers can use https://github.com/jakearchibald/es6-promise

Added a Promise wrapper with b88e7a655e609b0580fff5ec861e6f247755fda1 (v1.9.0). If callback is not defined for sendMail then the method returns a native Promise object instead

transport.sendMail(options).then(function(info){
    console.log(info);
}).catch(function(err){
    console.log(err);
});

Hi, this doesn't work for me. I import Bluebird first, and transport.sendMail(options) returns undefined (instead of a promise).

Is there a full example/tutorial on how to use promises with nodemailer? Apparently, Promise is a "global" object, in the scope of nodemailer module. So it's unclear how to get this working.

Thanks!

You need to use the latest Nodemailer with Nodejs 0.12+ as Nodemailer uses native Promise API that is not available in Node.js 0.10 and below.

If these conditions are met then you can use code like this:

transport.sendMail(mailOptions).then(function(info){
    // sending succeeded
}).catch(function(err){
    // sending failed
});

I see... so we can use this workaround for node < 0.12:

GLOBAL.Promise = require('bluebird');
var nodemailer = require('nodemailer');
...

Thanks

I'm in favor of the next major version dropping callback support, as https://github.com/feathersjs/feathers did

Yeah guys, let's drop this callback stuff for the sake of js. Nowadays it's all promises - async/await 馃憤

Was this page helpful?
0 / 5 - 0 ratings