PHP Mailer UTF-8编码问题

时间:2015-07-22 10:33:22

标签: php encoding utf-8 phpmailer mailer

我最近一直在网站上工作,并决定在其上加入PHPMailer。问题是我是匈牙利人,网站完全是匈牙利语,所以我不得不使用UTF-8字符集。我决定使用的PHP邮件程序尚不能使用UTF-8,我无法弄清楚如何使它在我喜欢的字符集中回显。请帮忙!

<?php

require_once('phpmailer/class.phpmailer.php');

$mail = new PHPMailer();

if( isset( $_POST['template-contactform-submit'] ) AND $_POST['template-contactform-submit'] == 'submit' ) {
    if( $_POST['template-contactform-name'] != '' AND $_POST['template-contactform-email'] != '' AND $_POST['template-contactform-message'] != '' ) {

        $name = $_POST['template-contactform-name'];
        $email = $_POST['template-contactform-email'];
        $phone = $_POST['template-contactform-phone'];
        $service = $_POST['template-contactform-service'];
        $subject = $_POST['template-contactform-subject'];
        $message = $_POST['template-contactform-message'];

        $subject = isset($subject) ? $subject : 'New Message From Contact Form';

        $botcheck = $_POST['template-contactform-botcheck'];

        $toemail = ''; // Your Email Address
        $toname = ''; // Your Name

        if( $botcheck == '' ) {

            $mail->SetFrom( $email , $name );
            $mail->AddReplyTo( $email , $name );
            $mail->AddAddress( $toemail , $toname );
            $mail->Subject = $subject;

            $name = isset($name) ? "Név: $name<br>" : '';
            $email = isset($email) ? "E-mail: $email<br><br>" : '';
            $message = isset($message) ? "Üzenet: $message<br>" : '';

            $referrer = $_SERVER['HTTP_REFERER'] ? '<br><br><br>Az üzenetet továbbította: ' . $_SERVER['HTTP_REFERER'] :'';

            $body = "$name $email $message $referrer";

            $mail->MsgHTML( $body );

            $mail->CharSet="UTF-8";

            $sendEmail = $mail->Send();

            if( $sendEmail == true ):
                echo 'Üzenet elküldve!';
            else:
                echo 'Üzenetküldés sikertelen. Kérlek próbáld újra!' . $mail->ErrorInfo . '';
            endif;
        } else {
            echo 'Bot vagy. Kérlek próbálkozz újra!';
        }
    } else {
        echo 'Kérlek tölts ki minden mezőt és próbáld meg újra!';
    }
} else {
    echo 'Valami félrecsúszott. Kérlek próbáld meg mégegyszer.';
}

?>

2 个答案:

答案 0 :(得分:4)

尝试替换:

$mail->CharSet="UTF-8";

由:

$mail->CharSet = 'UTF-8';

它在这里工作得很好。

答案 1 :(得分:0)

有用的是通过utf8_decode()php函数解码内容以发送内容。