通过PHP发送的电子邮件上的字符编码问题? [英] Problem with character encoding on email sent via PHP?

查看:96
本文介绍了通过PHP发送的电子邮件上的字符编码问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从PHP脚本发送格式正确的HTML电子邮件有一些麻烦。我在Windows XP Professional上运行PHP 5.3.0和Apache 2.2.11。



输出如下所示:


2010年4月20日星期二支持的代理摘要= 20
Ext。名称时间量

137代理名称01:27:25 1

138 = 09 00:00:00 0

139 = 09 00:00:00 0


你在那里看到= 20和= 09?如果你看HTML,你也会看到=符号变成= 3D。我认为这是一个字符编码问题,因为我在维基百科上看到以下内容:


ISO-8859-1和Windows-1252混淆

尽管数据实际上是Windows-1252编码,但使用字符集标签ISO-8859-1将文本数据错误标注是非常常见的。在Windows-1252中,0x80和0x9F之间的代码用于字母和标点符号,而它们是ISO-8859-1中的控制代码。许多网络浏览器和电子邮件客户端将将ISO-8859-1控制代码解释为Windows-1252字符,以适应这种错误标签,但不是标准行为,应注意避免在ISO-8859-1中生成这些字符标签的内容。


这看起来像是问题,但我不知道如何解决。我的代码如下所示:

  ob_start(); 
report_queue_summary($ yesterday,$ yesterday,$ first_extension,$ last_extension,$ queue);
$ body_report = ob_get_contents();
$ body_footer =这是一个自动生成的电子邮件。
$ message = new Mail_mime();
$ html = $ body_header $ body_report。$ body_footer;
$ message-> setHTMLBody($ html);
$ body = $ message-> get();
$ extraheaders = array(From=>***修改***,To=> $ recipient,Subject=&$昨天的代理摘要[$ queue] ,Content-type=>text / html; charset = iso-8859-1);
$ headers = $ message-> headers($ extraheaders);

#设置电子邮件; X-45454545 X- 20045 X-454545 X-454545 X-454545 X- 20045 X-4545 X-
$ port =26; 200新新新新旗新新新旗新新旗新新旗旗哨旗新新新新新新新新新旗新新旗新新旗新新款:

#发送电子邮件
$ smtp = Mail :: factory('smtp',
array('host'=> $ host,
'port '=> $ port,
'auth'=> true,
'username'=> $ username,
'password'=> $ password));

$ mail = $ smtp-> send($ recipient,$ extraheaders,$ body);

if(PEAR :: isError($ mail)){
echo(。$ mail-> getMessage()。);
} else {
echo(Message successfully sent!);
}

我正在使用输出缓冲的问题

解决方案

问题是您需要以下标题:

 内容转移编码:quoted-printable 


Having some trouble sending properly formatted HTML e-mail from a PHP script. I am running PHP 5.3.0 and Apache 2.2.11 on Windows XP Professional.

The output looks like this:

Agent Summary for Support on Tuesday April 20 2010=20 Ext. Name Time Volume
137 Agent Name 01:27:25 1
138 =09 00:00:00 0
139 =09 00:00:00 0

You see the =20 and =09 in there? If you look at the HTML you also see = signs being turned into =3D. I figure this is a character encoding issue as I read the following at Wikipedia:

ISO-8859-1 and Windows-1252 confusion

It is very common to mislabel text data with the charset label ISO-8859-1, even though the data is really Windows-1252 encoded. In Windows-1252, codes between 0x80 and 0x9F are used for letters and punctuation, whereas they are control codes in ISO-8859-1. Many web browsers and e-mail clients will interpret ISO-8859-1 control codes as Windows-1252 characters in order to accommodate such mislabeling but it is not standard behaviour and care should be taken to avoid generating these characters in ISO-8859-1 labeled content.

This looks like the problem but I don't know how to fix. My code looks like this:

ob_start();
report_queue_summary($yesterday,$yesterday,$first_extension,$last_extension,$queue);
$body_report = ob_get_contents();
ob_end_clean();
$body_footer = "This is an automatically generated e-mail.";
$message = new Mail_mime();
$html = $body_header.$body_report.$body_footer;
$message->setHTMLBody($html);
$body = $message->get();
$extraheaders = array("From"=>"***redacted***","To"=>$recipient, "Subject"=>"Agent Summary for $yesterday [$queue]", "Content-type"=>"text/html; charset=iso-8859-1");
$headers = $message->headers($extraheaders);

# setup e-mail;
$host = "*********";
$port = "26";
$username = "*****";
$password = "*****";

# Send e-mail
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($recipient, $extraheaders, $body);

if (PEAR::isError($mail)) {
  echo("" . $mail->getMessage() . "");
 } else {
  echo("Message successfully sent!");
 } 

Is the problem that I'm using output buffering?

解决方案

The problem is that you need the following header:

Content-Transfer-Encoding: quoted-printable

这篇关于通过PHP发送的电子邮件上的字符编码问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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