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

如何配置Procmail自动创建文件夹存储新发件人邮件?

Auto-Creating Folders for New Senders with Procmail

Absolutely, you can set up Procmail to automatically create local folders for new senders—let’s break this down with clear, easy-to-understand rules, no confusing obscure syntax required.

First, let’s assume you’re using Maildir format (the most common modern setup where each folder is a directory with cur/new/tmp subfolders; I’ll note tweaks for mbox format at the end). Here’s a complete, tested rule set you can add to your .procmailrc:

# Rule: Auto-create folder for unknown senders
:0 c
# Optional: Skip senders from domains you already know (remove if you don't want this)
* ! ^From:.*@your-familiar-domain\.com
# Prevent loopback: don't reprocess emails we've already handled
* ! ^X-Loop: auto-folder@your-personal-domain.com
{
    # Extract the sender's full email address, clean up invalid folder characters
    FROM=`formail -xFrom: | sed -e 's/.*<//' -e 's/>.*//' -e 's/[ /:]/_/g'`
    
    # Alternative: Use only the local part of the email (before @) instead of full address
    # FROM=`formail -xFrom: | sed -e 's/.*<//' -e 's/>.*//' -e 's/@.*//' -e 's/[ /:]/_/g'`
    
    # Create the folder if it doesn't exist
    :0
    * ? test ! -d "$HOME/Maildir/$FROM"
    | mkdir -p "$HOME/Maildir/$FROM"
    
    # Deliver the email to the new folder, add a loop header to avoid repeats
    :0
    * ? test -d "$HOME/Maildir/$FROM"
    {
        X-Loop: auto-folder@your-personal-domain.com
        :0
        $HOME/Maildir/$FROM/
    }
}

Let’s break down what each part does:

  • :0 c: The c flag tells Procmail to copy the email to the new folder (leaving the original in your inbox). If you want to move the email instead of copying, just remove the c.
  • The first * ! ^From:... line: This skips senders from domains you already recognize (replace your-familiar-domain.com with your own, or delete this line if you want auto-folders for all new senders).
  • The * ! ^X-Loop:... line: This prevents infinite loops. We add an X-Loop header to emails we process, so Procmail won’t re-run this rule on the same email again.
  • The FROM= line: Uses formail to extract the sender’s email address from the From: header, then sed cleans up characters that aren’t allowed in folder names (like spaces, slashes, or colons) by replacing them with underscores.
  • The mkdir -p line: Checks if the folder exists; if not, creates it (the -p flag ensures any parent directories are created too, though you probably won’t need that here).
  • The final delivery block: Adds the X-Loop header, then sends the email to the newly created folder.

If you’re using mbox format instead of Maildir:

Mbox stores each folder as a single file (not a directory). Adjust the rule like this:

# For mbox format
:0 c
* ! ^From:.*@your-familiar-domain\.com
* ! ^X-Loop: auto-folder@your-personal-domain.com
{
    FROM=`formail -xFrom: | sed -e 's/.*<//' -e 's/>.*//' -e 's/[ /:]/_/g'`
    MBOX="$HOME/Mail/$FROM"
    
    # Create the mbox file if it doesn't exist
    :0
    * ? test ! -f "$MBOX"
    | touch "$MBOX"
    
    :0
    * ? test -f "$MBOX"
    {
        X-Loop: auto-folder@your-personal-domain.com
        :0 >> $MBOX
    }
}

Testing the rule:

To make sure it works without waiting for a real new sender, create a test email file (e.g., test-email.txt) with content like:

From: new-sender@unknown-domain.com
Subject: Test Auto-Folder

This is a test email from a new sender.

Then run Procmail in verbose mode to see the process:

procmail -v -m ~/.procmailrc < test-email.txt

You should see the folder get created, and the email delivered to it.

Quick notes:

  • Make sure Procmail has permission to create folders/files in your Maildir (or Mail) directory—this is usually fine since it’s in your home folder.
  • If you want to tweak how folders are named (e.g., use the sender’s real name instead of email), adjust the sed command in the FROM= line. For example, to use the display name:
    FROM=`formail -xFrom: | sed -e 's/<.*//' -e 's/[ /:]/_/g'`
    

Hope this solves your problem—let me know if you need help tweaking it further!

内容的提问来源于stack exchange,提问作者someuser

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:13:07