What I would do is find the first text/plain part in the message, munge that and use it as the sole body in the outgoing message. the reason for that is you can be almost certain that the mail will contain a text/plain part and by removing all other parts you don't have to worry about ppl seeing them. If you need to munge every single part you have your work cut out for you. You could theoretically have a alternative part which is sound, not to mention mixed inside alternative inside mixed. And I have seen text/rtf inside a multipart/alternative
As for a full strategy I'd probably go OO style and make a object for each mime type which has functions for each of the actions you need to perform eg $txt->replace_string('foo', 'bar'). Then container types would call the functions on whatever they contain. Any type you don't have a handler for gets dropped from the message when you parse it in. So now your munging is basically $body->replace_string('foo', 'bar') and that's all in one place which just works on all the different types you support.
| [reply] [d/l] [select] |
| [reply] |
Ok, I'm working with this under the assumption that it is safe, even desirable to throw away parts you can't change. eg sound, archives, rtf, or html. If that is the case the problem becomes 'How do I make a message with containing only text/plain from a message which may contain any type'. This can be broken down into 3 parts 'Find all the text/plain parts', 'Edit them', 'Put all edited parts back in the body'. I think you have the 'Edit them' part covered basically which leaves us with two things to do. so let's tackle the first:
There's two cases I see, a body containing one thing (hopefully text/plain) you already know how to handle this right? the other case is a body containing 'multipart/mixed' or 'multipart/alternative' which are pretty much interchangeable for us.
In this case we can break it down to 'get each part inside the multipart', 'look at each part', and 'save for editing if part is text/plain'. I think this code should show my basic idea.
my @parts;
if ($msg->is_mime) {
my @t_parts = $msg->parts();
while (shift @t_parts) {
if ($_->mime_type eq 'text/plain') {
push @parts, $_;
next;
}
if ($_->mime_type eq 'multipart/mixed' ||
$_->mime_type eq 'multipart/alternative') {
push @t_parts, $_->parts();
}
}
}
then you just edit each item in @parts. and assemble a new message with the headers from the old one and a new body containing whatever parts you have edited. If there is only one piece then the message content-type is 'text/plain' if there is more than one piece the type is probably 'multipart/mixed'. You will probably have to be aware of multipart/alternative with multiple text/plain inside, but that case isn't very likely. As for messages that don't contain any text/plain... it's probably spam or hotmail/yahoo. You may find that you need to capture the text/html parts, but that should be relatively easy to add.
an alternative strategy might be basically the same thing but instead of saving the text/plain parts delete anything that isn't text/plain or a multipart containing a text/plain. This way you don't need to worry about which multipart a text part was supposed to go into. I believe it is legal to have a multipart/alternative with only a single part inside it.
As for making sure the message is reconstructed properly.. that's why you are using Mail::Audit and MIME:Entity in the first place :). I hope that helps you some. and note I haven't tested any of the code. as a side note if you really want your code to say DWIM check out Acme::DWIM :)
| [reply] [d/l] |
One thing that occurs to me is that if the MIME body contains HTML, you could strip the plain text message out completely, alter the HTML as you need it, and then insert a new plain text message derived from your altered HTML. I suggest this as an alternative to stripping the HTML because the more simple formats can be derived from the more complex ones with relatively little extra effort (it doesn't work in the other direction). I guess I'd suggest HTML::TokeParser or HTML::TokeParser::Simple for that job.
--
Allolex
| [reply] |