关于使用mutt实现特定主题邮件自动回复的技术问询
Absolutely, you can set up an automated reply system using mutt combined with basic shell scripting and email parsing tools. Here's a practical, step-by-step approach to make this work:
Step 1: Prepare your auto-reply template
First, create a plain text file with your standard reply content. Save it somewhere easy to access, like ~/auto_reply_template.txt:
Hi there, Thanks for reaching out! I’ll get back to you as soon as I can. Best regards, Your Name
Step 2: Write the automation script
Create a bash script (let’s call it ~/auto_reply_handler.sh) that scans your incoming emails, filters for matching subjects, and sends replies via mutt. This script uses formail (from the procmail package) to parse email headers cleanly:
#!/bin/bash # Configure these paths/settings to match your setup REPLY_TEMPLATE="$HOME/auto_reply_template.txt" # Match subjects starting with hello/hi/welcome (case-insensitive) MATCH_PATTERN="^hello|^hi|^welcome" MAIL_SPOOL="/var/mail/$USER" # Adjust if using Maildir instead of mbox TEMP_DIR="$HOME/.auto_reply_temp" # Create temp directory and lock file to avoid duplicate runs mkdir -p "$TEMP_DIR" LOCK_FILE="$TEMP_DIR/script.lock" if [ -f "$LOCK_FILE" ]; then echo "Script is already running. Exiting." exit 1 fi touch "$LOCK_FILE" # Process each email in the mail spool formail -s sh -c ' # Extract key email headers SUBJECT=$(formail -x Subject: <<< "$0") SENDER=$(formail -x From: <<< "$0" | sed -E "s/.*<([^>]+)>.*/\1/") MESSAGE_ID=$(formail -x Message-ID: <<< "$0" | tr -d "<>") # Check if subject matches our keywords (case-insensitive) if echo "$SUBJECT" | grep -qiE "'"$MATCH_PATTERN"'"; then # Avoid replying to the same email multiple times REPLIED_MARKER="$TEMP_DIR/replied_$MESSAGE_ID" if [ ! -f "$REPLIED_MARKER" ]; then # Send reply with mutt mutt -s "Re: $SUBJECT" "$SENDER" < "$REPLY_TEMPLATE" # Mark this email as replied to touch "$REPLIED_MARKER" echo "Sent auto-reply to $SENDER (Subject: $SUBJECT)" >> "$TEMP_DIR/reply_log.txt" fi fi ' < "$MAIL_SPOOL" # Clean up lock file rm "$LOCK_FILE"
Step 3: Make the script executable and test it
Run these commands to make the script usable and test it manually:
chmod +x ~/auto_reply_handler.sh ~/auto_reply_handler.sh
Check the log file at ~/.auto_reply_temp/reply_log.txt to confirm it worked.
Step 4: Set up periodic checks with cron
To make this run automatically, add a cron job to execute the script at regular intervals (e.g., every 5 minutes):
- Open your crontab editor:
crontab -e - Add this line (replace
your_usernamewith your actual username):
*/5 * * * * /home/your_username/auto_reply_handler.sh >> /home/your_username/auto_reply_cron_log.txt 2>&1
- Save and exit the editor. The cron job will now run every 5 minutes.
Important Notes:
- Dependencies: Install required tools if you don’t have them:
On Debian/Ubuntu:sudo apt install mutt procmail
On RHEL/CentOS:sudo dnf install mutt procmail - Maildir Adjustment: If you use Maildir instead of mbox, replace the
MAIL_SPOOLline with a loop through~/Maildir/new/*files instead of reading from/var/mail/$USER. - Loop Prevention: The script uses message IDs to avoid replying to the same email multiple times, which prevents infinite loops if your auto-reply gets sent back to you.
备注:内容来源于stack exchange,提问作者Damien

