NodeMailer queuing outgoing email, but email never sends

僤鯓⒐⒋嵵緔 提交于 2019-12-22 05:24:21

问题


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);
});

My server code using 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);
});

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)...

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?


回答1:


Documentation here has a note that states:

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.

(emphasis mine)

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.

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.

Something like:

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

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

transporter.sendMail(outMessage);


来源:https://stackoverflow.com/questions/53376807/nodemailer-queuing-outgoing-email-but-email-never-sends

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!