petkaantonov / bluebird

:bird: :zap: Bluebird is a full featured promise library with unmatched performance.
http://bluebirdjs.com
MIT License
20.45k stars 2.33k forks source link

promisify nodemailer-smtp-transport #333

Closed divramod closed 10 years ago

divramod commented 10 years ago

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 ✔ <foo@blurdybloop.com>', // sender address
      to: 'email@email.com', // 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;
      }
  });

};

wavded commented 10 years ago

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)
divramod commented 10 years ago

I got it running. Thx for your hint.

amaanmemon commented 9 years ago

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

ricardograca commented 9 years ago

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))
amaanmemon commented 9 years ago

@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: my@example.com', pass: '***' };

var message = {
sender: "my@example.com",
to:'you@example.com',
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??