E-mail sending by PHP does not show accents correctly

0

Situation

I have an email newsletter system, emails are sent to clients every day, but I noticed that when using accents such as ´~'^ , the words unconfigured like this: ÓAÃÃà below.

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

Important Information

The subject also has the same problem, ie the entire email, from subject to message leave with unconfigured accents.

The code php looks 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 somehow to put UTF-8 encoding or any other that does not have issues with accentuations in email.

@Edit 04/14/16 08:23 AM

One thing I noticed 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 and the message . The code for config.php looks 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://pt.stackoverflow.com";
$url_sist        = "http://pt.stackoverflow.com";
?>

And the result of the email is like this:

  

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

    
asked by anonymous 12.04.2016 / 14:36

4 answers

0

The problem was solved simply by adding utf8_decode() to the $_POST variables, ie it had no connection with the Content-Type or body of the message nor with the encoding of the file.

Currently with the solution to the problem, code looks 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

Thank you to anyone who tried to help and I hope it helps others who are in trouble too.

    
14.04.2016 / 14:37
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 it an UP here.

    
12.04.2016 / 15:14
2

How do you have these lines:

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

It's a matter of ensuring that the $msg_tipo variable has the correct charset, either coming from POST, or putting it in the send 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, you need to set it accordingly. The important thing is to add ; charset="UTF-8" to the end.

(not to be confused with Content-Type of HTTP header, we are talking about email header / MIME 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:

  

link

    
12.04.2016 / 15:21
0

I use PHPMailer and my emails are sent with the correct coding, 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">
    
13.04.2016 / 22:30