utf-8 phpmailler error

Asked

Viewed 218 times

0

I have the following email class:

<?php

  class EmailEnviarDao {

    public function __construct() {}

    public function enviaEmail($email)  {

      $enviaFormularioParaNome = $email->getNomeAlvo();
      $enviaFormularioParaEmail = $email->getEmailAlvo();

      $caixaPostalServidorNome = 'Hotplate Prensas';
      $caixaPostalServidorEmail = '[email protected]';
      $caixaPostalServidorSenha = '1234567890';

      $remetenteNome  = $email->getNomeRemete();
      $remetenteEmail = $email->getEmailRemete();
      $mensagem = $email->getDescricao();
      $assunto =  $email->getAssunto();
      $qual =  $email->getQual();

      $mensagemConcatenada = '<!doctype html>
                              <html>
                              <head>
                                <meta charset="UTF-8">
                                <?php header("Content-type: text/html; charset=utf-8");?>
                                <title>'.$caixaPostalServidorNome.'</title>
                              </head>
                              <body>
                                Formulário gerado via website<br/><br/><br/>
                                Nome: '.$remetenteNome.'<br/>
                                E-mail: '.$remetenteEmail.'<br/>
                                Assunto: '.$qual=="" ? $assunto : $qual.'<br/>
                                -------------------------------<br/><br/>
                                Mensagem: '.$mensagem.' 
                              </body>';

      $mail = new PHPMailer();

      $mail->IsSMTP();
      $mail->SMTPAuth  = true;
      $mail->Charset   = 'UTF-8';
      $mail->Host  = 'smtp.'.substr(strstr($caixaPostalServidorEmail, '@'), 1);
      $mail->Port  = '587';
      $mail->Username  = $caixaPostalServidorEmail;
      $mail->Password  = $caixaPostalServidorSenha;
      $mail->From  = $remetenteEmail;
      $mail->FromName  = $remetenteNome;
      $mail->IsHTML(true);
      $mail->Subject  = $assunto == "Outro Assunto" ? $qual : $assunto;
      $mail->Body  = $mensagemConcatenada; 
      $mail->AddAddress($enviaFormularioParaEmail,utf8_decode($enviaFormularioParaNome));

      if($mail->Send()){

          return array("success"=>1,"errors"=>"0K");

      } else {

          return array("success"=>0,"errors"=>"Não conseguimos enviar o e-mail");

      }    
    }   

  }

?>

Everything works well, except the accents.

I have tried several alternatives published here in the forum but none worked.

The email is fired. But the accents, no!

I tried to put UTF-8 on htmls and

  • utf8_decode($mensagemConcatenada);

  • thanks. it worked out!

  • Blz, I’ll put it in answer

1 answer

1

Simply use utf8_decode() in the desired variable;

I believe in your case utf8_decode($mensagemConcatenada);

  • blz. but now a doubt has arisen: when we do: $mail->Charset = 'UTF-8'; this should not be able to solve the problem?

  • @Carlosrocha, you tried it $mail->Charset = 'UTF-8'; ?

  • in my code already had this and it didn’t work. see in question

  • 1

    @Carlosrocha believes that the right thing should be $mail = new PHPMailer(); $mail->CharSet = "UTF-8"; You have to put it after the instantiation of the object.

  • The difference to my code is in the uppercase letter S (de Charset) or double quotes?]

  • @Carlosrocha, you wanted a solution or do tests with possibilities?

  • No friend. The doubt is that in my code there was already that the call to the charset. However written ->charset and not Charset equal to yours. . Another thing is that in yours, the UTF-8 is double-quote and mine with single-quote

  • @Carlosrocha the correct one is Charset, the quotes don’t matter! With Charset worked? Have a look at this link https://stackoverflow.com/questions/2491475/phpmailer-character-encoding-issues

Show 3 more comments

Browser other questions tagged

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