Bluebird: promisify nodemailer-smtp-transport

Created on 30 Sep 2014  Â·  5Comments  Â·  Source: petkaantonov/bluebird

Hey, i try to get into bluebird. can you help me with wrapping nodemailer-smtp-transport?
I am using koa and tried a lot but couldnt find the solution.
Here is my code:


var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var Promise = require("bluebird");

exports.create = function*() {
  var transporter = nodemailer.createTransport(smtpTransport({
      host: 'smtp.myemailservice.de',
      port: 25,
      auth: {
          user: 'user',
          pass: 'pw'
      }
  }));

  var mailOptions = {
      from: 'Fred Foo ✔ <[email protected]>', // sender address
      to: '[email protected]', // list of receivers
      subject: 'Hello ✔', // Subject line
      text: 'Hello world ✔', // plaintext body
      html: 'mail conent'·
  };

  transporter.sendMail(mailOptions, function(error, info) {
      if (error) {
          this.body = error;
      } else {
        this.body = info.response;
      }
  });

};

Most helpful comment

I was also having the same problem some time ago. The code above is no longer valid. You have to use this:

var nodemailer = require('nodemailer')
var transport = Promise.promisifyAll(nodemailer.createTransport({ /* options */ }))

Note that if you are trying to use a transport mechanism that is not built-in, there is a small difference. Instead of passing an options object to the createTransport method, you pass the external transport directly like so:

// Example using the stub transport
var stubTransport = require('nodemailer-stub-transport')()
var transport = Promise.promisifyAll(nodemailer.createTransport(stubTransport))

All 5 comments

nodemailer-smtp-transport is built-in to nodemailer (it's the default transport) so you should need to install it separately. I typically only promisify the sendMail method:

var nodemailer = require('nodemailer')
var P = require('bluebird')

var transport = nodemailer(smtpTransportOpts)
var sendMail = P.promisify(transport.sendMail, transport)

sendMail(mailOpts)
  .then(handleRes)
  .catch(handleErr)

However you can promisify all the methods by using P.promisifyAll like such:

var transport = P.promisifyAll(nodemailer(smtpTransportOpts))

By default, bluebird gives the new promise aware methods Async suffixes so you'd use sendMail like this:

tranport.sendMailAsync(mailOpts)
  .then(handleRes)
  .catch(handleErr)

I got it running. Thx for your hint.

@divramod can you please send the merge code.. i am facing the same issue

I was also having the same problem some time ago. The code above is no longer valid. You have to use this:

var nodemailer = require('nodemailer')
var transport = Promise.promisifyAll(nodemailer.createTransport({ /* options */ }))

Note that if you are trying to use a transport mechanism that is not built-in, there is a small difference. Instead of passing an options object to the createTransport method, you pass the external transport directly like so:

// Example using the stub transport
var stubTransport = require('nodemailer-stub-transport')()
var transport = Promise.promisifyAll(nodemailer.createTransport(stubTransport))

@ricardograca i want to send mail and i am using the following code

var nodemailer = require('nodemailer');
nodemailer.SMTP = {
host: 'mail.gmail.com',
port: 25,
use_authentication: true,
user: [email protected]',
pass: '*'
};

var message = {
sender: "[email protected]",
to:'[email protected]',
subject: 'Test Report'
};


and the below code should return promise because i am using stackify module to execute in sync.

nodemailer.send_mail(message,
function(err) {
if (!err) {
console.log('Email send ...');
} else console.log(sys.inspect(err));
});

Do you have any solution for this??

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chrisprobst picture chrisprobst  Â·  5Comments

wearhere picture wearhere  Â·  5Comments

julien-f picture julien-f  Â·  5Comments

SimonSchick picture SimonSchick  Â·  6Comments

rainabba picture rainabba  Â·  5Comments