Nodemailer慢慢发送邮件

时间:2015-05-25 02:00:54

标签: node.js send nodemailer

使用nodemailer-pool发送电子邮件时遇到问题。

我为每个发件人电子邮件创建了一个池,然后启动我的程序,以便从Rabbit mq获取信息时发送电子邮件。

起初,它运作良好,但在几天后(可能是3天),发送邮件变得非常慢,我是否应该采取措施解决此类问题?

我的代码如下。

var nodemailer = require('nodemailer');
var smtpPool = require('nodemailer-smtp-pool');
var senderConfig = require('../../utils/senderConfig');

的Utils:

var sender1 = senderConfig.smtp_server_list["sender1"];
var sender1Tp = createTransporter(sender1);

var sender2 = senderConfig.smtp_server_list["sender2"];
var sender2Tp = createTransporter(sender2);

var sender3 = senderConfig.smtp_server_list["sender3"];
var sender3Tp = createTransporter(sender3);

var sender4 = senderConfig.smtp_server_list["sender4"];
var sender4Tp = createTransporter(sender4);

var sender5 = senderConfig.smtp_server_list["sender5"];
var sender5Tp = createTransporter(sender5);


function generateBounceAddress(recipient, sender, len) {
    var baseStr = "abcdefghijklmnopqrstuvwxyz";
    var bsaeStrLen = baseStr.length;
    var bounceAddress = "bounce+";
    for (var i=0; i<len; i++) bounceAddress += baseStr.charAt(Math.random() * bsaeStrLen);

    bounceAddress += "-" + recipient.replace("@","=") + "@" + sender.slice(sender.indexOf("@")+1);

    return bounceAddress;
}


function createTransporter(sender) {
    console.log("createTransporter: ", sender.host);

    var transporter = nodemailer.createTransport(smtpPool({
        host: sender.host,
        port: sender.port,
        authMethod: sender.authMethod,
        auth: {
            user: sender.user,
            pass: sender.pass
        },
        maxConnections: 3,
        maxMessages: 500
    }));

    return transporter;
}
module.exports = {
    sendOneMail : function(email, callback) {
        var sender = senderConfig.smtp_server_list[email.sender];
        // if (!transporterPool[sender.user]) transporter = createTransporterPool(sender);
        // else transporter = transporterPool[sender.user];
        // messagePool[sender.user]--;

        var bounceAddress;
        var mailOptions = {};
        var senderUser = (sender.name) ? sender.name+" <"+sender.user+">" : sender.user;
        if (email.senderName) senderUser = email.senderName+" <"+sender.user+">";

        if (email.recipient.indexOf(",") >= 0) {
            mailOptions = {
                from: senderUser,
                to: email.recipient,
                subject: email.subject,
                html: email.content
            };
        }
        else {
            bounceAddress = generateBounceAddress(email.recipient, sender.user, 5);

            mailOptions = {
                from: senderUser,
                to: email.recipient,
                envelope: {
                    from: bounceAddress,
                    to: email.recipient
                },
                headers: {
                    'X-RETURN-PATH': bounceAddress
                },
                subject: email.subject,
                html: email.content
            };
        }

    // 美国
        if (email.sender === "sender1") sender1Tp.sendMail(mailOptions, callback);
        if (email.sender === "sender2") sender2Tp.sendMail(mailOptions, callback);
        if (email.sender === "sender3") sender3Tp.sendMail(mailOptions, callback);
        if (email.sender === "sender4") sender4Tp.sendMail(mailOptions, callback);
        if (email.sender === "sender5") sender5Tp.sendMail(mailOptions, callback);

    }
}

0 个答案:

没有答案