【问题标题】:multipart mail - accentuations problems多部分邮件 - 重音问题
【发布时间】:2010-11-11 22:32:41
【问题描述】:

我正在尝试通过 PHP 脚本发送多部分/替代 MIME 电子邮件...一切正常,但我在编码方面遇到了一些问题!电子邮件正文中的重音字符在邮件客户端中显示错误!如何对body进行编码来解决这个问题? ... 我试过用..

utf8_encode($body)

没有好的结果!

在某些原始格式的电子邮件中,我注意到重音被替换为 =XX(其中 XX 是字母数字字符)......我该怎么做?

提前致谢!

这是代码:

$header = "From: \n";
$header .= "Reply-To: \n";
$header .= "Content-Type: multipart/alternative; boundary=$alt_boundary\n"; 
$header .= "Mime-Version: 1.0\n";
$header .= "X-Mailer: PHP/".phpversion()."\n";

$body .= "\n".wordwrap($txt_body, 70);

$body .= "\n\n--$alt_boundary\n";
$body .= "Content-Type: multipart/mixed; boundary=$mixed_boundary\n";

$body .= "\n\n\n--$mixed_boundary\n";
$body .= "Content-Type: text/html; charset=utf-8\n";
$body .= "Content-Transfer-Encoding: 7bit\n";

$body .= "\n".wordwrap($html_body, 70);

$body .= "\n\n\n--$mixed_boundary\n";   
$body .= "Content-Disposition: attachment filename=\"test file\"\n";
$body .= "Content-Type: application/octet-stream; x-unix-mode=0644; name=\test file\"\n";
$body .= "Content-Transfer-Encoding: 7bit\n";

$body .="\n$file";

$body .= "\n\n--$mixed_boundary--";
$body .= "\n\n--$alt_boundary--"; 

mail($to, $subject, $body, utf8_encode($header));

编辑:

$txt_body$html_body 是两个文件的内容:

$txt_body = file_get_contents(...);
$html_body = file_get_contents(...);

在这些文件中,我替换了我通过 IPN 从 PayPal 收到的一些信息。我注意到,当我收到电子邮件时,只有 IPN 信息中出现的重音显示错误(换句话说,我在文件内容中替换的附加信息)!其他重音字符显示正确!!

我该如何解决这个问题?

已解决:

我已经解决了这个问题! utf8_encode() 函数必须仅适用于教皇信息变量,实际上是我尝试在 utf8 中编码 $txt_body ... paypal 变量在 utf8 中编码了 2 次。换句话说,我已经做到了:

$txt_body = utf8_encode(file_get_contents(...));
$html_body = utf8_encode(file_get_contents(...));

在 $txt_body 和 $html_body 中,我已经替换了通过 IPN 收到的信息!

感谢 ererybody!

【问题讨论】:

  • 在别无选择的情况下,为什么还要使用multipart/alternative?为什么不直接使用multipart/mixed
  • 您是否尝试过 quoted-printablebase64 传输编码?
  • 是的!问题是一样的!

标签: php utf-8 email mime mailto


【解决方案1】:

您需要在该特定部分的标题中声明您使用的字符编码:

MIME-Version: 1.0
Content-Type: multipart/alternative; boundary=boundary

--boundary
Content-Type: text/plain; charset=utf-8

This part uses UTF-8.
--boundary
Content-Type: text/plain; charset=iso-8859-1

This part uses ISO 8859-1.
--boundary--

【讨论】:

  • 感谢您的回复!是的,我使用 utf-8 作为字符集,但是当我收到邮件时,重音显示不正确!
  • 那么可能是您的数据不是 UTF-8 编码的。字符编码声明不会改变实际的编码。它只是改变了数据的解释方式。
【解决方案2】:

使用SwiftMailer 可以轻松创建utf-8 编码的多部分邮件

$message->addPart($txt_body, 'text/plain', 'utf-8');
$message->addPart($html_body, 'text/html', 'utf-8');
$message->attach(Swift_Attachment::fromPath('/path/to/testfile'));

【讨论】:

  • 但我的问题是仅对 paypal 通过 IPN 发送到我的 php 脚本的信息进行编码
猜你喜欢
  • 1970-01-01
  • 2010-11-10
  • 2014-09-27
  • 2015-10-27
  • 2018-11-03
  • 2018-03-06
  • 2011-04-01
  • 2014-11-14
  • 1970-01-01
相关资源
最近更新 更多