在这个网站
Website Link联系表格我需要发送主题的电子邮件为UTF-8.
我们需要在代码中声明UTF-8编码的位置?
我们需要在代码中声明UTF-8编码的位置?
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:
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; }
在主题中我需要utf 8德国字符编码.我们在哪里需要在代码中声明它?对于消息,我发现该怎么做,但对于这个主题,我找不到解决方案.
这是我如何做到的:
$head = "From: \"=?ISO-8859-15?Q?".imap_8bit("äöüßÄÖÜ sollte hier gehen")."?=\" <info@mydomain.de>\n"; $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/
对于您的代码,您必须在sendmail类中更改它:
if (mail($this->to,'=?utf-8?B?'.base64_encode($this->subject).'?=',$this->headers))
还是很讨厌然后我切换到phpmailer.那为你做了一切.方式更容易.我建议你使用它.