用蓝鸟宣传Nodemailer?

时间:2015-06-20 02:44:42

标签: javascript node.js promise bluebird nodemailer

nodemailer author已明确表示他不支持承诺。我以为我会尝试使用蓝鸟,但我对它的尝试似乎并没有发现Nodemailer抛出的任何错误:

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

// build the transport with promises
var transport = Promise.promisifyAll( nodemailer.createTransport({...}) );

module.exports = {
    doit = function() {
        // Use bluebird Async
        return transport.sendMailAsync({...});
    }
}

然后我这样做:

doit().then(function() {
    console.log("success!");
}).catch(function(err) {
    console.log("There has been an error");
});

但是,在提供无效的电子邮件时,我会看到:

Unhandled rejection Error: Can't send mail - all recipients were rejected

因此,我的蓝鸟承诺没有捕获到网络邮件错误。我做错了什么?

5 个答案:

答案 0 :(得分:2)

不能说出我头顶的代码有什么问题。我遇到了与promisification类似的问题,并决定手动宣传问题案例更容易。这不是最优雅的解决方案,但它是一种解决方案。

var transport = nodemailer.createTransport({...});

module.exports = {
    doit: function() {
        return new Promise(function (res, rej) {
           transport.sendMail({...}, function cb(err, data) {
                if(err) rej(err)
                else res(data)
            });
        });
    }
}

答案 1 :(得分:1)

这是我如何运作的(打字稿,蓝鸟&#4承诺,nodemailer-smtp-transport):

export const SendEmail = (from:string,
                          to:string[],
                          subject:string,
                          text:string,
                          html:string) => {

    const transportOptions = smtpConfiguration; // Defined elsewhere

    const transporter = nodemailer.createTransport(smtpTransport(transportOptions));

    const emailOptions = {
        from: from,
        to: to.join(','),
        subject: subject,
        text: text,
        html: html
    };

    return new Promise((resolve, reject) => {
        transporter.sendMail(emailOptions, (err, data) => {
            if (err) {
                return reject(err);
            } else {
                return resolve(data);
            }
        });
    });
};

答案 2 :(得分:0)

您可以尝试使用.fromNode()创建动态Promise吗?

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

module.exports = {
    doit = function() {            
        return Promise.fromNode(function(callback) {
            transport.sendMail({...}), callback);
        }
    }
}

doit()将返回蓝鸟承诺。您可以找到.fromNode() here的文档。

答案 3 :(得分:0)

如果您只想Promisify .sendMail方法,可以这样做:

const transport = nodemailer.createTransport({
  host: mailConfig.host,
  port: mailConfig.port,
  secure: true,
  auth: {
    user: mailConfig.username,
    pass: mailConfig.password,
  },
});

const sendMail = Promise.promisify(transport.sendMail, { context: transport });

并像这样使用它:

const mailOptions = {
  from: ...,
  to: ...,
  subject: ...,
  html: ...,
  text: ...,
};

sendMail(mailOptions)
  .then(..)
  .catch(..);

或者如果您使用包含在异步函数中的async/await

./mail.js

const send = async (options) => {
  ...

  return sendMail(options);
};

export default { send };

使用时await

./testing.js

import mail from './mail';

await mail.sendMail(options);

你明白了。

答案 4 :(得分:0)

现在,如果你只是省略回调,它会返回一个承诺。

示例

const nodemailer = require('nodemailer')

const transport = nodemailer.createTransport({
    host: 'smtp_host',
    port: 'smpt_port',
    auth: {
        user: 'smpt_auth_user',
        pass: 'smpt_auth_password',
    },
})

const promise = transport.sendMail({
    from: '1@test.com',
    to: '2@test.com',
    text: 'Hello world!',
    subject: 'Test'
})

console.log(promise)

上面的这个日志将返回 Promise { <pending> }