【问题标题】:Send UTF-8 encoded mail with Email::Sender使用 Email::Sender 发送 UTF-8 编码的邮件
【发布时间】:2015-08-24 10:00:39
【问题描述】:

我正在尝试使用包含变音符号的 Email::Sender 发送邮件。

#!/usr/bin/perl

use strict;
use warnings;

use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP::TLS;
use Email::Simple ();
use Email::Simple::Creator ();

open(my $mailbody, "<", "mail-content");

my $smtpserver = 'smtp.gmail.com';
my $smtpport = 587;
my $smtpuser   = 'example@mail.de';
my $smtppassword = 'password';

my $transport = Email::Sender::Transport::SMTP::TLS->new({
  host => $smtpserver,
  port => $smtpport,
  username => $smtpuser,
  password => $smtppassword,
});

my $email = Email::Simple->create(
  header => [
    To      => 'example@mail.de',
    From    => 'example@mail.de',
    Subject => 'Mail',
  ],
  body => <$mailbody>,
);

sendmail($email, { transport => $transport });

收到的邮件看起来像gegrÃŒÃt,而不是gegrüßt
有什么方法可以指定邮件是如何在 perl 中编码的?

【问题讨论】:

    标签: perl email utf-8


    【解决方案1】:

    你有

    open(my $mailbody, "<", "mail-content");
    

    如果文件mail-content包含UTF-8编码的数据,你必须用合适的IO层打开它:

    open my $mailbody, '<:encoding(UTF-8)', 'mail-content'
        or croak ...;
    

    此外,如果您的脚本源代码包含将包含在消息中的 UTF-8 字符串,则您的脚本中必须包含 use utf8;

    此外,如果任何标头字段包含 UTF-8 编码字符串,您也必须encode those strings

    【讨论】:

    • 好吧,创建一个简单的'mail-content' 文件,然后将 hexdump 粘贴到您的问题中。文件真的是 UTF-8 编码的吗?
    【解决方案2】:

    是的。在您的消息中添加Content-Type 行:

    Content-Type: text/html; charset=UTF-8
    

    您可能还需要在阅读邮件文件时指定输入编码:

    open ( my $input, "<:encoding(UTF-8)", "Your_File" ) or die $!; 
    

    【讨论】:

    • 添加两行完全没有改变。我刚刚注意到邮件在 Outlook.com 上正确显示,但在我的 iPhone 上的预览中却没有
    • 也许微软和 iPhone 不能很好地结合在一起。 ;)
    • @ProfGhost 更重要的是:您是否在E::S-&gt;create 的参数中正确添加了Content-Type 标头,还是将其放在邮件正文的前面?后者行不通。
    • @simbabque 放在邮件正文中,我会在创建中尝试。
    • 添加全套 MIME 标头。 MIME-Version: 1.0Content-Type: text/plain; charset=utf-8Content-Transfer-Encoding: 8bit
    【解决方案3】:

    刚刚遇到同样的问题,可以通过添加一个来解决它

    require Net::SMTP;
    

    在使用 Email::Simple 之前。这似乎与Email::Sender::Transport::SMTP 中的“解决方法”有关

    utf8::downgrade($next_hunk) if (Net::SMTP->VERSION || 0) < 3.07;
    

    如果Net::SMTP 未加载,则会解析为if (0 &lt; 3.07) 并导致降级。

    【讨论】:

      猜你喜欢
      • 2015-02-10
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      • 2011-12-31
      • 2012-11-23
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多