Thanks for your reply, CountZero.

The reason I posted an inquiry on PerlMonks before I delved into using Email::Simple is because it wasn't obvious to me from its documentation whether it could be used to edit EML files in place (effectively, at least). I needed to make small changes to many EML files, but I didn't want to have to construct whole Internet mail messages anew. And it takes me longer than it probably takes most Perl programmers to learn to use a new module.

It turns out I don't have to repair the EML files after all. But I do have to extract the character encoding damaged text from them and make a best effort to reverse the corruption. (See How to Fix Character Encoding Damaged Text Using Perl?.) I was able to determine that, in my finite collection of EML files, the damaged text is consistently in the same format, so matching the pattern was trivial using a regular expression. The script I wrote is included below.

#!perl use v5.14; use Encode qw( encode decode ); use MIME::Base64; binmode STDOUT, ':encoding(UTF-8)'; local $, = "\t"; local $\ = "\n"; @ARGV = <@ARGV>; # Expand wildcards... LINE: while (<>) { next LINE unless m{ ^(\S+): # field name \s+ =[?]utf-8[?]B[?] ([^?]+) # base64 encoded text [?]= }ix; my ($field_name, $base64_encoded_text) = ($1, $2); my $base64_decoded_text = decode_base64($base64_encoded_text); my $utf8_decoded_text = decode('UTF-8', $base64_decoded_text); next LINE unless $utf8_decoded_text =~ m{ [^\p{Script=Common}\p{Script=Latin}] }x; my $damaged_text = $utf8_decoded_text; my $repaired_text = decode('UTF-8', encode('UCS-2LE', $damaged_text)); $repaired_text =~ s{\x{00}+$}{}; print $ARGV, $field_name, $repaired_text, $damaged_text; } exit 0;

In reply to Re^2: Simplest Way to Edit EML Files? by Jim
in thread Simplest Way to Edit EML Files? by Jim

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.