NodeJS Email Alias Server

2.4k views Asked by At

The scenario is simple:

  • I own several domains
  • I want to run email on those domains
  • I wantthem all to deliver to the same address regardless of who they where sent to or on which domain
  • I want them to retain the original headers specifically the to field

For this project i need use NodeJS

I have tried several things to no avail, I'm pretty proficient in node, but have no idea what I'm doing when it comes to emails.

I have all the domains pointed to the correct place, including the MX records, and using simplesmtp, i am able to receive the emails, i just can't figure out how to deliver them without trashing the headers or forwarding them in which case they show up at their destination as two emails.

Any suggestions or guidance is much appreciated

Regards, David

Since we all love code here is the relevant stuff

    var simplesmtp  = require("simplesmtp"),
        MailParser  = require("mailparser").MailParser,
        mailparser  = new MailParser(),
        nodemailer  = require("nodemailer"),
        gmail       = nodemailer.createTransport("SMTP",{
            service: "Gmail",
            auth: {
                user: "***",
                pass: "***"
            }
        }),
        fs          = require("fs"),
        smtp        = simplesmtp.createServer();

    // Setup the listener
    smtp.listen(25);

    // runs when the email is initially recieved
    smtp.on("startData", function(connection){
        // log out some basic stuff
        console.log("Message from:", connection.from);
        console.log("Message to:", connection.to);
        // start the write stream
        connection.saveStream = fs.createWriteStream("/path/message.txt");
    });

    // each chunk of data that is received 
    smtp.on("data", function(connection, chunk){
        // continue the write stream
        connection.saveStream.write(chunk);
    });

    // email completly received
    smtp.on("dataReady", function(connection, callback){
        // end the write stream
        connection.saveStream.end();
        // log some more stuff
        console.log("Incoming message saved to /path/message.txt");

        // start a readstream to forward out the message
        fs.createReadStream("/path/message.txt").pipe(mailparser);

        // 
        callback(null, "ABC1"); // ABC1 is the queue id to be advertised to the client
        // callback(new Error("Rejected as spam!")); // reported back to the client
    });

    // Parse the email
    mailparser.on("end", function(email){
        // now lets forward the mail
        // for now everything goes back to *****, 
        // eventually ill be setting up some configs to handle other addresses
    //  console.log(email.headers); */
    //  email.headers['X-Forwarded-To'] = "*****"; 
    //  email.to = "*****";
        delete email.headers.received;
        delete email.text;
    //  delete email.headers.X-Received; 
        email.to = email.to + ', ' + "*****";
        email.headers.to = email.to + ', ' + "*****";
        console.log(email.headers);

        gmail.sendMail(email, function(err, response){
            if(err)
                console.log(err);

            // now clean up that message file
            fs.rename('/path/message.txt', 'emails/'+new Date().toJSON()+email.subject+'.eml', function(err){
                if(err) console.log(err);
                fs.unlink('/path/message.txt', function(){console.log('clenaed up');});

            })

            // final logging
            console.log('sent');
        });
    });
1

There are 1 answers

0
mkopala On

You can set the envelope sender for the email using nodemailer so that it contains the forwarding address:

        email.envelope = {
            from: email.from
            to: "[email protected]"
        }

Some SMTP services will not let you set the envelope sender (MAIL FROM). You may have trouble with Gmail.

See also: