NodeMailer queuing outgoing email, but email never sends
Asked Answered
G

1

7

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: '[email protected]',
            to: '[email protected]'
        };

        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?

Gailey answered 19/11, 2018 at 14:32 Comment(3)
What is console.log(\parsed) supposed to do? I get an uncaught SyntaxError when I run it in a repl.Trapezohedron
@Trapezohedron thanks for taking a look. it prints out the email stream from my SMTP connection file which encludes what you see in envelope and message aboveGailey
@Trapezohedron the backslash shouldn't be there and isn't in my actual code, it must have slipped in when I was cleaning things up to post on SOGailey
D
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);
Desirous answered 6/12, 2018 at 14:28 Comment(2)
Hmm interesting, you are correct it does accept messages both from my client and other email servers. If NodeMailer doesn't send mail, what service out there does?Gailey
nodemailer can send the message. See createTransport (nodemailer.com/usage).Desirous

© 2022 - 2024 — McMap. All rights reserved.