Solved

php mail for none-English characters.

Posted on 2010-09-01
27
323 Views
Last Modified: 2013-12-13
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
Comment
Question by:VapiSoft
  • 12
  • 5
  • 3
  • +3
27 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 33583859
Show us your code and maybe we can help.
0
 

Expert Comment

by:actionmanager
ID: 33583871

@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
 

Author Comment

by:VapiSoft
ID: 33584120
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 33584133
Did you also set the content encoding in your email form?
0
 

Author Comment

by:VapiSoft
ID: 33584191
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 33584228
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
 
LVL 7

Expert Comment

by:Swatantra Bhargava
ID: 33584286
try with below code

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

Expert Comment

by:actionmanager
ID: 33584385
and

<?php
$heb = 'put hebrew text here';
$utf = preg_replace("/([\xE0-\xFA])/e","chr(215).chr(ord(\${1})-80)",$heb);
?>
0
 

Author Comment

by:VapiSoft
ID: 33584394
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
 

Author Comment

by:VapiSoft
ID: 33584435
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
 
LVL 7

Expert Comment

by:Swatantra Bhargava
ID: 33584459
try below
$text = 'put hebrew text here';
$text = mb_convert_encoding(trim($text), 'HTML-ENTITIES', 'UTF-8');
0
 

Author Comment

by:VapiSoft
ID: 33584530
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
 
LVL 17

Expert Comment

by:shinuq
ID: 33584682
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 17

Expert Comment

by:shinuq
ID: 33584710
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
 
LVL 3

Expert Comment

by:JtR
ID: 33584943
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
 

Author Comment

by:VapiSoft
ID: 33585169
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
 
LVL 3

Expert Comment

by:JtR
ID: 33585240
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
 

Author Comment

by:VapiSoft
ID: 33585284
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
 
LVL 7

Expert Comment

by:Swatantra Bhargava
ID: 33585313
try attached code
multiplemailer.zip
0
 

Author Comment

by:VapiSoft
ID: 33585546
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
 
LVL 7

Expert Comment

by:Swatantra Bhargava
ID: 33585588
specify your SMTP server in below line:

$mail->Host = "gmail.com";
0
 

Author Comment

by:VapiSoft
ID: 33585635
Where do I do it?
I changed it (to vapisoft.com which is my server) and it didn't help (same problems).
0
 
LVL 7

Accepted Solution

by:
Swatantra Bhargava earned 500 total points
ID: 33585761
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
 

Author Comment

by:VapiSoft
ID: 33585839
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
 

Author Comment

by:VapiSoft
ID: 33586217
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
 

Author Closing Comment

by:VapiSoft
ID: 33586488
Thank you.
Now it works.
So the only way it works is via SMTP and via the php mail.
0
 
LVL 3

Expert Comment

by:JtR
ID: 33589150
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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now