Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Strip Brain-Damaged Mails of "HTML Alternative" Evilness

by merlyn (Sage)
on Jan 22, 2001 at 06:55 UTC ( #53404=sourcecode: print w/ replies, xml ) Need Help??

Category: E-Mail Programs.
Author/Contact Info merlyn
Description: Durn it if there seems to be no way for Outlook with Win2K to turn off that evil "HTML version" of mail. A MIME is a terrible thing to waste, so let's strip that as it comes in. Set up a procmail recipe to pipe mail that has both boundary and html in its content-type header through this program, and you'll never have to deal with that evil embrace-and-extend garbage from Redmond again.

It's safe to pipe all mail through this, if you don't want to be clever with procmail. You also might want to do something sensible with the die there. I put the hook there just in case, although all I'm doing is re-throwing it.

Requires MIME::Tools.

See also the column I wrote around this program.

#!/usr/bin/perl -w
use strict;
$|++;

my $envelope = <STDIN>;

use MIME::Parser;
use MIME::Entity;

my $parser = MIME::Parser->new;
$parser->output_to_core(1);
$parser->tmp_to_core(1);

my $ent = eval { $parser->parse(\*STDIN) }; die "$@" if $@;

if ($ent->effective_type eq "multipart/alternative"
    and $ent->parts == 2
    and $ent->parts(0)->effective_type eq "text/plain"
    and $ent->parts(1)->effective_type eq "text/html") {
  
  my $newent = MIME::Entity->build(Data =>
                                   $ent->parts(0)->body_as_string .
                                   "\n\n[[HTML alternate version delet
+ed]]\n");
  $ent->parts([$newent]);
  $ent->make_singlepart;
  $ent->sync_headers(Length => 'COMPUTE', Nonstandard => 'ERASE');
}

print $envelope;
$ent->print;

Comment on Strip Brain-Damaged Mails of "HTML Alternative" Evilness
Download Code
Re: Strip Brain-Damaged Mails of "HTML Alternative" Evilness
by marius (Hermit) on Jan 23, 2001 at 00:01 UTC
    Does anyone see the code, or am I on crack again?

    -marius

    UPDATE:Apparently it's a known issue. See Haunted Catacombs for more information.
      I see no code. The question of your substance (ab)use problem remains as an excercise for the reader. ;-)
        Excellent, at least I know it's not /just/ me. ;)

        -marius
Re: Strip Brain-Damaged Mails of "HTML Alternative" Evilness
by Fastolfe (Vicar) on Jan 24, 2001 at 07:49 UTC
    For some, it might be more useful to turn this HTML version into a plain vanilla e-mail attachment, in case there are differences that they want to see.

    Additionally, I get some e-mails that are offered only in text/html format. It might be nifty to have a similar filter set up to run the HTML through something like HTML::FormatText. What do you think?

Re: Strip Brain-Damaged Mails of "HTML Alternative" Evilness
by strredwolf (Chaplain) on Jan 24, 2001 at 08:08 UTC
    Is there a $ent->parts(x)->length? You may want to filter if the one has no text/plain (or one that's just blank lines). Alot of spammers are doing this.

    --
    $Stalag99{"URL"}="http://stalag99.keenspace.com";

Re: Strip Brain-Damaged Mails of "HTML Alternative" Evilness
by Anonymous Monk on Jan 26, 2002 at 01:20 UTC
    One point, if a message is HTML and has an attachment then the $ent->effective_type would be multipart/mixed and then $ent->parts(0)->effective_type would be multipart/alternative and then the sub-parts ($ent->parts(0)->parts(x)->effective_type) of this would be text/plain(0) and text/html(1).

    So you would need to add this after testing for multipart/alternative :)
    if ($ent->effective_type eq "multipart/mixed" and $ent->parts(0)->effective_type eq "multipart/alternative" and $ent->parts(0)->parts == 2 and $ent->parts(0)->parts(0)->effective_type eq "text/plain" and $ent->parts(0)->parts(1)->effective_type eq "text/html") { my $newent = MIME::Entity->build(Data => $ent->parts(0)->parts(0)->body_as_string); $ent->parts(0)->parts([$newent]); $ent->parts(0)->make_singlepart; $ent->parts(0)->sync_headers(Length => 'COMPUTE', Nonstandard => ' +ERASE'); $ent->sync_headers(Length => 'COMPUTE', Nonstandard => 'ERASE'); }
    -jeffphil

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://53404]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-09-02 02:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (18 votes), past polls