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;
}
});
};
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??
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:
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
createTransportmethod, you pass the external transport directly like so: