Skip to content

Instantly share code, notes, and snippets.

@smfreegard
Created April 5, 2018 14:35
Show Gist options
  • Save smfreegard/f7a11a2245c3c176b20fe07755ffe7ff to your computer and use it in GitHub Desktop.
Save smfreegard/f7a11a2245c3c176b20fe07755ffe7ff to your computer and use it in GitHub Desktop.
const Address = require('address-rfc2821').Address;
exports.hook_rcpt = function (next, conn, rcpt) {
const txn = conn.transaction;
const cfg = this.config.get('forwarder.ini');
conn.loginfo(this, JSON.stringify(cfg));
if (rcpt && rcpt[0] && rcpt[0].host) {
var domain = rcpt[0].host.toLowerCase();
if (cfg.main[domain]) {
conn.loginfo(this, 'Found domain ' + domain + ' forwarding to ' + cfg.main[domain]);
txn.notes.forwarding_relay = true;
conn.relaying = true;
// Replace the envelope sender otherwise we'll have issues with SPF
txn.mail_from = new Address('forwarding@' + domain);
// Replace the recipient with the forwarding address
txn.rcpt_to = [];
txn.rcpt_to.push(new Address(cfg.main[domain]));
return next(OK);
}
}
return next(DENY, 'Unknown address: ' + rcpt[0]);
}
exports.hook_reset_transaction = function (next, conn) {
const txn = conn.transaction;
// Remove relay permissions if we added them
if (txn.notes.forwarding_relay && conn.relaying) {
conn.relaying = false;
}
return next();
}
@smfreegard
Copy link
Author

Same plugin using transaction.relaying instead:

const Address = require('address-rfc2821').Address;

exports.hook_rcpt = function (next, conn, rcpt) {
    const txn = conn.transaction;
    const cfg = this.config.get('forwarder.ini');
    conn.loginfo(this, JSON.stringify(cfg));
    if (rcpt && rcpt[0] && rcpt[0].host) {
        var domain = rcpt[0].host.toLowerCase();
        if (cfg.main[domain]) {
            conn.loginfo(this, 'Found domain ' + domain + ' forwarding to ' + cfg.main[domain]);
            txn.relaying = true;
            // Replace the envelope sender otherwise we'll have issues with SPF
            txn.mail_from = new Address('forwarding@' + domain);
            // Replace the recipient with the forwarding address
            txn.rcpt_to = [];
            txn.rcpt_to.push(new Address(cfg.main[domain]));
            return next(OK);
        }
    }
    return next(DENY, 'Unknown address: ' + rcpt[0]);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment