procmail
[Top] [All Lists]

Re: convert all mails of a catch-all mailbox to html or .txt. files in folders named by the recipient

2007-08-29 23:05:28

On 29-Aug-2007, at 17:16, Stefan S. wrote:

I wonder if the following is possible:


I have a mailbox configured to "catch-all". I want to convert the  
mails
of this mailbox in /var/spoll/mail/MAILBOXNAME to html or at  
least .txt
files and put them in a directory which is named like the  
emailadres or
the part befor the @. (I would store them in a folder which is
accessible over Apache over http://lanserver/FOLDER_WE_TALK_ABOUT )
Example: a mail to XYZ(_at_)domain(_dot_)tld in var/spool/mail/MAILBOX should 
be
converted and stored in a directory which is named XYZ or  
XYZ(_at_)domain(_dot_)tld
This means: Procmail has to look in the mailbox file, and to search  
for
mails with the same recipient´s-adress, and to put them into a folder
which is named like the emailadress of the recipient.

YOu can do this by simply extracting the username in teh To: field.   
this is problematic if you have messages that are CCed or sent to  
multiple email addresses, but let's ignore that because you liekly  
don't care and it makes the task Herculean.

First, get the user part of the To address:

:0
* $ ^To:$WS*\/[^$WS].*
{ TO=$MATCH }

:0
* TO ?? ^\/[^(_at_)]+
{ TO_USER=$MATCH }

Now, deliver the message, ignoreing anything else, to a mbox file

:0
$TO_USER

Now, the caveats are this will fail with any message that was sent to  
more than one person.  It will only end up in the file named for the  
first user to appear on the To: line.  I expect you don't care.

ALSO, and very important, if the message was Bcced (like a list  
message) then the To will contain, like on this mesage, the name of  
the list.  So you will get a mailbox named 'procmail'. I know, you're  
thinking, no big deal.  Trouble is, a LOT of messages are sent via  
Bcc and you might find this is more of an issue than you might have  
guessed.

There are other options.  None of them are foolproof.  The next best  
is to scan the headers for a line from your mailserver like:

Received: from mta-2.ms.rz.rwth-aachen.de (mta-2.ms.rz.RWTH-Aachen.DE  
[134.130.7.73])
        by mail.covisp.net (Postfix) with ESMTP id 0BD5E118ADDB
        for <kremels(_at_)kreme(_dot_)com>; Wed, 29 Aug 2007 17:16:58 -0600 
(MDT)

and try to parse for that user name:

MYRCD='by [^$WS]*(mail\.)?covisp.net.*for [^$WS]*<'
:0
* $ ^Received:.*$MYRCD\/[^(_at_)]+
{ TO_USER=$MATCH }

In fact, I'd go ahead and do this:


---cut
# Space and TAB, natch
WS='  '
MYRCD='by [^$WS]*(mail\.)?covisp.net.*for [^$WS]<'

# Clear TO_USER to be safe
TO_USER
# This recipe will fail if there is no 'for <user(_at_)doamin(_dot_)tld>' in the
# Received header, so TO_USER will be unset
:0
* $ ^Received:.*$MYRCD\/[^(_at_)]+
{ TO_USER=$MATCH }

# If TO_USER is empty, try the To: header
:0
* TO_USER ?? ^^^^
{
    :0
    * $ ^To:$WS*\/[^$WS].*
    { TO=$MATCH }

    :0
    * TO ?? ^\/[^(_at_)]+
    { TO_USER=$MATCH }
}

:0
$TO_USER
---cut

It's still not going to work 100% of the time, but it's likely good  
enough.

-- 
It was a fifty-four with a mashed up door and a cheesy little amp  
with a sign on the front said "Fender Champ" and a second-hand guitar  
it was a Stratocaster with a whammy bar




____________________________________________________________
procmail mailing list   Procmail homepage: http://www.procmail.org/
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)DE
http://MailMan.RWTH-Aachen.DE/mailman/listinfo/procmail