Decapitatef Decapitatef - 17 days ago 5
PHP Question

not able to send arabic email with good characters

I want to send an arabic email using php, but it gives me others characters. How to send it with the arabic characters? This is my code:

<?php
if( isset($_POST['name']) )
{
$to = 'support@alkramlaundry.qa'; // Replace with your email

$subject = $_POST['subject'];
$message = $_POST['message'] . "\n\n" . 'Regards, ' . $_POST['name'] . '.';
$headers = 'From: ' . $_POST['name'] . "\r\n" . 'Reply-To: ' . $_POST['email'] . "\r\n" . 'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

if( $_POST['copy'] == 'on' )
{
mail($_POST['email'], $subject, $message, $headers);
}
}
?>

Answer
  1. you aren't specifying any Content-Type header so the recipient will use some default encoding
  2. values appended to the $additional_headers parameter of the mail() function need to be properly sanitized (or users may inject additional headers)

Modified code (I assume the encoding is UTF-8):

$filterHeaderValue = function ($value) {
    return str_replace(array("\r", "\n"), '', trim($value));
};

$subject = $_POST['subject'];
$message = $_POST['message'] . "\n\n" . 'Regards, ' . $_POST['name'] . '.';
$headers =
    "Content-Type: text/plain; charset=UTF-8\r\n" 
    . 'From: ' . $filterHeaderValue($_POST['name']) . "\r\n"
    . 'Reply-To: ' . $filterHeaderValue($_POST['email']) . "\r\n"
    . 'X-Mailer: PHP/' . phpversion()
;

mail($to, $subject, $message, $headers);

if ($_POST['copy'] == 'on') {
    mail($_POST['email'], $subject, $message, $headers);
}