Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Putting a utf-8 subject into a mail header

by talexb (Chancellor)
on Feb 27, 2009 at 15:01 UTC ( [id://746901]=perlquestion: print w/replies, xml ) Need Help??

talexb has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to put a Korean subject line into an E-Mail that I sending using a Perl module, and I'm not having much success. The body of the E-Mail is Korean, and it's coming out just fine, but I am stuck on getting the Korean subject line to come out right.

I'm running Kubuntu 8.10, and I have Perl 5.8.8, so it's a relatively recent build. Brother clinton was kind enough to point me towards the Encode module, and it seemed to do the right type of thing, but after some further testing, isn't really.

My Sample string is 'XYZ Comfortable Furniture BNC account activation' which Google Translate assures me translates to 'XYZ 편안한 가구에서 BNC 계정 활성화'. When I put this is a subject line of an E-Mail in Thunderbird and send it to myself, it gets encoded into

Subject: XYZ =?UTF-8?B?7Y647JWI7ZWcIOqwgOq1rOyXkOyEnCBCTkMg6rOE7KCVIO2 +ZnOyEsQ==?= =?UTF-8?B?7ZmU?=
When I try to encode the same thing at the command line using my encode script (1), I get
talexb@foo:~/bin$ encode "XYZ 편안한 가&#44396 +;에서 BNC 계정 활성화" =?UTF-8?B?WFlaIMOtwo7CuMOswpXCiMOtwpXCnCDDqsKwwoDDqsK1wqzDrMKXwpDDrMK +EwpwgQk5DIMOqwrPChMOswqDClSDDrcKZwpzDrMKEwrHDrcKZwpQ=?=
Different. That's puzzling.

Using the decode (2) script on these two strings, first the Thunderbird encoding ..

talexb@foo:~/bin$ decode =?UTF-8?B?7Y647JWI7ZWcIOqwgOq1rOyXkOyEnCBCTkM +g6rOE7KCVIO2ZnOyEsQ==?= =?UTF-8?B?7ZmU?= 편안한 가구에서 BNC 계 +정 활성 화
.. I can see that Thunderbird's encoded string decodes nicely, but my encoded string does not:
talexb@foo:~/bin$ decode =?UTF-8?B?WFlaIMOtwo7CuMOswpXCiMOtwpXCnCDDqsK +wwoDDqsK1wqzDrMKXwpDDrMKEwpwgQk5DIMOqwrPChMOswqDClSDDrcKZwpzDrMKEwrHD +rcKZwpQ=?= XYZ í¸ìí ê°êµ¬ìì BNC ê³ì  íì±í
Please help me to understand why Thunderbird's encoding works but my version using Encode does not.


  1. #!/usr/bin/perl -wC # Encode to utf-8. use Encode qw/encode/; { my @out; foreach my $arg (@ARGV) { push(@out, encode("MIME-Header",$arg)); } print join(' ',@out) . "\n"; }
  2. #!/usr/bin/perl -w # Decode from utf-8. use Encode qw/decode/; { my @out; foreach my $arg (@ARGV) { push(@out, decode("MIME-Header",$arg)); } # See http://www.perlmonks.org/?node_id=742727 for why I've used +binmode # here. binmode STDOUT, ':utf8'; print join(' ',@out) . "\n"; }

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Replies are listed 'Best First'.
Re: Putting a utf-8 subject into a mail header
by Anonymous Monk on Feb 27, 2009 at 15:23 UTC
    Try uppercase "UTF-8"
        Try uppercase "UTF-8"

      I tried it in the decode script and got

      Unknown PerlIO layer "UTF8" at /home/talexb/bin/decode line 17.
      Is there somewhere else that you think the case of 'utf' matters?

      Alex / talexb / Toronto

      "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

        Sorry, i was thinking
        binmode STDOUT, ':encoding(UTF-8)';
        might make a difference, but you need
        encode("MIME-Header",decode('UTF-8',$arg)));

        To expand on what Anonymous Monk said, @ARGV is one of those things in Perl (like filenames), where Unicode isn't handled automatically, so you need to explicitly decode your input.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://746901]
Approved by olus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 09:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found