1MIME::Words(3) User Contributed Perl Documentation MIME::Words(3)
2
3
4
6 MIME::Words - deal with RFC-1522 encoded words
7
9 Before reading further, you should see MIME::Tools to make sure that
10 you understand where this module fits into the grand scheme of things.
11 Go on, do it now. I'll wait.
12
13 Ready? Ok...
14
15 use MIME::Words qw(:all);
16
17 ### Decode the string into another string, forgetting the charsets:
18 $decoded = decode_mimewords(
19 'To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>',
20 );
21
22 ### Split string into array of decoded [DATA,CHARSET] pairs:
23 @decoded = decode_mimewords(
24 'To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>',
25 );
26
27 ### Encode a single unsafe word:
28 $encoded = encode_mimeword("\xABFran\xE7ois\xBB");
29
30 ### Encode a string, trying to find the unsafe words inside it:
31 $encoded = encode_mimewords("Me and \xABFran\xE7ois\xBB in town");
32
34 Fellow Americans, you probably won't know what the hell this module is
35 for. Europeans, Russians, et al, you probably do. ":-)".
36
37 For example, here's a valid MIME header you might get:
38
39 From: =?US-ASCII?Q?Keith_Moore?= <moore@cs.utk.edu>
40 To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>
41 CC: =?ISO-8859-1?Q?Andr=E9_?= Pirard <PIRARD@vm1.ulg.ac.be>
42 Subject: =?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=
43 =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=
44 =?US-ASCII?Q?.._cool!?=
45
46 The fields basically decode to (sorry, I can only approximate the Latin
47 characters with 7 bit sequences /o and 'e):
48
49 From: Keith Moore <moore@cs.utk.edu>
50 To: Keld J/orn Simonsen <keld@dkuug.dk>
51 CC: Andr'e Pirard <PIRARD@vm1.ulg.ac.be>
52 Subject: If you can read this you understand the example... cool!
53
55 decode_mimewords ENCODED, [OPTS...]
56 Function. Go through the string looking for RFC-1522-style "Q"
57 (quoted-printable, sort of) or "B" (base64) encoding, and decode
58 them.
59
60 In an array context, splits the ENCODED string into a list of
61 decoded "[DATA, CHARSET]" pairs, and returns that list. Unencoded
62 data are returned in a 1-element array "[DATA]", giving an effec‐
63 tive CHARSET of "undef".
64
65 $enc = '=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>';
66 foreach (decode_mimewords($enc)) {
67 print "", ($_[1] ⎪⎪ 'US-ASCII'), ": ", $_[0], "\n";
68 }
69
70 In a scalar context, joins the "data" elements of the above list
71 together, and returns that. Warning: this is information-lossy,
72 and probably not what you want, but if you know that all charsets
73 in the ENCODED string are identical, it might be useful to you.
74 (Before you use this, please see "unmime" in MIME::WordDecoder,
75 which is probably what you want.)
76
77 In the event of a syntax error, $@ will be set to a description of
78 the error, but parsing will continue as best as possible (so as to
79 get something back when decoding headers). $@ will be false if no
80 error was detected.
81
82 Any arguments past the ENCODED string are taken to define a hash of
83 options:
84
85 Field
86 Name of the mail field this string came from. Currently
87 ignored.
88
89 encode_mimeword RAW, [ENCODING], [CHARSET]
90 Function. Encode a single RAW "word" that has unsafe characters.
91 The "word" will be encoded in its entirety.
92
93 ### Encode "<<Franc,ois>>":
94 $encoded = encode_mimeword("\xABFran\xE7ois\xBB");
95
96 You may specify the ENCODING ("Q" or "B"), which defaults to "Q".
97 You may specify the CHARSET, which defaults to "iso-8859-1".
98
99 encode_mimewords RAW, [OPTS]
100 Function. Given a RAW string, try to find and encode all "unsafe"
101 sequences of characters:
102
103 ### Encode a string with some unsafe "words":
104 $encoded = encode_mimewords("Me and \xABFran\xE7ois\xBB");
105
106 Returns the encoded string. Any arguments past the RAW string are
107 taken to define a hash of options:
108
109 Charset
110 Encode all unsafe stuff with this charset. Default is
111 'ISO-8859-1', a.k.a. "Latin-1".
112
113 Encoding
114 The encoding to use, "q" or "b". The default is "q".
115
116 Field
117 Name of the mail field this string will be used in. Currently
118 ignored.
119
120 Warning: this is a quick-and-dirty solution, intended for character
121 sets which overlap ASCII. It does not comply with the RFC-1522
122 rules regarding the use of encoded words in message headers. You
123 may want to roll your own variant, using "encoded_mimeword()", for
124 your application. Thanks to Jan Kasprzak for reminding me about
125 this problem.
126
128 Exports its principle functions by default, in keeping with
129 MIME::Base64 and MIME::QuotedPrint.
130
132 Eryq (eryq@zeegee.com), ZeeGee Software Inc (http://www.zeegee.com).
133 David F. Skoll (dfs@roaringpenguin.com) http://www.roaringpenguin.com
134
135 All rights reserved. This program is free software; you can redis‐
136 tribute it and/or modify it under the same terms as Perl itself.
137
138 Thanks also to...
139
140 Kent Boortz For providing the idea, and the baseline
141 RFC-1522-decoding code!
142 KJJ at PrimeNet For requesting that this be split into
143 its own module.
144 Stephane Barizien For reporting a nasty bug.
145
147 $Revision: 1.14 $ $Date: 2006/03/17 21:03:23 $
148
149
150
151perl v5.8.8 2006-03-17 MIME::Words(3)