vithalreddy/node-mailin

How to reject an incoming email?

BetaHuhn opened this issue · 2 comments

Is there a way to reject an incoming email?
For example if someone sends an email to the server you could check if it was send to foo@bar.com and if not, you could send an error like the one outlook sends if an email adress doesn't exist (550 5.1.10 RESOLVER.ADR.RecipientNotFound;)
~ Maxi

as of now, there is no api to reject email based on custom logic,
But you could do something like this,

nodeMailin.on("message", function(connection, data, content) { console.log(data); // process or ignore if email is not found in your db });

I got it to work by changing the function onRcptTo() in the node-mailin.js from this

function onRcptTo(address, session, streamCallback) {
let ack = function(err) {
streamCallback(err);
};
//_this.emit("validateRecipient", session, address.address, callback);
validateAddress("recipient", address.address, session.envelope)
.then(ack)
.catch(ack);
}

to only this:

function onRcptTo(address, session, streamCallback) {
        _this.emit("validateRecipient", session, address.address, streamCallback)
}

Then you can use this listener

nodeMailin.on('validateRecipient', async function(session, address, callback) {
   if(address == 'foo@bar.com'){
        callback();
    }else{
        callback(
           new Error("email adress not found on server")
         );
    }
});

to check if the recipient is valid and if not the email gets rejected instead of just not processed further