php 联系表单电子邮件主题的 UTF-8 编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17064510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UTF-8 encoding for subject in contact form email
提问by Daniel Hernandez
On this sites Website Linkcontact form I need to send the subject for email in UTF-8. Where in the code we need to do declare the UTF-8 encoding?
在本网站的网站链接联系表上,我需要以 UTF-8 格式发送电子邮件主题。我们需要在代码中的哪个位置声明 UTF-8 编码?
kontakt.php:
kontakt.php:
<?
require_once "php/sendmail.class.php";
$sendmail = new sendMail();
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$sendmail->setParams($_POST);
$sendmail->parseBody();
$sendmail->setHeaders();
if ($sendmail->send())
{
header('Location: kontakt.php?success=1');
}
}
?>
sendmail.class.php:
sendmail.class.php:
class sendMail {
var $to = 'email'; // set contact email
var $name = '';
var $subject = '';
var $email = '';
var $body = '';
var $error = array();
var $headers = array();
function setHeaders()
{
$this->headers = "From: $this->email\r\n";
$this->headers.= "MIME-Version: 1.0\r\n";
$this->headers.= "Content-type: text/html; charset=UTF-8\r\n";
}
function parseBody()
{
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= '<tr style="background-color: #eee;"><td><strong>Name:</strong> </td><td>' . $this->name . '</td></tr>';
$message .= "<tr><td><strong>E-Mail-Adresse:</strong> </td><td>" . $this->email . "</td></tr>";
$message .= "<tr><td><strong>Betreff:</strong> </td><td>" . $this->subject . "</td></tr>";
$message .= "<tr><td><strong>Text:</strong> </td><td>" . $this->body . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
$this->body = $message;
}
function send()
{
if ($this->error)
{
return FALSE;
}
if (mail($this->to, $this->subject, $this->body, $this->headers))
{
return TRUE;
}
else
{
$this->error[] = 'Fehler beim senden';
return FALSE;
}
In the subject I need the utf 8 german characters encoding. Where do we need to declare it in the code? For the message I found out what to do but for the subject I found no solution.
在主题中,我需要 utf 8 德语字符编码。我们需要在代码的什么地方声明它?对于消息,我发现了该怎么做,但对于主题,我没有找到解决方案。
回答by x4rf41
here is how i did it:
这是我如何做到的:
$head = "From: \"=?ISO-8859-15?Q?".imap_8bit("??ü???ü sollte hier gehen")."?=\" <[email protected]>\n";
$subject = "=?ISO-8859-15?Q?".imap_8bit("??ü???ü sollte hier gehen")."?=";
mail($mail,$subject,$text,$head);
that is ofc just Latin-15 (German) encoding. utf-8 works the same way: look here for a great explanation on how to use character encoding in mail headers: http://ncona.com/2011/06/using-utf-8-characters-on-an-e-mail-subject/
那只是Latin-15(德语)编码。utf-8 的工作方式相同:在此处查看有关如何在邮件标头中使用字符编码的详细说明:http: //ncona.com/2011/06/using-utf-8-characters-on-an-e-邮件主题/
for your code you have to change this in the sendmail class:
对于您的代码,您必须在 sendmail 类中更改它:
if (mail($this->to, '=?utf-8?B?'.base64_encode($this->subject).'?=', $this->body, $this->headers))
! this only works properly if your php file is utf-8 encoded !
!这只有在您的 php 文件是 utf-8 编码时才能正常工作!
still very annoying. then i switched to phpmailer. that does everything for you. way more easy. i would suggest you use that.
还是很烦。然后我切换到phpmailer。为你做一切。方式更容易。我建议你使用它。
回答by Pavel Kenarov
Or you can replace a $this->subject variable with
或者您可以将 $this->subject 变量替换为
$this->subject = '=?windows-1251?B?'.base64_encode($this->subject).'?=';
$this->subject = '=?windows-1251?B?'.base64_encode($this->subject).'?=';
Just replace
只需更换
windows-1251
视窗-1251
with some other encoding ( utf-8 or something else )
使用其他编码(utf-8 或其他编码)