Perl中的电子邮件主题MIME编码 [英] Email subject MIME encoding in Perl.

查看:177
本文介绍了Perl中的电子邮件主题MIME编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Perl 5.8.5的主题行中发送一封非ASCII字符的电子邮件。我的简单例子使用Änderungen(德文umlaut)这个词,而不是正确地转换Ä,主题行将永远变成Ã?nderungen。

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";
close(MAIL);

$ subject_encoded的内容打印为 =?UTF-8?Q? = C3 = 83 = C2 = 84nderungen?= 而在线编码器工具显示它实际上应该是 =?UTF-8?Q?= C3 = 84nderungen?=

The content of $subject_encoded prints as =?UTF-8?Q?=C3=83=C2=84nderungen?= while an online encoder tool shows that it actually should be =?UTF-8?Q?=C3=84nderungen?=.

当使用后者编码手动构建主题字符串时,邮件主题将在我的电子邮件软件中正确显示Änderungen,因此问题似乎与实际的Perl编码命令有关。我试图利用引用的可打印编码,但是通过MIME-B和MIME-Header进行编码也会导致Ã?nderungen的错误表示。

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".

我检查了我的代码文件的文件格式,字符集也返回为utf-8。所以我很失落,为什么Perl显然是编码错误的方式。任何想法或我可能忽略的东西?

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 ?

推荐答案

您的编辑器将该文件视为UTF-8,因此显示

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

my $subject = "Änderungen";

Perl将文件有效地视为iso-8859-1,所以它看到

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

my $subject = "Ã?nderungen";

通过添加

use utf8;

这篇关于Perl中的电子邮件主题MIME编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆