Procmail转发邮件插入文本遇问题:日志报错与位置异常求助
Let's break down the issues with your script and fix them step by step:
1. The "procmail: Missing action" Error
Your original script has two syntax missteps triggering this error:
- You had a standalone
:0 * ^Subject:.*weatherrule with no associated action. Procmail requires every:0rule block to have a clear action (like filtering, saving, or forwarding). - Your pipe command wasn't wrapped in shell braces, so Procmail misinterpreted the separate
echoandcatcommands as invalid, unassociated actions.
2. Text Injection in the Wrong Position
Since your incoming email is a multipart MIME message, using the bfw flag filters the entire raw email content—including MIME headers. That’s why your injected text ended up above the MIME body instead of the actual message content. We need to target only the text/plain body part to insert your text properly.
Corrected Procmail Script
Here's the fixed version that resolves both issues:
SHELL=/bin/bash VERBOSE=off LOGFILE=/home/test/.procmail.log LOGDATE_=`/bin/date +%Y-%m-%d` :0 * ^Subject:.*weather { # Inject text at the start of the text/plain body part :0 fhw | sed '/^Content-Type: text\/plain;/,/^$/ { /^$/!b i\ This is injected text\ }' # Forward the modified email (keep a copy with the 'c' flag) :0 c ! mymail@somedomain.com }
What Each Part Does:
- Branch Block
{}: Wraps all actions for the subject-matching rule, eliminating the "Missing action" error by ensuring every:0has a defined purpose. - Sed Filter: The
sedcommand locates thetext/plainMIME part header, waits until the end of the header (blank line), then inserts your text right before the actual message content. - Forward Rule: The
:0 crule forwards the modified email to your target address, withcpreserving a copy of the modified email in your inbox (removecif you don't want a local copy).
Verification
After updating your script, send a test email with "weather" in the subject. Check your .procmail.log for errors, and verify the forwarded email has your injected text at the top of the message body, not above the MIME headers.
内容的提问来源于stack exchange,提问作者Marek MW

