Classify Email with Fetchmail, Procmail and Postfix

Written by
Date: 2014-09-07 16:30:35 00:00

Connecting Email Accounts Using Fetchmail and Postfix

I know that most email providers have to option to connect accounts, so your main account can collect emails from all others using POP protocol, so you can have all your email concentrated in just one account.

That is not the way I do it, I use Fetchmail and Postfix on an Debian server to do the job. I know there is no reason to do it that way, but as people likes to play soccer I like to work with servers (And play soccer).

Here is what you need:

  • A Linux server that is online all time (I prefer Ubuntu or Debian)
  • Fetchmail install on that server to collect emails
  • Postfix to send collected emails to your main account

I am using a Debian server on Digital Ocean (affiliate link)

Fetchmail Setup

These are the contents on ~/.fetchmailrc

set no bouncemail
defaults:
  antispam -1 
  batchlimit 100
poll pop.gmail.com port 995 with protocol pop3
user account1@domain password password is user
ssl
sslproto SSL3
fetchall
keep
no rewrite
mda "/usr/bin/procmail -f %F -d %T";

poll pop.gmail.com port 995 with protocol pop3
user account2@gmail.com password passed is user
ssl
sslproto SSL3
fetchall
keep
no rewrite
mda "/usr/bin/procmail -f %F -d %T";

As you can see I am polling email from a Google Apps account and a Gmail account, and passing them to procmail

Procmail Setup

These are the contents of ~/.procmailrc

:0
	* .*
{
    #:0 c
    #$DEFAULT

    :0 
    !user@domain
}

This just instructs Procmail to send all collected email to the user@domain address. You can of course uncomment the two commented lines to have a local copy of all collected emails. I do not want that. Check out this great Procmail Guide

Postfix Setup

Finally, somebody has to take care of sending all collected emails to the main account. That is going to be postfix. Debian comes with Exim as default, but as soon as you install Postfix, Exim is uninstalled.

apt-get install postfix

And the /etc/postfix/main.cf file has this:

alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
append_dot_mydomain = no
biff = no
config_directory = /etc/postfix
home_mailbox = Maildir/
inet_interfaces = all
mailbox_command =
mailbox_size_limit = 0
mydestination = your_domain, wordpress, localhost.localdomain, localhost
myhostname = servername.domain
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
myorigin = your_domain_here
readme_directory = no
recipient_delimiter = +
relay_domains = localhost
relayhost =
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
smtpd_helo_restrictions = reject_invalid_helo_hostname, reject_non_fqdn_helo_hostname, reject_unknown_helo_hostname
smtpd_recipient_restrictions = reject_invalid_hostname, reject_unknown_recipient_domain, reject_unauth_destination, reject_rbl_client sbl.spamhaus.org, permit
smtpd_tls_cert_file = /etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file = /etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_session_cache_database = btree:${data_directory}	smtpd_scache
smtpd_use_tls = yes

With this configuration, it will send emails by itself, I mean it is not configured to send emails through a SmartHost.

If that is what you want you can read here for Postfix and Here for Exim or Sendmail