Link to home
Start Free TrialLog in
Avatar of VapiSoft
VapiSoft

asked on

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.
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Show us your code and maybe we can help.
Avatar of actionmanager
actionmanager


@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
Avatar of VapiSoft

ASKER

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");


Did you also set the content encoding in your email form?
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).
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

try with below code

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

<?php
$heb = 'put hebrew text here';
$utf = preg_replace("/([\xE0-\xFA])/e","chr(215).chr(ord(\${1})-80)",$heb);
?>
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: ¿”¿•¿“¿¢¿” ¿¿¿¿¿¿™¿™¿˜¿¨¿™

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'.
try below
$text = 'put hebrew text here';
$text = mb_convert_encoding(trim($text), 'HTML-ENTITIES', 'UTF-8');
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
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
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
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! ;)
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

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.
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).
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.
specify your SMTP server in below line:

$mail->Host = "gmail.com";
Where do I do it?
I changed it (to vapisoft.com which is my server) and it didn't help (same problems).
ASKER CERTIFIED SOLUTION
Avatar of Swatantra Bhargava
Swatantra Bhargava
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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).
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.
Thank you.
Now it works.
So the only way it works is via SMTP and via the php mail.
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. ;)