NodeMailer对传出电子邮件进行排队,但是电子邮件从不发送 [英] NodeMailer queuing outgoing email, but email never sends

查看:168
本文介绍了NodeMailer对传出电子邮件进行排队,但是电子邮件从不发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从自己的域发送电子邮件,而不使用外部SMTP服务器。我正在使用 NodeMailer的SMTP连接

I'm trying to send an email from my own domain without using an external SMTP server. I'm using NodeMailer's SMTP connection:

let options = {
    secure: true,
    port: consts.portOut,//465
    host: consts.host, //mydomain.com
    transactionLog: true,
    debug: true,
    requireTLS: true,
    authMethod: 'PLAIN',
};

let connection = new SMTPConnection(options);

connection.connect(function() {
    let auth = {
        user: 'abc',
        pass: 'def'
    };

    connection.login(auth, function(err) {
        if (err) {
            console.log("Authentication failed!", err);
        }
        console.log("Authentication to SMTP server successful.");

        let envelope = {
            from: 'fee@mydomain.com',
            to: 'myemail@gmail.com'
        };

        let message = 'message hello world';

        connection.send(envelope, message, function(err, info) {
            if (err) {
                console.log("err:::", err);
            } else {
                console.log('info?', info);
                //connection.quit();
            }
        });

        connection.quit();

    });

});

connection.on("error", function(err) {
    console.log(err);
});

我的服务器代码使用 NodeMailer的SMTP服务器

const options = {
    secure: true,
    size: 25000000, //25MB
    authMethods: ['PLAIN'],
    key: hskey,
    cert: hscert,
    ca: [hschain],
    onAuth(auth, session, callback) {
        if(auth.username !== 'abc' || auth.password !== 'def') {
            return callback(new Error('Invalid username or password'));
        }
        callback(null, {user: 123}); // where 123 is the user id or similar property
    },
    onConnect(session, callback) {
        console.log("the address is:", session.remoteAddress)
        if (session.remoteAddress === consts.ip) {
            return callback(); // Accept the address
        } else {
            return callback(new Error('Only connections from %s allowed', consts.ip));
        }

    },
    onData(stream, session, callback) {
        simpleParser(stream, (err, parsed) => {
            if(err) {
                console.error(err);
            } else {
                console.log(parsed);
            }

        });
        stream.on('end', function () {
            let err;
            if(stream.sizeExceeded){
                err = new Error('Message exceeds fixed maximum message size');
                err.responseCode = 552;
                return callback(err);
            }
            callback(null, 'Message queued as abcdef');
        });
    }

};

const emailServer = new SMTPServer(options);

emailServer.listen(consts.portOut, function () {
    processSMTPConnection(consts, hskey);
});

emailServer.on("error", function (err) {
    console.error("Error %s", err.message);
});

因此,在客户端连接到本地SMTP服务器之后,我收到的最后一条消息是 abcdef,并且什么也没有发送(什么也没有到达我的gmail收件箱或任何其他电子邮件测试服务)...

So after my client connects to my local SMTP server, the last message I get is 'Message queued as abcdef' and nothing ever sends (nothing ever arrives in my gmail inbox or any other email testing services)...

没有不正确的端口被阻止,因此我必须丢失东西(?)。
这不是如何正确使用NodeMailer吗?
我应该能够使用NodeMailer从本地域发送电子邮件吗?

No incorrect ports are blocked, so I must be missing something(?). Is this not how to correctly use NodeMailer? Should I be able to send emails from my local domain using NodeMailer?

推荐答案

文档此处有一条注释,指出:

Documentation here has a note that states:


此模块本身不会进行任何电子邮件传递。 smtp-server
允许您使用SMTP或LMTP
协议在端口25/24/465/587等上侦听,仅此而已。 您自己的应用程序负责
接受并将消息传递到目的地。

(强调我的)

您的服务器似乎接受了电子邮件(这就是为什么它显示消息已排队的原因),但没有传递到目的地。

Your server seems to accept the email (that's why it's showing that the message has been queued) but it doesn't delivers to destination.

扩展一些有关邮件到达SMTP服务器后如何发送的信息。如果收件人地址是本地地址,只需将邮件放在他们的邮箱中即可。但是,如果您想转寄该邮件,则需要将该邮件与TO邮件交换联系。

To expand a little bit on how to send the message once it arrives to your SMTP server. If the TO address is local, just put the message in their mailbox. But if you want to "remail" the message, you need to contact the TO mail exchange with the message.

类似以下内容:

const toExchange = getMX(parsed.to);
const outMessage = createMessageFromParsed(parsed);

const transporter = createTransport({
    port: 25,
    host: toExchange,
    name: os.hostname(),
});

transporter.sendMail(outMessage);

这篇关于NodeMailer对传出电子邮件进行排队,但是电子邮件从不发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆