• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

php mail for none-English characters.

Hi,
I want to send an email in Hebrew from a PHP server.
I fetch the text from MySQL table.
I set the Text "collation" of the text-column (in the MySQL table) to "hebrew_general_ci".
But when I recive the mail I see gibrish instead of the text.
0
VapiSoft
Asked:
VapiSoft
  • 12
  • 5
  • 3
  • +3
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
Show us your code and maybe we can help.
0
 
actionmanagerCommented:

@mysql_query('set character_set_client="hebrew"');

@mysql_query('set character_set_results="hebrew"');

@mysql_query('set collation_connection="hebrew_general_ci"');


and try php iconv function for text encoding
0
 
VapiSoftAuthor Commented:
to actionmanager:

I tried the
@mysql_query('set character_set_client="hebrew"');
@mysql_query('set character_set_results="hebrew"');
@mysql_query('set collation_connection="hebrew_general_ci"');
and it didn't change anything.

about the iconv, I dont know what parameters to give.
as I wrote, the current text encoding (in the PHP 'collation' is 'hebrew_general_ci");


0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Dave BaldwinFixer of ProblemsCommented:
Did you also set the content encoding in your email form?
0
 
VapiSoftAuthor Commented:
What do you mean?
 I receive mails in Hebrew and I intend to send the mail only to people in Israel (that their client is programmed to receive mail in Hebrew).
0
 
Dave BaldwinFixer of ProblemsCommented:
If you are using PHP's mail() function. look at this from http://php.net/manual/en/function.mail.php.  I think that 'plain' email is US-ASCII character set.  You might want to look at the headers in your email client and see what they are using.
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

Open in new window

0
 
Swatantra BhargavaTechnology no BarCommented:
try with below code

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
0
 
actionmanagerCommented:
and

<?php
$heb = 'put hebrew text here';
$utf = preg_replace("/([\xE0-\xFA])/e","chr(215).chr(ord(\${1})-80)",$heb);
?>
0
 
VapiSoftAuthor Commented:
I tried both headers and it work.
Now it also adds the headers to the email content and it doesn't look so good.

Something is wrong here. I added also the 'From:' to the headers and I don't see it in the email title (It is empty) but I get it in the body as:
Content-type: text/html; charset=iso-8859-1
From: MyTree <info@vapisoft.com>
X-Mailer: PHP/5.2.6-pl7-gentoo
Message-Id: <20100902075622.8FAF7B152@mx1.lingocom.com>
Date: Thu,  2 Sep 2010 10:56:22 +0300 (IDT)
X-Spam-Prev-Subject: ¿”¿•¿“¿¢¿” ¿¿¿¿¿¿™¿™¿˜¿¨¿™

0
 
VapiSoftAuthor Commented:
To: actionmanager
I tried it and it doesn't work.
I understannd that you want to convert it from utf8, but the 'collation' in the table is 'utf8'.
0
 
Swatantra BhargavaTechnology no BarCommented:
try below
$text = 'put hebrew text here';
$text = mb_convert_encoding(trim($text), 'HTML-ENTITIES', 'UTF-8');
0
 
VapiSoftAuthor Commented:
To:

Now I get it like that (so I assume It does something)
&#1497;&#1513; &#1500;&#1498; &#1514;&#1490;&#1493;&#1489;&#1492; &#1500;&#1492;&#1493;&#1491;&#1506;&#1492; &#1513;&#1492;&#1513;&#1488;&#1512;&#1514; &#1489;&#1502;&#1488;&#1497;&#1497;&#1496;&#1512;&#1497;\n&#1500;&#1512;&#1488;&#1493;&#1514; &#1488;&#1514; &#1492;&#1514;&#1490;&#1493;&#1489;&#1492; &#1500;&#1495;&#1509; &#1506;&#1500; &#1492;&#1500;&#1497;&#1504;&#1511;:\r\n\r\nhttp://www.vapisoft.com/MyTree/main.php?link=1,809.810|804.805|10.13|2.6,0,820,808,809:2:810:c85:52
0
 
Shinesh PremrajanEngineering ManagerCommented:
wts the code for the mail compose, jsut paste it here. it need to set the content type as utf-8, and it will work.

in the HTML mail it need to have sopmething like this

<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8“ >

Hope this helps
0
 
Shinesh PremrajanEngineering ManagerCommented:
Step 1) Set the HTML page charset encoding to utf8:

    meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8“

Step 2) set the ‘collation’ attribute for the fields which are to accept hebrew text to: utf8_bin

Step 3) within your php script, right before calling the mysql_query function to insert the data into your DB, use both the utf8_encode function along with addslashes. This will encode the data into utf8 friendly characters.

Step 4) when pulling the text from the DB onto the page, make sure to use the utf8_decode along with stripslashes in order to display the data on the page.

hope this helps
0
 
JtRCommented:
hehe shinug, you missed step 5,6,7,8 ;)

so there is a lot more to take care of when you handle international charsets like unicode

5) the header of the html page needs to specify the right codation. normally if you take HTML 4 strictly then the page falls back to not unicode as soon you have a not unicode char inside it

6) you have to have enabled the PHP extension "mbstring" - otherwise the standard string functions would "scramble" your utf8 codes (for example if you use split() on a utf8 string)

7) is to use the W3C validator at the html output. if there is ANY SINGLE non valid coded char in your output page then the (depending on the browser) it will show you question marks, or whatever.

8) you should test with more than 1 email client.. for example i wasted a lot of time to find out that outlook (up to 2007) can send utf8 text mails but cannot receive them .. so in outlook it happens that you get ???? in the subject line.. so better only use HTML mails (but maybe this comment is obsolete nowadays) ;)

9) if you get any strange html output then always use

http://validator.w3.org/ 

to find out if there are invalid characters in your page.


ah, i forgot step 10 : you need a lot of coffee and good luck! ;)
0
 
VapiSoftAuthor Commented:
To shinug:
1. I Inserted the text manually to the table from 'phpmyadmin'.
2. I set the collation as you wrote 'utf8_bin'
3. I added the utf_decode and the header (see attached code).

I get the following email.

Content-type: text/html; charset=UTF-8
Message-Id: <20100902100609.A39718D11@mx1.lingocom.com>
Date: Thu,  2 Sep 2010 13:06:09 +0300 (IDT)
From: apache@lingocom.com (apache)
X-Spam-Prev-Subject: ????? ????????


?? ?? ????? ?????? ?????? ????????\n????? ?? ?????? ??? ?? ?????:\r\n\r\nhttp://www.vapisoft.com/MyTree/main.php?link=1,809.810|804.805|10.13|2.6|16.738|1109.738,0,0,0,809:1:809:c85:57

$headers  = 'MIME-Version: 1.0' . "\r\n";
           $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
	   	  	
           $title = utf8_decode($text[1]); // mb_convert_encoding(trim($text[1]), 'HTML-ENTITIES', 'UTF-8');
           $body  = utf8_decode($text[2]); // mb_convert_encoding(trim($text[2]), 'HTML-ENTITIES', 'UTF-8');  	  	
	   	   mail ($user_mail, $title,$body,$headers);

Open in new window

0
 
JtRCommented:
just one question "off the discussion thread":

have you thought about saving hours and using a framework for this

like Zend Mail (Zend Framework)
http://framework.zend.com/manual/de/zend.mail.html

or phpMailer
http://phpmailer.worxware.com/

HTML MIME message encoding is really a time consuming game.. ins this case you really should think about not to invent the wheel for second millionst time.
0
 
VapiSoftAuthor Commented:
OK, I am downloading phpMailer.I hope it will solve my problems.
It looks like a large code, is it also CPU consuming to use it?
I need something that will be quick enough so I can use it in a social-network with many simultaneous users (I use it to notify the user on a response their comments).
0
 
Swatantra BhargavaTechnology no BarCommented:
try attached code
multiplemailer.zip
0
 
VapiSoftAuthor Commented:
Hi koolswans:
I tried the code. I run the: email_attachment_multiple.php and recived a page.
I filled the input boxes (without the attachment) and clicked the 'Sent' button.
I received  "The website cannot display the page" error.
and received no mail.
0
 
Swatantra BhargavaTechnology no BarCommented:
specify your SMTP server in below line:

$mail->Host = "gmail.com";
0
 
VapiSoftAuthor Commented:
Where do I do it?
I changed it (to vapisoft.com which is my server) and it didn't help (same problems).
0
 
Swatantra BhargavaTechnology no BarCommented:
1. Replace below variable in class.phpmailer.php

var $CharSet           = "iso-8859-1";

To

var $CharSet           = "UTF-8";

2. Replace below code in email_attachment_multiple.php page

<?php
include("class.smtp.php");
include("class.phpmailer.php");
if($_POST['hdnAction'] == "send")
{
      /* Creating Mail class object */
      $mail = new PHPMailer();
      
      /* SMTP Host Information */
      $mail->IsSMTP();
      $mail->Host = "vapisoft.com";
      $mail->SMTPAuth = false;
      $mail->Username = "";
      $mail->Password = "";
      /*******************************************/
      
      $mail->From = $_POST["sender_name"];
      $mail->FromName = $_POST["sender_email"];
      
      
      /* Mail send to */
      $to = $_POST["receiver_mail"];
      $mail->AddAddress($to);
      
      /* Mail subject */
      $subject = $_POST["subject"];
      $mail->Subject = $subject;
      
      /* Mail CC */
      if(isset($_POST["cc"])&&($_POST["cc"]!=""))      
            $mail->AddCC($_POST["cc"]);
      
      /* Mail BCC */            
      if(isset($_POST["bcc"])&&($_POST["bcc"]!=""))      
            $mail->AddBCC($_POST["bcc"]);
      
      /* Mail In HTML Format */
      if(isset($_POST["isHtml"])&&($_POST["isHtml"]=="yes"))
      {
            $msg = "Comments: ".$_POST["remarks"]."      ";
            
            $mail->IsHTML(true);
      }
      else
      {
            $msg = $_POST["remarks"];
            $mail->IsHTML(false);
      }

      /* Mail Message */
      $mail->Body = $msg;
      
      if($mail->Send())
      {
            $message = "Thanks for your feedback.";
            header("location:email_attachment_multiple.php?message=".urlencode($message));
            exit();
      }
      else
      {
            $message="Error has been occured. Your feedback can't send.";
      }
      echo($message);
      die();
}
?>
0
 
VapiSoftAuthor Commented:
Finally (after a long time) it works well.
Thank you very much.
I will incorporate it in my code and then Iwill give you the point (just in case I will run in to problems).
0
 
VapiSoftAuthor Commented:
I am trying to include this in my PHP file.
When I include the include files (even without anything else) it doesn't execute the file.

I see that you are using the PHP mail function.
This means that  don't need all this code I also copied your headers
so I think that my problem is the fetching of the text from the data base.
0
 
VapiSoftAuthor Commented:
Thank you.
Now it works.
So the only way it works is via SMTP and via the php mail.
0
 
JtRCommented:
ah yes, still one thing: if you really want to use it "scalable" than you should write a kind of messaging queue service that runs in the background.. so best first to put the message into the database and then let a cronjob working in the background sending the messages to the recipients! at the beginning you can implement this cronjob using the phpmailer and later you can use a more performant mailer without changing the rest. ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 12
  • 5
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now