Sending email by PHP does not show accents correctly

Asked

Viewed 2,812 times

0

Situation

I have an email newsletter system, on it is sent emails to customers every day, but I realized that when using accents like ´~'^, the words disfigure getting like this: ÓAÃÃà forward.

Well, I’ve tried to include <meta charset="UTF-8"> and other encodings but nothing changes but saving with UTF-8 encoding.

Información Importante

The subject also has the same problem, IE, the entire email, since subject to message come out with disfigured accents.

The code php is like this:

<?php              
$msg_para    = $_POST["msg_para"];
$msg_assunto = $_POST["msg_assunto"];
$msg_tipo    = $_POST["msg_tipo"];
$mensagem    = $_POST["mensagem"];
if($msg_para == "todos"){
$sql = mysql_query("SELECT * FROM list-emails WHERE ativo = 'online'");
$total = mysql_num_rows($sql);
$mailok = 0;
$falha  = 0;
while($lista = mysql_fetch_array($sql)){
$email = $lista["email"];
$cabecalho  = "From: $a_nome <$a_email>";
$cabecalho .= "\nReply-To: $a_nome <$a_email>";
$cabecalho .= "\nContent-Type: $msg_tipo";
if(@mail($email,$msg_assunto,$mensagem,$cabecalho)){
$mailok = $mailok + 1;
$msg = "<font color=green>SUCESSO!</font>";
}
else{
$falha = $falha + 1;
$msg = "<font color=red>FALHA!</font>";
}
?>
<font face="Arial" size="2">Enviando para <b><?=$email?></b>...
<b><?=$msg?></b></font><br>
<?php } ?>
<script>alert("<?=$total?> e-mails deveriam ser enviados...\n<?=$mailok?> 
foram mandados corretamente,\n<?=$falha?> falharam!\n") </script>
<?php
}
else{
$cabecalho  = "From: $a_nome <$a_email>";
$cabecalho .= "\nReply-To: $a_nome <$a_email>";
$cabecalho .= "\nContent-Type: $msg_tipo";
if(@mail($msg_para,$msg_assunto,$mensagem,$cabecalho)){
$msg = "<font color=green>SUCESSO!</font>";
}
else{
$msg = "<font color=red>FALHA!</font>";
}
?>
<font face="Arial" size="2">Enviando para <b><?=$msg_para?></b>...
<b><?=$msg?></b></font><br><?php
}
}
else{
echo "<script>location.href='login.php'</script>";
}
?>

The full code can be checked here

Briefly I need some way to put encoding UTF-8 or any other that does not have problems with accents in the email.

@Edit 14/04/16 08:23 AM

One thing I realized is that the name of the email set in config.php comes out with normal accents, the only fields that have this problem are the subject matter and that of message. The code of config.php is like this:

<?php
$host    = "4322-8922";
$usuario = "5";
$senha   = "4";
$banco   = "321";
mysql_connect($host,$usuario,$senha);
mysql_select_db($banco);
$a_nome          = "Téstê dê Ácëñtõs StáckÔvèrFlôw";
$a_email         = "[email protected]"; 
$formato_msg     = "Text/HTML"; 
$confirm_assunto = "Confirmação de Email $a_nome"; //esse assunto é especificamente para a parte de newsletter
$titulo          = "Máîs Úm Tèstê"; 
$url             = "http://answall.com";
$url_sist        = "http://answall.com";
?>

And the result of the e-mail is like this:

problema de acentos com email

Using text converters is out of the question since sending email email is done directly by the system, this would cause a slowdown in the process.

  • 1

    Try this: utf8_encode($msg);

  • @Edcesar, I tried to put this in $msg_subject and $message but nothing has changed, have any idea what it might be ?

4 answers

2

You need to configure the PHP header.

Put this at the top of the page:

header('Content-type: text/html; charset=utf-8');

If it works give a UP here.

  • Unfortunately remains the same thing, I believe I will have to set the charset for each of the fields, is that it? If yes, how can I do this?

  • Dude, there’s one other thing you can do... Set your editor to utf-8 encoding.. It helped me once...

  • 1

    had already done that friend, problem has been solved

2

How do you have these lines:

$msg_tipo    = $_POST["msg_tipo"];
$cabecalho .= "\nContent-Type: $msg_tipo";

is a matter of ensuring that the variable $msg_tipo has the correct charset, either coming from the POST, or putting in the sending code.

The correct format is this:

              Content-Type: text/html; charset="UTF-8"
aqui você adapta pro seu caso  ---^      ^---- fundamental além do tipo certo, o charset

Note that text/html was just an example, need to set according to the content. The important thing is to add the ; charset="UTF-8" at the end.

(not to be confused with the Content-Type HTTP header, we’re talking about the email/MIME header type)

In addition, it is important that the page where the form is also using the correct charset. ISO-8859-1 and UTF-8 are the most common and suitable for shipping.


Here are some more considerations (in English):

https://en.wikipedia.org/wiki/Unicode_and_email

  • I did as you recommended but now it seems the following mistake: parse error: syntax error, unexpected '=' in /xxx/xx/public_html/xx/mod/envia_mail.php on line 265

  • on line 265 is: $cabecalho .= "\nContent-Type: $msg_tipo"; charset="UTF-8";

  • Your quotation marks are wrong. You need to correct the syntax. Either you "escape" them, or you use simple concatenation and quotation marks. $cabecalho .= "\nContent-Type: $msg_tipo"; charset=\"UTF-8\"";

  • or else $cabecalho .= "\n".'Content-Type: '.$msg_tipo.'; charset="UTF-8"';

  • remember to test with ISO-8859-1 if the problem persists.

  • Unfortunately the 2 forms, including alternating between ISO-8859-1 and UTF-8, did not work

  • then there is something else wrong with the code. Did you check the source of the email to see if the output was the same as intended? It may be some detail wrong in the general header mount as well. Looking at the source generated is important. Incidentally, an extra detail, breaking email header is with CRLF. As for the "subject" field, the link I posted has a description about the encodings. It is not enough to just hit the charset, "subject" header has a special encoding. (but the most important is to hit the body of the email first)

  • It’s the way to study it better and while I can’t manage to go using special character converter, thank you for helping @Bacco, thank you very much

  • @Victorgomes take a good look at the email source, it’s probably some silly thing. Instead of sending the test, print the data on the screen to check. The thing in theory is very simple, just find the cause of the problem. Show on the screen the data before and after sending. I suggest changing the " n" by " r n" as well, which is the default for email. If you hit the body of the email, then you can proceed to the solution for the header.

Show 4 more comments

1


The problem was solved simply adding utf8_decode() in the variables $_POST, that is, had no connection with the Content-Type or message body and not with file encoding.

Currently with the solution of the problem, the code is like this:

<?php              
$msg_para    = $_POST["msg_para"];
$msg_assunto = utf8_decode($_POST["msg_assunto"]);
$msg_tipo    = $_POST["msg_tipo"];
$mensagem    = utf8_decode($_POST["mensagem"]);
if($msg_para == "todos"){
$sql = mysql_query("SELECT * FROM n_emails WHERE ativo = 's'");
$total = mysql_num_rows($sql);
$mailok = 0;
$falha  = 0;
while($lista = mysql_fetch_array($sql)){
$email = $lista["email"];
$cabecalho  = "From: $a_nome <$a_email>";
$cabecalho .= "\nReply-To: $a_nome <$a_email>";
$cabecalho .= "\nContent-Type: $msg_tipo";
 if(@mail($email,$msg_assunto,$mensagem,$cabecalho)){
 $mailok = $mailok + 1;
 $msg = "<font color=green>SUCESSO!</font>";
 }
 else{
 $falha = $falha + 1;
 $msg = "<font color=red>FALHA!</font>";
 }
 ?>
 <font face="Arial" size="2">Enviando para <b><?=$email?></b>...
 <b><?=$msg?></b></font><br>
 <?php } ?>
 <script>alert("<?=$total?> e-mails deveriam ser enviados...\n<?=$mailok?> foram mandados corretamente,\n<?=$falha?> falharam!\n")</script>
 <?php

I thank those who tried to help and hope to help other people who are in trouble too.

  • I didn’t understand the down vote, it’s wrong the way I wrote the answer or something included?

  • For me, it worked perfectly.

0

I make use of Phpmailer and my emails are sent with the correct encoding, for this I added in my email script, in the first line I added the code below:

<meta http-equiv="content-type" content="text/html; charset=UTF-8">
  • Guy unfortunately didn’t work, I tried to add both in the script, as well as in the body of the email

  • What is the encoding of your files? To test, in the linux terminal do as in the example below (replacing "mail.php" with your file responsible for sending the email): # file --mime mail.php in my case, the answer was: mail.php: text/html; charset=utf-8

  • managed to resolve already friend, I posted below as I did, thanks for the help @asfelix

Browser other questions tagged

You are not signed in. Login or sign up in order to post.