
Introduction
Email forwarding is an essential task for system administrators and email power users. With Procmail, you can automate email filtering and forwarding efficiently on Ubuntu. This guide will walk you through everything you need to know to set up and optimize Procmail for mail forwarding.
What is Procmail?
Procmail is a mail processing utility for Unix-based systems, including Ubuntu. It filters, sorts, and forwards emails based on user-defined rules called recipes. Procmail is widely used for:
- Automatic email sorting
- Spam filtering
- Email forwarding to multiple addresses
- Running scripts based on incoming emails
Why Use Procmail for Mail Forwarding?
Using Procmail for mail forwarding offers several benefits:
- Flexibility: You can define multiple conditions and actions.
- Automation: Once set up, Procmail handles emails without manual intervention.
- Customization: Supports regex-based filtering and scripting.
- Integration: Works seamlessly with other mail servers like Postfix and Sendmail.
Prerequisites
Before setting up Procmail, ensure you have the following:
- An Ubuntu server or desktop (version 18.04, 20.04, or later)
- A configured mail server (Postfix, Sendmail, or Exim)
- Basic knowledge of the command line
- Administrative or sudo privileges
Installing Procmail on Ubuntu
Procmail is available in Ubuntu’s default repositories. Install it using:
sudo apt update
sudo apt install procmailVerify installation:
procmail -vConfigure Procmail for Mail Forwarding
Step 1: Create .procmailrc File
Each user who wants to set up mail forwarding must create a .procmailrc configuration file in their home directory. Navigate to the user’s home directory and create the file:
cd ~
nano .procmailrcStep 2: Set Up Basic Procmail Configuration
Add the following lines to the .procmailrc file:
SHELL=/bin/bash
MAILDIR=$HOME/Maildir/
LOGFILE=$HOME/.procmail.log
DEFAULT=$MAILDIR
VERBOSE=yesStep 3: Configure Mail Forwarding Rules
To forward emails to another address, use the following rule:
:0
* ^TO_myemail@example.com
! forwardemail@example.comExplanation:
:0starts the rule definition.^TO_myemail@example.commatches incoming emails sent tomyemail@example.com.! forwardemail@example.comforwards the matched email toforwardemail@example.com.
Step 4: Enable Global Procmail Forwarding (Optional)
For system-wide mail forwarding, configure Procmail as the default MTA mail delivery agent by editing the Postfix configuration file:
sudo nano /etc/postfix/main.cfAdd or modify the following line:
mailbox_command = /usr/bin/procmailRestart Postfix to apply changes:
sudo systemctl restart postfixTesting Mail Forwarding
Send a test email to verify that mail forwarding works as expected:
echo "Test email" | mail -s "Test Subject" myemail@example.comCheck the recipient’s inbox to confirm the forwarded email.
Advanced Mail Forwarding Rules
Forward emails only if they contain specific keywords:
:0
* ^Subject:.*Important
! important@example.comAutomating Procmail with Cron Jobs
Run a script daily to clean old emails:
crontab -eAdd following code to configure corn job as per your convinince to run the job.
0 2 * * * find ~/Mail -type f -mtime +30 -deleteAlternative Methods for Mail Forwarding
- Postfix Aliases: Modify /etc/aliases
- Gmail Forwarding: Use built-in filters
1- Can I forward emails to multiple addresses?
Yes, separate addresses with a comma:
! email1@example.com, email2@example.com2- How do I log Procmail actions?
Enable logging in .procmailrc:
LOGFILE=$HOME/.procmail.log
VERBOSE=yes3- Can Procmail filter spam?
Yes, use recipes to detect spam keywords.
4- How do I disable Procmail temporarily?
Rename .procmailrc
mv ~/.procmailrc ~/.procmailrc.bak5- What happens if Procmail fails?
Emails are delivered to the default mailbox.
6- Does Procmail work with cloud-based email providers?
Only if they support forwarding via SMTP/IMAP.
Setting up mail forwarding in Procmail on Ubuntu is a straightforward process that enhances email management capabilities. By configuring Procmail with custom rules, users can efficiently filter and forward emails to desired destinations.