nodemailer / smtp-server

Create custom SMTP servers on the fly
Other
846 stars 146 forks source link

Usage...help please #82

Closed SPlatten closed 7 years ago

SPlatten commented 7 years ago

I would like to use this module, my code looks like this:

    try{
        const defs       = require("./svr/defs.js");
        const fs     = require("fs");
        const os         = require("os");
        const SMTPServer = require("smtp-server").SMTPServer;
        const server     = new SMTPServer({
            onData(stream, session, callback) {
                stream.pipe(process.stdout); // print message to console
                stream.on('end', () => {
    // reject every other recipient
                    let response = session.envelope.rcptTo.map((rcpt, i) => {
                        if (i % 2) {
                            return new Error('<' + rcpt.address + '> Not accepted');
                        } else {
                            return '<' + rcpt.address + '> Accepted';
                        }
                    });
                    if ( typeof callback == "function" ) {
                        callback(null, response);
                    }
                });
            },onMailFrom(address, session, callback) {
                console.dir(address);
                console.dir(session);

                if ( typeof callback == "function" ) {
                    console.dir(callback);
                    return callback(); // Accept the address
                }
            },onRcptTo(address, session, callback) {
                console.log(address);
                console.log(session);

                if ( typeof callback == "function" ) {
                    console.dir(callback);
                    return callback();
                }
            }
        });
    /*Find the external I/P address for this system*/
        var objNIs = os.networkInterfaces(), strServerHost;
        for( var strName in objNIs ) {
            var aryIFace = objNIs[strName];
            for( var i=0; i<aryIFace.length; i++ ) {
                var objNI = aryIFace[i];
                if ( objNI['internal'] == false && "IPv4".match(objNI['family']) ) { 
                    strServerHost = objNI['address'];
                    break;
                }
            }
            if ( strServerHost !== undefined ) {
                break;
            }
        }
        if ( strServerHost === undefined ) {
    /*If we couldn't determine the external I/P address use the local address*/
            strServerHost = "";
        } 
    /*Start listening for client connections*/
        server.listen(465, strServerHost);
    } catch( ex ) {
        console.log(ex);
    }

I've setup a domain name on dynamic DNS which points to my system, previous I have used Haraka and received emails I've sent to my system. I'm sending emails to the same domain, this time using the above code, but I'm obviously missing something as I don't see any activity.

Is there a working example or please could I have some help on what I need to do ?

Thank you,

andris9 commented 7 years ago

What do you expect that should happen?

SPlatten commented 7 years ago

I was hoping that the email would be received and some of the console messages would be displayed as a result of the callbacks, but I realise I may have missed something, I'm hoping for some guidance to get me started.

andris9 commented 7 years ago

If you want to receive messages then you should listen on port 25 not 465 which is a submission port

SPlatten commented 7 years ago

Ok, modified the code to port 25, still nothing, I got a message failure received on my own system...but nothing displayed in the console.

The bounced email included:

Reason: There was an error while attempting to deliver your message with [Subject: "test"] to domain. MTA p3plsmtpa08-03.prod.phx3.secureserver.net received this response from the destination host IP - ##.##.##.## - 530 , 530 Error: authentication Required

andris9 commented 7 years ago

You need to disable authentication. See this example