Created
April 5, 2018 14:35
-
-
Save smfreegard/f7a11a2245c3c176b20fe07755ffe7ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Same plugin using
transaction.relaying
instead: