Haraka邮件服务器:是否可排除指定收件人域名?
How to Exclude Specific Recipient Domains in Haraka
Absolutely, you can implement this domain exclusion requirement in Haraka—its flexible plugin architecture makes custom rules like this straightforward. Here's how to build exactly what you're asking for:
Step 1: Create a Custom Haraka Plugin
Haraka relies on plugins for almost all its functionality, so we'll write a simple plugin to handle domain exclusion.
- Navigate to your Haraka plugins directory (usually
./plugins/in your Haraka installation folder). - Create a new file named
exclude_domains.jswith the following code:
const fs = require('fs'); const path = require('path'); // Path to your excludedomain file - adjust this to match your actual file location const EXCLUDE_FILE = path.join(__dirname, '../config/excludedomain'); // Load excluded domains once on plugin init let excludedDomains = new Set(); exports.register = function () { this.load_excluded_domains(); }; exports.load_excluded_domains = function () { try { const content = fs.readFileSync(EXCLUDE_FILE, 'utf8'); // Split by semicolons or newlines, trim whitespace, and filter out empty entries excludedDomains = new Set( content.split(/[;\n]/) .map(domain => domain.trim().toLowerCase()) .filter(domain => domain) ); this.loginfo(`Loaded ${excludedDomains.size} excluded domains`); } catch (err) { this.logerror(`Failed to load excluded domains file: ${err.message}`); } }; // Hook into the queue_before stage to filter recipients before queuing exports.hook_queue_before = function (next, connection) { const txn = connection.transaction; const validRecipients = []; for (const rcpt of txn.rcpt_to) { const recipientDomain = rcpt.host.toLowerCase(); if (!excludedDomains.has(recipientDomain)) { validRecipients.push(rcpt); } else { this.loginfo(`Rejected recipient ${rcpt.address} (domain ${recipientDomain} is excluded)`); } } // Update the transaction's recipient list to only include valid ones txn.rcpt_to = validRecipients; // If no valid recipients left, reject the transaction if (txn.rcpt_to.length === 0) { return next(DENY, 'No valid recipients (all domains are excluded)'); } next(); };
Step 2: Set Up Your Exclusion List
- Create a
excludedomainfile in your Harakaconfig/directory. - Add your excluded domains, either separated by semicolons or newlines (the plugin handles both):
first.com;second.com third-bad.com
Step 3: Enable the Plugin
- Open your Haraka
config/pluginsfile. - Add the following line to enable your new plugin (place it early enough in the list to ensure it runs before queuing):
exclude_domains
Step 4: Test the Setup
- Send a test email to
first@first.comandthird@third.com. The plugin will filter out thefirst@first.comrecipient, so onlythird@third.comshould receive the email. - Check Haraka's logs to verify the exclusion is working (look for lines like
Rejected recipient first@first.com (domain first.com is excluded)).
Notes:
- Case Insensitivity: The plugin converts domains to lowercase, so
First.COMwill be treated the same asfirst.com. - File Reload: If you update the
excludedomainfile, you'll need to restart Haraka or use Haraka's plugin reload command to pick up changes. - Error Handling: The plugin logs errors if it can't read the exclusion file, so you'll know if there's a path issue.
内容的提问来源于stack exchange,提问作者edit2112
相关产品推荐
相关产品推荐

