• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

[PHP] Text to Html entitities

HugoLeite

New Member
Hi there, i've created a script to send mail, but i'm getting a problem when the message contains " á à é è " and so on, what happens is that the email gets like
Loja : ááá ééé
Nome do Cliente : ááá ééé
Texto mensagem : ççç ººº ªªª âââ õõõ

And the original mesage was something like
Store : ááá ééé
Client name : ááá ééé
Message : ççç ººº ªªª ããã õõõ
It was just a test message.

I've tried using some PHP functions such as htmlentities() and get_html_translation_table(), but none worked. Is there anyone who has happened something familiar, or know how to solve this.


Thank you
 
This is not related to html entities, but to html locale/charset. You can put charset meta tag for current language/locale being used in html, for example with en-us charset:
Code:
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

Charset meta tag mostly work with graphical browser and mime-compatible email client software. For plain text in email, you should put charset code into email header code "Content-Type". Not all email client software support plain text email using this way.
 
Hi there, i've been trying various ways of fixing this problem. I think i've tried the solution you suggested, but it continued to give the same problem. Here is the headers i'm sending in the email.

PHP:
$headers  = "MIME-Version: 1.0\r\n" ;
 $headers .= "Content-type: text/html; charset=iso-8859-1\r\n" ;
 $headers .= "To: $paranome <$paraemail>\r\n" ;
 $headers .= "From: $denome <$deemail>\r\n" ;
 $headers .= "Reply-To: $deemail\r\n" ;
 $headers .= "X-Mailer: PHP/" . phpversion() ;

$paranome is the name of the recipient, $denome is the senders name, and $paraemail, and $deemail are the emails of the recipient and sender.

The email is text with some parts of html, just line brakes and bold tags.
Where you refering to sending the headers in the email something like that ?

I think it must be the server, because to do "á é í ó ú" in normal html pages i need to do write the proper html entitie like &aacute; &ccedil; and so on.


Thanks
 
Hi,

CareBear, I'm trying to use special characters in the email body, i posted the headers so you could all see how i'm sending them.

PHP:
$dominio_referer=parse_url($_SERVER['HTTP_REFERER']) ;

   if(!in_array($dominio_referer['host'],$referers)){
	   $sucesso = 0 ;
		echo ('<br /><center><b><font face="Verdana" size="2" color="red">N&atilde;o &eacute; possivel enviar o email a partir do endere&ccedil;o especificado.</font><br /><br /><font face="Verdana" size="1"><a href="javascript: history.go(-1) ;">« Retroceder</a></font></b></center>');
                          exit ;
	}else{
	
		/* Verifies if there is any config field in the form */
		$denome = verifica_valor_post( $_POST['cfg_denome'] , $default_from_nome , 0 ) ;
		$deemail = verifica_valor_post( $_POST['cfg_deemail'] , $default_from_email , 1 ) ;
		$paranome = verifica_valor_post( $_POST['cfg_paranome'] , $default_to_nome , 0 ) ;
		$paraemail = verifica_valor_post( $_POST['cfg_paraemail'] , $default_to_email , 1 ) ;
		$assunto = verifica_valor_post( $_POST['cfg_assunto'] , $default_assunto , 0 ) ;
		$sendto = verifica_valor_post( $_POST['cfg_sendto'] , $default_send_to , 0 ) ;

		$headers  = "MIME-Version: 1.0\r\n" ;
		$headers .= "To: $paranome <$paraemail>\r\n" ;
		$headers .= "From: $denome <$deemail>\r\n" ;
		$headers .= "Reply-To: $deemail\r\n" ;
		$headers .= "X-Mailer: PHP/" . phpversion() . "\r\n" ;
		$headers .= "Content-type: text/html; charset=iso-8859-1" ;
		
		/* Fetches the values in the fields created by the user */
		$getarray = array_keys($_POST) ;
		$len = count($getarray) ;
		for($j = 0; $j < $len; $j++) {
			if(substr($getarray[$j], 0, 4) != 'cfg_' && substr($getarray[$j], 0, 3) != 'hd_' ){
				$mensagem .= '<b>' . str_replace('_',' ',$getarray[$j]) . ' : </b>' . $_POST[$getarray[$j]] . '<br />' ; 
			}
		}
		
		/* Sends the mail */
		$sucesso = mail( $paraemail , $assunto , nl2br($mensagem) , $headers ) ;
		
	} // Else End ( REFERERS )
	
	if($sucesso == 1){
		header("Location: $sendto ") ;
		exit;
	}else{
		echo '<font face="Verdana" size="2" color="#0066cc"><b>N&atilde;o foi possivel enviar o email.</b></font>' ;
	}
Above this part of the code there is a function "verifica_valor_post" used to verify if the $_POST is empty and it has to use the default values that are specified in the beginning of the file like:

PHP:
$default_from_nome = "My Name" ;
 $default_from_email = "MyEmail@domain.com" ;
 $default_to_nome = "Your name" ;
 $defalt_to_email = "YourEmail@domain.com" ;
 /* More */
The script is going to be used to submit forms created by the user, with field names that he wants, the forms are something like contact forms and requests and that. Thats why there are $default variables, so that the user doesn't has to put all the "cfg_" hidden fields in the form.

Where it "Fetches the values in the fields created by the user " its a for that cycles thru the $_POST array, and checks if the field name wasn't started with "cfg_" or "hd_" if it doesn't it adds it to the body of the email.

I'm making some changes, but is only to the way the email is displayed, i'm adding a table, so this won't change the logic of the code.


I didn't posted the configuration because it has a lot of comments and they are in portuguese.
 
You are trying to use special characters in your email headers, which means they should be properly MIME encoded.

PHP:
<?php

$mailTo = "someone@somewhere";
$mailSubject = mb_encode_mimeheader("Tést subject", "ISO-8859-1", "Q");
$mailHeaders  = "From: \"".mb_encode_mimeheader("ááá ééé", "ISO-8859-1", "Q")."\" <from@test.com>";
$mailHeaders .= "To: \"".mb_encode_mimeheader("ááá ééé", "ISO-8859-1", "Q")."\" <to@test.com>";
$mailBody = "Mail body";

echo mail($mailTo, $mailSubject, $mailBody, $mailHeaders);
?>

Note that this will only work if your host has the PHP multibyte string (mbstring) extension installed. Be sure to check with a phpinfo();.
If they don't have it but they do have iconv you can use those functions, otherwise you're just out of luck.
 
Hi there, i've tried you script, only adding the emails, and it didn't worked, but after i added the MIME-Version and Content-type it did.
It got something like this:
PHP:
$mailTo = "mail@hotmail.com"; 
 $mailSubject = mb_encode_mimeheader("Tést subject", "ISO-8859-1", "Q"); 
 $mailHeaders  = "MIME-Version: 1.0\r\n" ; // Added this
 $mailHeaders .= "Content-type: text/html; charset=iso-8859-1\r\n" ; // And this
 $mailHeaders .= "From: \"".mb_encode_mimeheader("Vitor Leite", "ISO-8859-1", "Q")."\" <mail@mail.com>\r\n"; 
 $mailHeaders .= "To: \"".mb_encode_mimeheader("Hugo", "ISO-8859-1", "Q")."\" <mail@mail.com>\r\n"; 
 $mailBody = "ééé ííí"; 

echo mail($mailTo, $mailSubject, $mailBody, $mailHeaders);
And the email got out fine, all as it was supposed to be
the email was "ééé ííí" equal to $mailBody.

The mbstring is enabled, i've checked it.

The thing is, i've made those changes to the script, and it didn't worked.

I guess i've made something wrong. but the email is sent anyway, it just shows as it did before ...

Here's the new headers:
PHP:
$headers  = "MIME-Version: 1.0\r\n" ;
 $headers .= "Content-type: text/html; charset=iso-8859-1\r\n" ;		
 $headers .= "To: \"".mb_encode_mimeheader( $paranome, "ISO-8859-1", "Q")."\" <$paraemail>\r\n" ;
 $headers .= "From: \"".mb_encode_mimeheader( $denome, "ISO-8859-1", "Q")."\" <$deemail>\r\n" ;
 
You are still mixing two things up: using special characters in your mail headers and in your mail body are very different. :confused5
Content-Type and MIME-Version apply only to the body, they have nothing to do with how the headers are encoded.

I don't have the time right now to get into a whole explanation; the specifications on email are there for you to read if you want to know how and why email should be encoded a certain way.

However, one quick dirty fix that should work for you:
PHP:
<?php

$mailTo = "someone@somewhere";
$mailSubject = mb_encode_mimeheader("Tést subject", "ISO-8859-1", "Q")."\r\n";
$mailHeaders  = "From: \"".mb_encode_mimeheader("ááá ééé", "ISO-8859-1", "Q")."\" <from@test.com>\r\n";
$mailHeaders .= "To: \"".mb_encode_mimeheader("ááá ééé", "ISO-8859-1", "Q")."\" <to@test.com>\r\n";
$mailHeaders .= "MIME-Version: 1.0\r\n";
$mailHeaders .= "Content-Type: text/html;\r\n\tcharset=\"ISO-8859-1\"\r\n";
$mailHeaders .= "Content-Transfer-Encoding: base64\r\n";

$mailBody = "Mail body with some spécial çharacter§.";

mail($mailTo, $mailSubject, chunk_split(base64_encode($mailBody)), $mailHeaders);
?>
will send the email as:
Code:
Subject: =?ISO-8859-1?Q?T=E9st=20subject?=  
From: "=?ISO-8859-1?Q?=E1=E1=E1=20=E9=E9=E9?=" <from@test.com>
To: "=?ISO-8859-1?Q?=E1=E1=E1=20=E9=E9=E9?=" <to@test.com>
MIME-Version: 1.0
Content-Type: text/html;
	charset="ISO-8859-1"
Content-Transfer-Encoding: base64

TWFpbCBib2R5IHdpdGggc29tZSBzcOljaWFsIOdoYXJhY3Rlcqcu
If you're not sending your emails as HTML, text/html should be text/plain.

Also, the "nice" thing to do when sending HTML mail is to include a plain text copy stripped of all tags along with it for those that don't have HTML mail turned on.
If you don't want to get into multipart email messages I'd suggest sticking to plain text. HTML mails don't look nice to those people who choose not to view them.
 
Hmm, guess i really was mixing it. :p

Anyway I've found out that the source of the problem was the form submission, the chars come encoded in the post data, i've tried with a perl script i downloaded and the email appeared as with the one i made, so, the real problem is the encoded chars in the post.

I've tried some things, but none seems to work out. i've also changed the charset in both the form and the script to UTF-8, but i can't figure out how am i supposed to decode the chars properly.

I also tried another thing, if i hardcode the mail body, it gets out fine, and if i echo it to the page that has the script, i shows all wrong, some chinese characters, but if i submit it from the form, the text appears right on the browser, and the source code is the same as if i hardcode the text, it's still "ééé ááá".
 
Back
Top