Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

php - UTF-8 encoding for subject in contact form email

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:

<?
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
";
    $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;
    }

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

here is how i did it:

$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);

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/

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))

! this only works properly if your php file is utf-8 encoded !

still very annoying. then i switched to phpmailer. that does everything for you. way more easy. i would suggest you use that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...