Joro Seksa
Joro Seksa

Reputation: 1583

Php mail body not encoded correctlly

I have a wev site with a form for sending emails. Unfortunatelly today i got e-mail from a client but the message body was unreadable because it couldn't display cyrilic symbols properly.

Here is the php code:

<?php
$owner_email = $_POST["owner_email"];
$headers = 'From:' . $_POST["email"];
$subject = 'Имате съобщение изпратено през формуляра на вашия WEB сайт от ' . $_POST["name"];
$messageBody = "";

if($_POST['name']!='nope'){
    $messageBody .= '<p>Visitor: ' . $_POST["name"] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}
if($_POST['email']!='nope'){
    $messageBody .= '<p>Email Address: ' . $_POST['email'] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}else{
    $headers = '';
}
if($_POST['state']!='nope'){        
    $messageBody .= '<p>State: ' . $_POST['state'] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}
if($_POST['phone']!='nope'){        
    $messageBody .= '<p>Phone Number: ' . $_POST['phone'] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}   
if($_POST['fax']!='nope'){      
    $messageBody .= '<p>Fax Number: ' . $_POST['fax'] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}
if($_POST['message']!='nope'){
    $messageBody .= '<p>Message: ' . $_POST['message'] . '</p>' . "\n";
}

if($_POST["stripHTML"] == 'true'){
    $messageBody = strip_tags($messageBody);
}

try{
    if(!mail($owner_email, $subject, $messageBody, $headers)){
        throw new Exception('mail failed');
    }else{
        echo 'mail sent';
    }
}catch(Exception $e){
    echo $e->getMessage() ."\n";
}
?>

I am new to php so the question is:

What should i change in this code to make it work with cyrilic format?

Upvotes: 0

Views: 1416

Answers (4)

cyphar
cyphar

Reputation: 2890

Add \nContent-type: text/plain; charset=\"utf-8\"\nContent-Transfer-Encoding: 8bit to the end of your $header variable (you can do this by using the .= operator).

Cyrillic characters do not exist in plain ascii, so you need to use different character encoding.

Upvotes: 0

powtac
powtac

Reputation: 41040

Add this after your $headers = ... line:

$headers .= "\n".'MIME-Version: 1.0'."\n".'Content-Type: text/html; charset="UTF-8";'."\n".'Content-Transfer-Encoding: 7bit';

Upvotes: 1

Heberfa
Heberfa

Reputation: 441

Try

htmlentities($_POST['message']) instead only $_POST['message']

Upvotes: 0

Solon
Solon

Reputation: 362

try

$header .= "\nContent-type: text/plain; charset=\"utf-8\"";

and save this file in utf-8

Upvotes: 0

Related Questions