You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

  1. Navigate to your Haraka plugins directory (usually ./plugins/ in your Haraka installation folder).
  2. Create a new file named exclude_domains.js with 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

  1. Create a excludedomain file in your Haraka config/ directory.
  2. 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

  1. Open your Haraka config/plugins file.
  2. 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.com and third@third.com. The plugin will filter out the first@first.com recipient, so only third@third.com should 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.COM will be treated the same as first.com.
  • File Reload: If you update the excludedomain file, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:53:30