procmail
[Top] [All Lists]

Re: metamail and procmail

1997-07-13 14:39:00
Patrik Wallstrom <pawal(_at_)stacken(_dot_)kth(_dot_)se> writes:
I have searched the archives on how to use metamail for this specific
purpose, but I haven't found any answer to the following query:

I am subscribing to a couple of HTML-newsletters which are sent as a
regular mail with the HTML being an attachment. When trying the following
recipe,

#Good Morning Silicon Valley
:0Hb:
* ^From: .*Mercury.*
* ^To: (_dot_)*GMSV-HTML-L(_at_)MLIST(_dot_)MERCURYCENTER(_dot_)COM(_dot_)*
* ^Subject: .*Good Morning.*
| metamail -x -y > mercury/`date '+%y%m%d.%H%M%S'`.html

metamail says something ugly, and the mail ends up in the folder "^From:" 
(or something just as stupid, I just erased my log, silly me...), which
isn't exactly what I hoped for. As you can see I am putting each mail into
a separate HTML-file, marked with the date and the time. 

Anyway, anybody out there who has a solution for me?

When you say it's an attachment, do you mean it's a multipart message,
or is it a simple "Content-Type: text/html" message?  If the latter,
then I don't see why your metamail action doesn't work.  If the former,
well, you'll want something bigger than metamail to extract the html.
If you've programmed in Perl before, I'd suggest using the MIME::*
modules to do a full MIME parse and the extraction.

This is all totally untested, and based off a high-speed scanning of
the POD pages.


# Good Morning Silicon Valley
:0 w
* ^From: .*Mercury.*
* ^To: (_dot_)*GMSV-HTML-L(_at_)MLIST(_dot_)MERCURYCENTER(_dot_)COM(_dot_)*
* ^Subject: .*Good Morning.*
* ^Content-Type: multipart/
|perl -mMIME::Parser -e '$foo = new MIME::Parser;' \
        -e '$foo->output_dir("$ENV{HOME}/mimemail");' \
        -e '$foo->read(\*STDIN) or die "couldn't parse MIME stream";' \
        -e '@t = localtime(); $t[5] += 1900; $t[4]++; $" = "";' \
        -e 'open(FOO,">mercury/@t[5,4,3](_dot_)(_at_)t[2,1,0]") or die "open: 
$!";' \
        -e 'for($foo->parts){' \
        -e '    if($_->mime_type eq "text/html") {' \
        -e '        $_->bodyhandle->print(\*FOO);exit(0);' \
        -e '}}'
        -e 'exit(1);'

(Quick synopsis: we load the required module, the create an object of
that type.  We tell it to put temporary files in $HOME/mimemail -- you
should create that directory or change it to something appropriate: I
recommend against /tmp in favor of $HOME/tmp as the temporary files are
not opened in a safe fashion (symlinks will be followed and targets
overwritten, etc), so a local user could carefully craft a message that
would overwrite your .rhosts, if they have write access to the
directory used by this blurb of code.

Anyway, it then slurps the message, gets the time, adjusts for the
idiosyncrasies of C's struct tm and then opens the desired file.  It
then iterates through the parts of this multipart message, writing the
body of the first text/html part to the opened file and exits.  If
there wasn't a text/html part then it returns failure and procmail
should recover the message for later recipes to deal with.)


Philip Guenther

<Prev in Thread] Current Thread [Next in Thread>