Oliver N. Oliver N. - 6 months ago 37
Perl Question

Email subject MIME encoding in Perl.

I am trying to send an email with non-ASCII characters in the subject line under Perl 5.8.5. My simple example uses the word "Änderungen" (German umlaut), but instead of correctly converting the "Ä" the subject line will always turn out as "Ã?nderungen".

#!/usr/bin/env perl

use warnings;
use strict;
use Encode qw(decode encode);

my $subject = "Änderungen";
my $subject_encoded = encode("MIME-Q", $subject);


open(MAIL, "| /usr/sbin/sendmail -n -t $recipient") || return "ERROR";
print MAIL 'Content-Type: text/plain; charset="utf-8"\n';
print MAIL "To: $recipient\n";
print MAIL "From: $from\n";
print MAIL "Reply-To: $from\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$body\n\n";
print MAIL ".\n";

The content of $subject_encoded prints as
while an online encoder tool shows that it actually should be

When manually constructing the subject string with the latter encoding the mail subject will correctly display "Änderungen" in my e-mail software so the problem seems to be with the actual Perl encode command. I am trying to utilize the quoted-printable encoding, but encoding via MIME-B and MIME-Header will also lead to the wrong representation of "Ã?nderungen".

I did check the file format of my codefile and the charset is also returned as utf-8. Therefore I am at a loss why Perl is apparently encoding it the wrong way. Any ideas or something I might have overlooked ?


Your editor treats the file as UTF-8, so it shows

my $subject = "Änderungen";

Perl effectively treats the file as iso-8859-1, so it sees

my $subject = "Ã?nderungen";

Tell Perl you encoded your script using UTF-8 by adding

use utf8;