Re: [Hampshire] Bash script for forwarding spam from an mbox

Top Page

Reply to this message
Author: Ali Afshar
Date:  
To: Hampshire LUG Discussion List
Subject: Re: [Hampshire] Bash script for forwarding spam from an mbox
On 16/07/07, Graeme Hilton <graeme.hilton@???> wrote:
> Hi all,


Hi everyone, this is my first post here (of many maybe!),

> I'm part way through revamping my email setup and am now relying more
> heavily on my ISPs spam/virus detection. Their spam detection uses
> DSPAM and tags each spam with [-SPAM-] in the subject as well as several
> header fields.
>
> My own setup runs mail through SpamAssassin and another virus check. In
> order to help my ISP get better spam detection I want to forward back
> to them anything that they missed and I detected as spam. They prefer
> emails forwarded inline with all headers intact.
>
> Currently I manually check the Junk folder once a day and move all
> spammy mails to JunkSpam, and non-spammy mails to the JunkNotSpam.
> Overnight I have a cron script that feeds the mails to SpamAssassin[1].
> I'd also like this script to detect which mails have been missed by the
> ISP (don't have [-SPAM-] in the subject or have DSPAM: INNOCENT as a
> header), and then forward them individually, inline to the ISPs training
> address.
>
> Does anyone have any pointers to a shell scripting tutorial that might
> be able to help me split out the emails from an mbox? Googling for
> various combinations of "shell script mail forward mbox" doesn't give me
> much to work with.


Shell scripting an actual parser might be a bit of a pain. My choice
of language for this kind of thing is Python. Parsing an mbox is as
easy as:

import mailbox
mb = mailbox.UnixMailbox(open('/path/to/mbox','r'))
for msg in mb:
    document = msg.as_string()
    # do whatever you want with document


Equally, Perl, Lua, Ruby etc would make this easy.

Cheers,

Ali