email - NodeMailer 排队传出电子邮件,但电子邮件从不发送

标签 email smtp sendmail smtpclient 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);

关于email - NodeMailer 排队传出电子邮件,但电子邮件从不发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53376807/

相关文章:

java - 有没有办法以编程方式获取当前 java mail api 的版本?

email - Drupal Simplenews - 如何订阅所有现有用户?

c# - 使用 Mailkit : "The SMTP server has unexpectedly disconnected."

php - 发布 mysql 并发送电子邮件给用户注册

html - 删除段落之间的空格

iOS 如何在按下发送按钮时重置文本字段

linux - 无法从谷歌云虚拟机远程登录 smtp 服务器

windows - 设置本地 SMTP 和 POP3 以测试邮件发送和接收循环

email - mailx 更改发件人姓名

linux - Shell脚本使用sendmail嵌入多个图像