tedrek,
I appreciate your advice and your reply. Unfortunately, I have no idea how to implement. I am sorry if I gave the false impression that I knew what I was doing with my code snippets - I am figuring this out as I go. That is why I was hoping for a working template. What I don't understand how to do (in code) is to figure out what pieces I can throw away, which pieces I need to keep, and how to make sure when I reconstruct the message I have done it properly. I have no issues with dropping the HTML piece (or RTF) as long as there is a plain text piece, but is it still mixed/alerternate at that point? What happens if it is MIME and the only thing I get is HTML?
I know this is my problem and not yours. I guess I have more questions than I have answers. I can make the code work if I get a specific type of message - I don't know how to say - DWIM to my code.
Thanks again, L~R | [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] |
tedrek,
I will play with this tomorrow. I really appreciate it. I hate projects where you know a better way to do it but you are told - do it this way because I said so. In any case - even if the project never gets off the ground I have learned something and I appreciate the help.
Cheers - L~R
| [reply] |