All:
I am looking for a way to modify an email message in transit. In all cases I will be modifying the "to", "from", and some other headers. In some cases - I will be removing a string from the body of the message. In other cases - I will be adding a string to the body of the message.

I have been using Mail::Audit (which uses MIME::Entity if needed) as a starting point, and am going to throw some snippets to give you an idea (in no particular order):

my $mail = Mail::Audit->new(data => \@message); $mail->bodyhandle; my @body = $mail->bodyhandle->as_lines; if ($msgtyp ne 'reply') { my $orig_from = $mail->get('Reply-To') ? $mail->get('Reply-To') : +$mail->get('From'); unshift @body , "RPLYFROM: $replyfrom"; unshift @body , "ORIGFROM: $origfrom"; if (my $io = $mail->open("w")) { $io->print($_) foreach(@body); $io->close; } } $mail->delete_header('Reply-To') if ($mail->delete_header('Reply-To')) +; $mail->delete_header('Received'); $mail->delete_header('Message-Id'); $mail->replace_header('From', $orig); $mail->replace_header('To', $recip); $mail->resend($recip);

Ok - you get the idea - I munge the data, so what's the problem? I am not sure how to handle MIME body correctly. What I would like to say is:

  • Remove string x from the body and have it figure out how to handle the plain text and/or MIME
  • Add string x to the body and have it figure out how to handle the plain text and/or MIME

    I thought I was pretty close until I encountered "Content-Type: multipart/alternative;". There are basically multiple pieces to the body (in my case plain text and HTML). The email client chooses the one it likes.

  • Now I am parsing HTML which I hadn't counted on (another module)
  • I have no idea how to make sure both pieces are updated so that it doesn't matter which body the client chooses.

    I am grateful for any help that I can get, but what is most desireable is a working template. I can handle modifying all the headers - it is the body and specifically MIME body that is causing me grief.

    Thanks in advance - L~R


    In reply to Advice for email munging by Limbic~Region

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.