Jump to content
Larry Ullman's Book Forums

Problem With Encoding Email Subject For Arabic Langauge Using Mail()


Recommended Posts

Hello,

First, I would like to thank you for this useful book.

I have a problem reading the email subject in Arabic language when using the mail(). The content of the body is readable, it is only the subject.

here is what i get ุชุฌุฑุจุฉ ุงุณุฑุงู ุงูููู.

 

this is my code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Contact Us</title>

</head>

 

<body>

<h1>Contact me</h1>

<?php

if(isset($_POST['submit'])){

function spam_scrubber($value){

$very_bady = array('to:', 'cc:', 'bcc:', 'content-type:', 'mime-version:',

'multipart-mix:','content-transfer-encoding:');

foreach($very_bady as $v) {

if(stripos($value, $v) !==false)

return '';

}// end of foreach

$value = str_replace(array("\r", "\b", "%0a", "%0d"), '', $value);

return trim($value);

}// end of spam_scubber

//$scrubbed = array_map('spam_scrubber', $_POST);

$name = spam_scrubber($_POST['name']);

$email = spam_scrubber($_POST['email']);

$comment = spam_scrubber($_POST['comments']);

$subject = "ارسال إيميل";

if(!empty($email) && !empty($name) && !empty($comment) ) {

 

$body = "Name: {$name }\n\nComments: {$comment}";

$body= wordwrap($body, 70);

// send the email

mail('exmample@yahoo.com', $subject, $body, "from: {$email}");

// print a message

echo '<p><em>Thank you for contacting me. i will reply some day.</em></p>';

// clear $_POST[

$_POST = array();

 

}else {

echo '<p style="font-weight:bold; color: #c00;">Please fill out the form completely. </p>';

}

 

}// enf of if isset $_POST['submit

 

?>

<form action="contact.php" method="post">

<p>Name:<input type="text" name="name"/></p>

<p>Email address: <input type="email" name="email"/></p>

<p>Comments: <textarea name="comments" rows="5" cols="30"></textarea></p>

<p><input type="submit" name="submit" value="Send"/></p>

</form>

</body>

</html>

Link to comment
Share on other sites

Thanks Antonia and Larry, but the problem still the same.

This is what I did

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Contact Us</title>

</head>

 

<body>

<h1>Contact me</h1>

<?php

if(isset($_POST['submit'])){

function spam_scrubber($value){

$very_bady = array('to:', 'cc:', 'bcc:', 'content-type:', 'mime-version:',

'multipart-mix:','content-transfer-encoding:');

foreach($very_bady as $v) {

if(stripos($value, $v) !==false)

return '';

}// end of foreach

$value = str_replace(array("\r", "\b", "%0a", "%0d"), '', $value);

return trim($value);

}// end of spam_scubber

//$scrubbed = array_map('spam_scrubber', $_POST);

$name = spam_scrubber($_POST['name']);

$email = spam_scrubber($_POST['email']);

$to = "myemail@live.ca";

$comment = spam_scrubber($_POST['comments']);

$subject = " تجربة اسرال ايميل";

$header = "From:{$email}\r\n";

$header .= "Reply-to: {$email}\r\n";

$header .= "X-Mailer: PHP/". phpversion();

$header .= "Content-type: text/plain; charset=\"UTF-8\"\n";

if(!empty($email) && !empty($name) && !empty($comment) ) {

 

$body = "Name: {$name }\n\nComments: {$comment}";

$body= wordwrap($body, 70);

// send the email

mail($to, $subject, $body, $header);

// print a message

echo '<p><em>Thank you for contacting me. i will reply some day.</em></p>';

// clear $_POST[

$_POST = array();

 

}else {

echo '<p style="font-weight:bold; color: #c00;">Please fill out the form completely. </p>';

}

 

}// enf of if isset $_POST['submit

 

?>

<form action="contact.php?id=<?php echo base64_encode(14); ?>" method="post">

<p>Name:<input type="text" name="name"/></p>

<p>Email address: <input type="email" name="email"/></p>

<p>Comments: <textarea name="comments" rows="5" cols="30"></textarea></p>

<p><input type="submit" name="submit" value="Send"/></p>

</form>

</body>

</html>

 

 

 

Any suggestions?

Link to comment
Share on other sites

Well...you can start by reading the forum guidelines. In it, I say:

 

PLEASE DO NOT:

  • ...post messages that add nothing to the description of the problem (like just posting "Help please?" or "Anyone?").

Obviously if someone could help, they would, right? So you don't need to post a message like this. This is one of my days to answer questions and I'm hoping to find the time to do some research to find you an answer, because I don't know it offhand. In the meantime, please be patient.

Link to comment
Share on other sites

Well...you can start by reading the forum guidelines. In it, I say:

 

PLEASE DO NOT:

  • ...post messages that add nothing to the description of the problem (like just posting "Help please?" or "Anyone?").

Obviously if someone could help, they would, right? So you don't need to post a message like this. This is one of my days to answer questions and I'm hoping to find the time to do some research to find you an answer, because I don't know it offhand. In the meantime, please be patient.

sorry

It is my fault that I did not read the guideline.

Link to comment
Share on other sites

It's okay. As for an answer, I haven't personally tested this, but it looks promising:

http://bitprison.net/php_mail_utf-8_subject_and_message

I think the application of base 64 encoding is the key.

 

If you look at the user comments for the mail() function (http://php.net/manual/en/function.mail.php), you'll see other recommendations, including the use of the mb_encode_mimeheader() function (http://php.net/manual/en/function.mb-encode-mimeheader.php).

 

Let us know if that helps and if you find a solution or need more assistance.

Link to comment
Share on other sites

bahaa, I have never sent non-English subjects via the mail function, and I don't have any means with which to test out anything at the moment, but I would recommend consulting the page about the mail function on PHP.net (http://php.net/manual/en/function.mail.php).

 

While quickly scanning through, I found the following:

 

Italian users cursing against "È" and other uppercase-accented-vowels ("vocali maiuscole accentate"") in subjects! While the lowercase ones ("è", "é" and so on) work as expected, qmail doesn't handle the uppercase ones. 

To fix it, the only way I found was this: 

<?php 

function mail_utf8($to, $subject = '(No subject)', $message = '', $header = '') { 
 $header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n"; 
 mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_ . $header); 
} 

?> 

It should apply to other languages too.

 

It definitely seems like this is an issue with header information and the wrong encoding being used.

 

And here is one more decent link for good measure:

 

http://bytes.com/topic/php/answers/745913-how-send-subject-chinese-using-mail

 

Please try these out, and then let us know if it works.

 

Edit: Dangit, Larry! You gotta stop writing the answer in at the same time I do, and then beat me to the punch in actually posting your answer.

  • Upvote 1
Link to comment
Share on other sites

  • 2 months later...

Thanks Larry and Hartley San

 

It worked fine with this '=?UTF-8?B?'.base64_encode($subject).'?='

 

dear bahaa,

I am also sufferng with this encoding problem.

and I tried '=?UTF-8?B?'.base64_encode($subject).'?='

but still there is no effect, I have a feedback form that returns me Name, Email, Regions, Subject and Comments in Arabic letter (genrally).

client, is windows live user and there is showing only ط¹ط¨ط¯ظ‡ ط¬ط§ط¨ط± (like this)

where I put this function I give you whole cod if needed.

Link to comment
Share on other sites

  • 2 weeks later...

solved!


    $header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n"; 
    $header .= "From : "	."=?UTF-8?B?".base64_encode ($name). "?=" . "<" .$email. ">\r\n";
    $body = "Enquiry :" . "\r\n" . "\r\n" .
    $comments . "\r\n" . "\r\n" . "\r\n" ."\r\n" . 
    "City / Country : " . $region . "\r\n";

    mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=',$body, $header_ . $header);

 

Thanks

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...