2

i have been trying to grab info from a form and send it to email. it works fine unless there is arabic in the form in which case the arabic parts are shown as gibberish in the email. here is my latest trial mail code:

require_once('PHPMailer/class.phpmailer.php');
require ('diet/contact.html');



$name = htmlentities($_POST['cf-name']);
$email = htmlentities($_POST['cf-email']);
$tel = htmlentities($_POST['cf-tel']);
$complaint = htmlentities($_POST['cf-complaint']);
$hour = htmlentities($_POST['cf-hour']);
$min = htmlentities($_POST['cf-min']);

if ($_POST['cf-am'] == 'am') {
    $tod = 'am';
} elseif ($_POST['cf-pm'] == 'pm') {
    $tod = 'pm';
}

$day = htmlentities($_POST['cf-day']);

if (isset($_POST['cf-info']) === true && empty($_POST['cf-info']) === false ) {
    $info = htmlentities($_POST['cf-info']);
}

$mail = new PHPMailer(true);
try {
    $mail->AddReplyTo($email, $name);
    $mail->AddAddress('email@email.com', 'Name');
    $mail->SetFrom('email@gmail.com', 'Name 2'); //
    $mail->Subject = 'موضوع';
    $mail->AltBody = 'برنامه شما از این ایمیل پشتیبانی نمی کند، برای دیدن آن، لطفا از برنامه دیگری استفاده نمائید'; // متنی برای کاربرانی که نمی توانند ایمیل را به درستی مشاهده کنند
    $mail->CharSet = 'UTF-8';
    $mail->ContentType = 'text/html';
    $mail->MsgHTML('<html>
<body><h1 style="color: #8b4513">New Contact</h1>
    <h3 style="color: #006400">Hello,</h3>
    You have just received a contact request with the following information:<br/><br/>
    <b>Name:</b> '.$name.'<br/>
    <b>Email</b>: '.$email.'<br/>
    <b>Telephone number:</b> '.$tel.'<br/>
    <b>Complaint:</b> '.$complaint.'<br/>
    <b>His preferred time to visit:</b> '.$hour.':'.$min.' '.$tod.'<br/>
    <b>His preferred day is:</b> '.$day.'<br/><br/>
    <b>Additional info:</b> '.$info.'<br/><br/>
    **You can reply directly to this email to email the contact.**<br/><br/>
    - Have a nice day</body></html>

');


    $mail->Send();
    echo "پیام با موفقیت ارسال شد\n";
}
catch (phpmailerException $e) {
    echo $e->errorMessage();
}
catch (Exception $e) {
    echo $e->getMessage();
}

the email works fine except for any arabic input from the form. ive searched a lot for a solution but nothing seems to output the arabic font. i use mac osx mail client and icloud webmail to check both dont see the arabic parts correcly. any help is appreciated.

Tikaa
  • 49
  • 1
  • 3
  • 11
  • possible duplicate of [php mail special characters utf8](http://stackoverflow.com/questions/19708097/php-mail-special-characters-utf8) – sunshinekitty Mar 29 '14 at 14:37

1 Answers1

2

Try the following, add charset right after you construct the object before specifying the Arabic text.

$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8';

Also trying encoding the body either way:

$mail->AltBody = "=?UTF-8?B?".base64_encode($arabicBody)."?=";
$mail->AltBody = "=?UTF-8?Q?".imap_8bit($arabicBody)."?=";
Aziz Saleh
  • 2,687
  • 1
  • 17
  • 27
  • thank you for you reply.i tried what you mentioned but to the html part not the altbody so: $mail->MsgHTML("=?UTF-8?B?".imap_8bit('

    – Tikaa Mar 29 '14 at 14:35
  • The text you are having the issue with is it in AltBody or the message? – Aziz Saleh Mar 29 '14 at 14:50
  • message in $mail->MsgHTML(' $name $complaint ../'); these variables are from the form and most likely will be typed in arabic by the user – Tikaa Mar 29 '14 at 14:59