简体   繁体   English

PHP UTF8邮件在iPhone邮件应用程序上出现乱码

[英]PHP UTF8 mail gets garbled on iPhone mail app

I am using php internal mail function to send mail, I wrapped it up in a function which UTF8 the process, I found it online. 我正在使用php内部邮件功能发送邮件,我把它包装在UTF8的一个功能过程中,我在网上发现了它。

it works, I sent the email in the code to my GMAIL and to my EXCHANGE server, both in GMAIL and my Outlook client on my PC I get the message just right. 它有效,我将代码中的电子邮件发送到我的GMAIL和我的EXCHANGE服务器,无论是在GMAIL还是我的PC上的Outlook客户端,我都得到了正确的信息。

My iPhone is also connected to the exchange and GMAIL accounts, so I get both messages on my iPhone. 我的iPhone也连接到交换机和GMAIL帐户,因此我在iPhone上收到这两条消息。 the iPhone receives the GMAIL message correctly, but the EXCHANGE account shows the message with garbled gibberish text where no-latin characters are. iPhone正确收到GMAIL消息,但EXCHANGE帐户显示带有乱码乱码文本的消息,其中没有拉丁字符。

the example bellow sends Hebrew characters in the subject and in the body. 下面的例子在主体和身体中发送希伯来字符。 On my iPhone, the subject gets received correctly in Hebrew, but the Hebrew parts in the body of the message are garbled. 在我的iPhone上,主题在希伯来语中被正确接收,但是消息正文中的希伯来语部分是乱码。

can anyone comment on that ? 谁能对此发表评论? maybe try it on your own setup ? 也许你自己设置尝试?

thanks, 谢谢,

<?php


function mail_utf8($to, $subject = '(No subject)', $message = '', $header = '') { 
$header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' .   "\r\n"; 
mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_ . $header);  
}

$mymessage = "Hello <BR>"; 
$mymessage.= "Received: now<BR/><BR/>";
$mymessage.= "details: שלום שלום<BR/><BR/>";

mail_utf8 ("XXX@gmail.com,XXX@exchangeServer.com", "שלום", $mymessage, "my_mail", "mailer@phpmail.com");


?>

I got the same problem with iPhone and Exchange, I solved my problem using Content-Transfer-Encoding: quoted-printable\\r\\n and quoted_printable_encode() try your code this way: 我在iPhone和Exchange上遇到了同样的问题,我使用Content-Transfer-Encoding: quoted-printable\\r\\n解决了我的问题Content-Transfer-Encoding: quoted-printable\\r\\nquoted_printable_encode()这种方式尝试你的代码:

function mail_utf8($to, $subject = '(No subject)', $message = '', $from ='') { 
  $headers = "MIME-Version: 1.0\r\n";
  $headers .= "Content-type: text/html; charset=UTF-8\r\n";
  $headers .= "Content-Transfer-Encoding: quoted-printable\r\n";
  $headers .= "From: $from\r\n";
  mail($to, $subject, $message, $headers);  
}
$mymessage = "Hello <BR>"; 
$mymessage.= "Received: now<BR/><BR/>";
$mymessage.= "details: שלום שלום<BR/><BR/>";
$mymessage = quoted_printable_encode($mymessage);

mail_utf8 ("XXX@gmail.com,XXX@exchangeServer.com", "שלום", $mymessage, "mailer@phpmail.com");

Because the message is correctly received on your gmail account through your iPhone this is probably a bug/misconfiguration on the exchange server or in the iPhone Exchange synchronisation. 由于通过iPhone在您的Gmail帐户上正确接收邮件,因此可能是Exchange服务器或iPhone Exchange同步中的错误/配置错误。

The best thing to do would be to base64 encode not only the subject but the whole message: the characters base64 uses are almost always transferred correctly since the character codes are the same in US-ASCII, ISO-8859-x and UTF-8. 最好的办法是base64编码不仅是主题而是整个消息:base64使用的字符几乎总是正确传输,因为字符代码在US-ASCII,ISO-8859-x和UTF-8中是相同的。 All modern mail clients can decode base64, but it does increase the size of your message by about 33%. 所有现代邮件客户端都可以解码base64,但它确实会使邮件大小增加约33%。

 function mail_utf8($to, $subject = '(No subject)', $message = '', $header = '') { 
      $header_ = 'MIME-Version: 1.0' . "\r\n" .
                 'Content-type: text/plain; charset=UTF-8' .   "\r\n" .
                 'Content-Transfer-Encoding: base64' . "\r\n"; 
      mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', base64_encode($message), $header_ . $header);  
 }

Simple answer - use Swift Mailer . 简单的答案 - 使用Swift Mailer The header magic in emails is unpredictable. 电子邮件中的标题魔法是不可预测的。 I've also tried playing around with it for a looong time but sooner or later, someone reported that on his setup (email provider or application), text is garbled or something (I send a lot of emails in czech, so working UTF is a priority). 我也试过玩它一段时间,但迟早有人报告说,在他的设置(电子邮件提供商或应用程序),文本是乱码或其他东西(我发送了很多捷克语的电子邮件,所以工作的UTF是优先权)。

Use Swift Mailer and save yourself some nerves. 使用Swift Mailer可以省去一些神经。 It's fast to setup and learn and works great. 设置和学习速度快,效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM