Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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;

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
  $ent->sync_headers(Length => 'COMPUTE', Nonstandard => 'ERASE');

print $envelope;
Replies are listed 'Best First'.
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.


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'); }
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?


    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. ;)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://53404]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2017-01-16 20:42 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (151 votes). Check out past polls.