How to Set Up Mail Forwarding in Procmail on Ubuntu? [Step-by-Step Guide]

Introduction

What is Procmail?

  • Automatic email sorting
  • Spam filtering
  • Email forwarding to multiple addresses
  • Running scripts based on incoming emails

Why Use Procmail for Mail Forwarding?

  • 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

  • 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

sudo apt update
sudo apt install procmail

Verify installation:

procmail -v

Configure Procmail for Mail Forwarding

Step 1: Create .procmailrc File

cd ~
nano .procmailrc

Step 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=yes

Step 3: Configure Mail Forwarding Rules

To forward emails to another address, use the following rule:

:0
* ^TO_myemail@example.com
! forwardemail@example.com
  • :0 starts the rule definition.
  • ^TO_myemail@example.com matches incoming emails sent to myemail@example.com.
  • ! forwardemail@example.com forwards the matched email to forwardemail@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.cf

Add or modify the following line:

mailbox_command = /usr/bin/procmail

Restart Postfix to apply changes:

sudo systemctl restart postfix

Testing Mail Forwarding

Send a test email to verify that mail forwarding works as expected:

echo "Test email" | mail -s "Test Subject" myemail@example.com

Check 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.com

Automating Procmail with Cron Jobs

Run a script daily to clean old emails:

crontab -e

Add following code to configure corn job as per your convinince to run the job.

0 2 * * * find ~/Mail -type f -mtime +30 -delete

Alternative Methods for Mail Forwarding

FAQs

1- Can I forward emails to multiple addresses?

Yes, separate addresses with a comma:

! email1@example.com, email2@example.com

2- How do I log Procmail actions?

Enable logging in .procmailrc:

LOGFILE=$HOME/.procmail.log
VERBOSE=yes

3- Can Procmail filter spam?

Yes, use recipes to detect spam keywords.

4- How do I disable Procmail temporarily?

Rename .procmailrc

mv ~/.procmailrc ~/.procmailrc.bak

5- What happens if Procmail fails?

6- Does Procmail work with cloud-based email providers?

Conclusion

Read Also

How to Set Up Mail Forwarding in Postfix on Linux – A Complete Guide

Set Up Mail Forwarding in Sendmail on Linux?

Previous Post
Next Post