0

I'm having problem with setting to utf-8 in php with this contact form:

    <?php header('Content-type: text/plain; charset=utf-8');

require 'PHPMailer-master/PHPMailerAutoload.php';

$fromEmail = 'test@domain.com';
$fromName = 'test form';

$sendToEmail = 'email@outlook.com';
$sendToName = 'TEST';

$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');

$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';

error_reporting(E_ALL & ~E_NOTICE);

try
{

    if(count($_POST) == 0) throw new \Exception('Form is empty');

    $emailTextHtml = "<h1>You have a new message from your contact form</h1><hr>";
    $emailTextHtml .= "<table>";

    foreach ($_POST as $key => $value) {
        if (isset($fields[$key])) {
            $emailTextHtml .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";
        }
    }
    $emailTextHtml .= "</table><hr>";
    $emailTextHtml .= "<p>Have a nice day,<br>Best,<br>Ondrej</p>";

    $mail = new PHPMailer;

    $mail->setFrom($fromEmail, $fromName);
    $mail->addAddress($sendToEmail, $sendToName);
    $mail->addReplyTo($from);

    $mail->isHTML(true);

    $mail->Subject = $subject;
    $mail->msgHTML($emailTextHtml);


    if(!$mail->send()) {
        throw new \Exception('I could not send the email.' . $mail->ErrorInfo);
    }

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $e->getMessage());
}


if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
else {
    echo $responseArray['message'];
}

When I send the message I get ths: 'Сандрина'. I read all the forums and google results but I don't get it. I tried adding "header('Content-type: text/plain; charset=utf-8');" almost everywhere but it didn't help.

Please help :)

ss777
  • 1
  • 1
    read https://stackoverflow.com/questions/279170/utf-8-all-the-way-through?rq=1 – Jeff Dec 15 '17 at 20:44
  • 2 possible issues here: you send something (namely some blanks) before ``, as it looks like here (could be a copy&paste issue though). Maybe the php file itself isn't encoded in utf-8 – Jeff Dec 15 '17 at 20:46
  • You need to clarify if you're getting the garbled text in the HTML response your page generates, or in the email that you send, or both. They are two separate problems with different solutions – Sammitch Dec 15 '17 at 20:52
  • It's in the email message. – ss777 Dec 15 '17 at 21:02
  • 1
    Then setting an HTTP header is not going to solve the problem, you need to configure PHPMailer to set UTF8 encoding on the message. `$mail->CharSet = 'UTF-8';` https://stackoverflow.com/questions/2491475/phpmailer-character-encoding-issues – Sammitch Dec 15 '17 at 21:12
  • It worked. Thank u very much. :) – ss777 Dec 15 '17 at 22:00

0 Answers0