如何配置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: Thecflag 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 thec.- The first
* ! ^From:...line: This skips senders from domains you already recognize (replaceyour-familiar-domain.comwith 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 anX-Loopheader to emails we process, so Procmail won’t re-run this rule on the same email again. - The
FROM=line: Usesformailto extract the sender’s email address from theFrom:header, thensedcleans up characters that aren’t allowed in folder names (like spaces, slashes, or colons) by replacing them with underscores. - The
mkdir -pline: Checks if the folder exists; if not, creates it (the-pflag ensures any parent directories are created too, though you probably won’t need that here). - The final delivery block: Adds the
X-Loopheader, 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(orMail) 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
sedcommand in theFROM=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

