我正在尝试以utf-8编码发送包含俄语文本和主题的电子邮件。正在收到电子邮件,当我在gmail的web界面中打开它时,一切都是正确的。但是当我在“蝙蝠”客户端打开电子邮件时,默认情况下编码不正确(我可以手动将其设置为utf-8):
Subject: "Hello. Текст"
Body: "test email. Русский текст"
而不是:
Subject: "Hello. Текст"
Body: "test email. Русский текст"
代码:
#!/usr/bin/perl
use utf8;
use strict;
use warnings;
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP ();
use Email::Simple ();
use MIME::Base64 qw( encode_base64 );
use open ':std', ':encoding(UTF-8)';
sub send_email
{
my $email_from = shift;
my $email_to = shift;
my $subject = shift;
my $message = shift;
my $smtpserver = 'smtp.gmail.com';
my $smtpport = 465;
my $smtpuser = 'user@gmail.com';
my $password = 'secret';
my $transport = Email::Sender::Transport::SMTP->new({
host => $smtpserver,
port => $smtpport,
sasl_username => $smtpuser,
sasl_password => $password,
debug => 1,
ssl => 1,
});
my $email = Email::Simple->create(
header => [
To => $email_to,
From => $email_from,
Subject => $subject,
],
body => $message,
);
$email->header_set( 'Content-Type' => 'text/html' );
$email->header_set( 'charset' => 'UTF-8' );
sendmail($email, { transport => $transport });
}
my $body = Encode::encode('utf-8', 'test email. Русский текст');
my $subject = Encode::encode('utf-8', 'Hello. Текст');
send_email('user@gmail.com', 'user@gmail.com', $subject, $body);
如何告诉电子邮件客户端编码是utf-8?
答案 0 :(得分:1)
Email::Sender
/ Net::SMTP
字符串 Email::Sender
(Net::SMTP
)需要see answer提到的字节(user4035):
my $msg = $email->as_string();
utf8::encode($msg) if utf8::is_utf8($msg);
sendmail($msg, ...);
为" raw"设置所有三个MIME标头utf-8电子邮件正文:
(您可以使用text/html
代替典型的text/plain
)
$email->header_set( 'MIME-Version' => '1.0' );
$email->header_set( 'Content-Type' => 'text/plain; charset=utf-8' );
$email->header_set( 'Content-Transfer-Encoding' => '8bit');
您的SMTP服务器应该接受它并从" raw"进行转换。 (8位)utf-8到另一个编码的电子邮件,如有必要。大多数现代电子邮件服务器都这样做 请参阅Steffen Ullrich关于电子邮件提供商[1& 1(GMX)]中值得注意的例外情况的评论。
$email->header_raw_set( 'Subject' => Encode::encode('MIME-Header',$subject));
创建最小Email::Simple
条消息,在utf-8终端上打印
(print $email->as_string();
)并发布结果。