forwarding
Filtering Spam before Forwarding Email with Postfix/SpamAssassin
One feature many cPanel/Shared Webhosts has is an option to forward your email to a different address. Very useful if you want to have multiple email addresses but check it all in one place (Gmail) like I do. But if you’re like me, you’ve likely migrated onto your own dedicated server you manage yourself, and its likely your making mistakes with email forwarding and filtering spam!
The problem is that that when you receive spam, you are also forwarding spam to your email provider, which makes them upset with you and tarnishes your servers IP address. I did this for years! I always thought that Gmail would be smart enough to see the path in the headers to realize it was forwarded – but then thinking about it – why would Gmail trust me that those servers actually sent the email and that I didn’t just spoof those Received: lines to blame someone else?
When I recently migrated my host, I put in a lot more effort into filtering the spam before it even hits Gmail, and learned quite a few things.
Filtering Spam with Postfix
First Off: Initial Connection Client Checks – These stop a majority of the spammers, and its so simple!
Add this line to your /etc/postfix/main.cf:
smtpd_client_restrictions = permit_mynetworks permit_sasl_authenticated reject_unauth_destination reject_rbl_client zen.spamhaus.org reject_rbl_client bl.spamcop.net reject_rbl_client cbl.abuseat.org reject_unknown_client permit
This will enforce a lot of restrictions on the client, namely the Zen Spamhaus check, which knocks out so many spammer connections!
Filtering Spam with SpamAssassin
If you haven’t already installed SpamAssassin, do so now. There is a bit to this than I want to put into this post, so follow this sites guide: http://plecko.com.hr/?p=389
His instructions look spot on to me. Key thing I did not do on my setup and I just realized I needed to do: enable CRON=1! I’ve been running with stale SA Rules… But his guide covers it!
Next up is this page: http://wiki.apache.org/spamassassin/ImproveAccuracy
One thing it mentions is missing Perl Modules that SpamAssassin can try to use. For me, I had to run these commands to get them all installed.
sudo apt-get install libgeoip-dev
sudo cpan Geo::IP Mail::DKIM Encode::Detect DBI IO::Socket::IP Digest::SHA1 Net::Patricia
I don’t know what some of them are for, but SpamAssassin is obviously trying to use them, so give them to it!
Passing SPF Checks
Then there is SRS Rewriting. One problem with forwarding email is that it makes every one of your emails now fail SPF checks, because it looks like your server is sending mail for InsertBigNameDomain.com which does not authorize you to send mail on their behalf.
SPF is considered a “broken” implementation, and it is preferred that system admins use DKIM instead as a way to verify authenticity of an email, so ideally you need to rewrite the return path to be your own server name instead.
I used this guide: https://www.mind-it.info/forward-postfix-spf-srs/
Which summarizes down to
sudo apt-get install cmake sysv-rc-conf cd /usr/local/src/ wget https://github.com/roehling/postsrsd/archive/master.zip unzip master cd postsrsd-master/ make sudo make install sudo postconf -e "sender_canonical_maps = tcp:127.0.0.1:10001" sudo postconf -e "sender_canonical_classes = envelope_sender" sudo postconf -e "recipient_canonical_maps = tcp:127.0.0.1:10002" sudo postconf -e "recipient_canonical_classes = envelope_recipient" sudo sysv-rc-conf postsrsd on sudo service postsrsd restart sudo service postfix reload
Now when you inspect a received emails header, you will see that the ReturnPath is now something like <SRS0+9CLa=52=paypal.com=service@starlis.com>
And your SPF will now pass (You do have SPF records set right for your domain?)
Dropping the Spam
Now the final part… getting rid of that spam before it goes to Gmail!
In /etc/postfix/header_checks (you likely will need to create this file), add this simple line:
/^X-Spam-Level: \*{5,}.*/ DISCARD spam
then in /etc/postfix/main.cf:
header_checks = regexp:/etc/postfix/header_checks
This will drop the spam, but you may want to only drop higher level spam, so instead you could change the 5 to a 7, and then add to your /etc/spamassassin/local.cf (might already be there commented out):
rewrite_header Subject *****SPAM*****
This makes it so that any spam that doesn’t get dropped, has SPAM prepended to the header, which Gmail suggests you do if you do end up forwarding spam to Gmail.
With this approach, low score (5-6) spam will be forwarded but makes Gmail happy that you told them its spam ahead of time, and 7+ spam won’t even bother forwarding.
Taking these steps will help you maintain a good mail sending reputation (Hopefully I don’t have to repair mine too much…). Good luck 🙂
Final note for Gmail users
And one final step if you are using Gmail, ensure EVERY email address that you receive mail from that is forwarded to Gmail is added as a “Send Mail As” account. Gmail uses this list to know it is a forwarded address, and will be more lenient in spam rules. I don’t know if other ESP’s do this, but Gmail has requested you do this if you forward mail to them.
0