MIME::Words − deal with RFC 2047 encoded words


Before reading further, you should see MIME::Tools to make sure that you understand where this module fits into the grand scheme of things. Go on, do it now. I’ll wait.

Ready? Ok...

    use MIME::Words qw(:all);
    ### Decode the string into another string, forgetting the charsets:
    $decoded = decode_mimewords(
          'To: =?ISO−8859−1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>',
    ### Split string into array of decoded [DATA,CHARSET] pairs:
    @decoded = decode_mimewords(
          'To: =?ISO−8859−1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>',
    ### Encode a single unsafe word:
    $encoded = encode_mimeword("\xABFran\xE7ois\xBB");
    ### Encode a string, trying to find the unsafe words inside it:
    $encoded = encode_mimewords("Me and \xABFran\xE7ois\xBB in town");


Fellow Americans, you probably won’t know what the hell this module is for. Europeans, Russians, et al, you probably do. ":−)".

For example, here’s a valid MIME header you might get:

      From: =?US−ASCII?Q?Keith_Moore?= <moore@cs.utk.edu>
      To: =?ISO−8859−1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>
      CC: =?ISO−8859−1?Q?Andr=E9_?= Pirard <PIRARD@vm1.ulg.ac.be>
      Subject: =?ISO−8859−1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=

The fields basically decode to (sorry, I can only approximate the Latin characters with 7 bit sequences /o and ’e):

      From: Keith Moore <moore@cs.utk.edu>
      To: Keld J/orn Simonsen <keld@dkuug.dk>
      CC: Andr'e  Pirard <PIRARD@vm1.ulg.ac.be>
      Subject: If you can read this you understand the example... cool!


decode_mimewords ENCODED

Function. Go through the string looking for RFC 2047−style "Q" (quoted-printable, sort of) or "B" (base64) encoding, and decode them.

In an array context, splits the ENCODED string into a list of decoded "[DATA, CHARSET]" pairs, and returns that list. Unencoded data are returned in a 1−element array "[DATA]", giving an effective CHARSET of "undef".

    $enc = '=?ISO−8859−1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>';
    foreach (decode_mimewords($enc)) {
        print "", ($_[1] || 'US−ASCII'), ": ", $_[0], "\n";

In a scalar context, joins the "data" elements of the above list together, and returns that. Warning: this is information-lossy, and probably not what you want, but if you know that all charsets in the ENCODED string are identical, it might be useful to you. (Before you use this, please see "unmime" in MIME::WordDecoder, which is probably what you want.)

In the event of a syntax error, $@ will be set to a description of the error, but parsing will continue as best as possible (so as to get something back when decoding headers). $@ will be false if no error was detected.

Any arguments past the ENCODED string are taken to define a hash of options:

encode_mimeword RAW , [ ENCODING ], [ CHARSET ]

Function. Encode a single RAW "word" that has unsafe characters. The "word" will be encoded in its entirety.

    ### Encode "<<Franc,ois>>":
    $encoded = encode_mimeword("\xABFran\xE7ois\xBB");

You may specify the ENCODING ("Q" or "B"), which defaults to "Q". You may specify the CHARSET , which defaults to "iso−8859−1".

encode_mimewords RAW , [ OPTS ]

Function. Given a RAW string, try to find and encode all "unsafe" sequences of characters:

    ### Encode a string with some unsafe "words":
    $encoded = encode_mimewords("Me and \xABFran\xE7ois\xBB");

Returns the encoded string. Any arguments past the RAW string are taken to define a hash of options:

Encode all unsafe stuff with this charset. Default is ’ ISO−8859−1 ’, a.k.a. "Latin−1".


The encoding to use, "q" or "b". The default is "q".

Warning: this is a quick-and-dirty solution, intended for character sets which overlap ASCII . It does not comply with the RFC 2047 rules regarding the use of encoded words in message headers. You may want to roll your own variant, using "encode_mimeword()", for your application. Thanks to Jan Kasprzak for reminding me about this problem.


MIME::Base64, MIME::QuotedPrint, MIME::Tools

For other implementations of this or similar functionality (particularly, ones with proper UTF8 support), see:

Encode::MIME::Header, MIME::EncWords, MIME::AltWords

At some future point, one of these implementations will likely replace MIME::Words and MIME::Words will become deprecated.


Exports its principle functions by default, in keeping with MIME::Base64 and MIME::QuotedPrint.


Eryq (eryq@zeegee.com), ZeeGee Software Inc (http://www.zeegee.com). David F. Skoll (dfs@roaringpenguin.com) http://www.roaringpenguin.com

All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Thanks also to...

      Kent Boortz        For providing the idea, and the baseline
                         RFC−1522−decoding code!
      KJJ at PrimeNet    For requesting that this be split into
                         its own module.
      Stephane Barizien  For reporting a nasty bug.


Personal Opportunity - Free software gives you access to billions of dollars of software at no cost. Use this software for your business, personal use or to develop a profitable skill. Access to source code provides access to a level of capabilities/information that companies protect though copyrights. Open source is a core component of the Internet and it is available to you. Leverage the billions of dollars in resources and capabilities to build a career, establish a business or change the world. The potential is endless for those who understand the opportunity.

Business Opportunity - Goldman Sachs, IBM and countless large corporations are leveraging open source to reduce costs, develop products and increase their bottom lines. Learn what these companies know about open source and how open source can give you the advantage.

Free Software

Free Software provides computer programs and capabilities at no cost but more importantly, it provides the freedom to run, edit, contribute to, and share the software. The importance of free software is a matter of access, not price. Software at no cost is a benefit but ownership rights to the software and source code is far more significant.

Free Office Software - The Libre Office suite provides top desktop productivity tools for free. This includes, a word processor, spreadsheet, presentation engine, drawing and flowcharting, database and math applications. Libre Office is available for Linux or Windows.

Free Books

The Free Books Library is a collection of thousands of the most popular public domain books in an online readable format. The collection includes great classical literature and more recent works where the U.S. copyright has expired. These books are yours to read and use without restrictions.

Source Code - Want to change a program or know how it works? Open Source provides the source code for its programs so that anyone can use, modify or learn how to write those programs themselves. Visit the GNU source code repositories to download the source.


Study at Harvard, Stanford or MIT - Open edX provides free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses for almost all major subjects and course levels. Open edx also offers some paid courses and selected certifications.

Linux Manual Pages - A man or manual page is a form of software documentation found on Linux/Unix operating systems. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts.