NodeMailer排队发送电子邮件,但电子邮件从不发送

我正在尝试从我自己的域发送电子邮件,而不使用外部SMTP服务器.我正在使用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’s SMTP Server

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收件箱或任何其他电子邮件测试服务)…

没有错误的端口被阻止,所以我必须遗漏一些东西(?).
这不是如何正确使用NodeMailer的吗?
我是否可以使用NodeMailer从本地域发送电子邮件?

最佳答案
文档here有一条说明:

This module does not make any email deliveries by itself. smtp-server
allows you to listen on ports 25/24/465/587 etc. using SMTP or LMTP
protocol and that’s it. Your own application is responsible of
accepting and delivering the message to destination.

(强调我的)

您的服务器似乎接受了电子邮件(这就是它显示邮件已排队的原因)但它不会传递到目标.

有关如何在邮件到达SMTP服务器后发送邮件的扩展.如果TO地址是本地的,只需将邮件放入其邮箱即可.但是,如果您要“重新发送”邮件,则需要与邮件联系TO邮件交换.

就像是:

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

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

transporter.sendMail(outMessage);

转载注明原文:NodeMailer排队发送电子邮件,但电子邮件从不发送 - 代码日志