問題描述
在這個網站上網站鏈接聯系表我需要將主題發送到電子郵件UTF-8.需要在代碼的什么地方聲明UTF-8編碼?
On this sites Website Link contact 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?
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
";
$this->headers.= "MIME-Version: 1.0
";
$this->headers.= "Content-type: text/html; charset=UTF-8
";
}
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;
}
在主題中,我需要 utf 8 德語字符編碼.我們需要在代碼的什么地方聲明呢?對于消息,我發現了該怎么做,但對于主題,我沒有找到解決方案.
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.
推薦答案
我是這樣做的:
$head = "From: "=?ISO-8859-15?Q?".imap_8bit("??ü???ü sollte hier gehen")."?=" <info@mydomain.de>
";
$subject = "=?ISO-8859-15?Q?".imap_8bit("??ü???ü sollte hier gehen")."?=";
mail($mail,$subject,$text,$head);
這只是拉丁語 15(德語)編碼.utf-8 的工作方式相同:在此處查看有關如何在郵件標頭中使用字符編碼的詳細說明:http://ncona.com/2011/06/using-utf-8-characters-on-an-e-mail-subject/
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/
對于您的代碼,您必須在 sendmail 類中進行更改:
for your code you have to change this in the sendmail class:
if (mail($this->to, '=?utf-8?B?'.base64_encode($this->subject).'?=', $this->body, $this->headers))
!這僅在您的 php 文件是 utf-8 編碼時才能正常工作!
! this only works properly if your php file is utf-8 encoded !
還是很煩.然后我切換到 phpmailer.為你做一切.方式更容易.我建議你使用那個.
still very annoying. then i switched to phpmailer. that does everything for you. way more easy. i would suggest you use that.
這篇關于聯系表單電子郵件主題的 UTF-8 編碼的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!