Getmail and SpamAssassin

I use the SpamAssassin spam filter with my mail and it categorizes it pretty well

To get it to work with getmail I added the following to my getmail config files (I chose not to use the spamd).


[filter]
type = Filter_external
path = /usr/bin/spamassassin

Unfortunately getmail doesn’t appear to have the facility to filter messages based on their headers (SpamAsssassin, in the above configuration modifies the email header to include the line “X-Spam-Flag: YES” to indicate a message is spam), so I created a bash script which launches getmail and re-categorizes my mail if spam assassin marks it as spam. It works by grepping my maildir’s for this header and moving the file to a special spam directory if it is found.


#!/bin/bash
function spamdetect {
if [ ! -d "$1/spam/" ]
then
echo "You must create a spam direction $1/spam";
exit 1
fi

# strips out trailing slash if there is one
a=$1
if [ ${a:${#a}-1} = "/" ]
then
a=${a:0:${#a}-1}
fi

grep -H "X-Spam-Flag: YES" $a/new/* | sed "s/:.*//" | uniq | xargs -i mv '{}' $a/spam/
}

/usr/bin/getmail -r getmailrc-bob -r getmailrc-bingo

spamdetect ~/Mail/bob
spamdetect ~/Mail/bingo

Getmail + GMail

I found an interesting program called getmail for linux (which you can install through ‘apt-get install getmail4’ on Ubuntu), its used for fetching mail from a mail server and delivering it to a folder or MTA.

The following is the configuration file I used to download email from my gmail account to keep a back up (you need to go to setting in GMail if you want to retrieve all your messages).

I found Getmail was only pulling the first 349 emails for me, by adding an options section to the config file with ‘delete = true’ i managed to download all my email. It downloaded messages in batches so i had to run getmail a number of times to get all my email. Interestingly ‘delete = true’ doesn’t seem to actually delete the message for the GMail webbased interface, but it does mean you can’t retrieve it again via POP.

[retriever]
type = SimplePOP3SSLRetriever
server = pop.gmail.com
username = email_address
password = password

[destination]
type = Maildir
path = ~/Mail/your-maildir-folder

[options]
delete = true

SSL connection

I needed a way to connect to gmail’s POP server (directly without a mail client), it looks like you can’t connect to their server without using SSL. I thought about using netcat combined with stunnel which can let you connect to SSL based servers however i found an easier way using a command from the openssl suite.

openssl s_client -connect pop.gmail.com:995