Rare characters in emails sent with PHP

1

I am developing a website that should send e-mail with different types of notifications to users. For sending them I am using the class PHPMailer. The e-mail is sent from the server, with that I have no problem, but when I open the mail in the corresponding inbox, it comes out with many strange characters due to accents and so on.

I get the text like this:

"Remember that, for safety, you should change this password for a new one when you access the platform in your account settings (you will find it in the navigation menu)."

I know it must be something of the coding that I'm not doing correctly, I've tried the htmlentities function, applying it to the body of the e-mail, but it has not worked for me.

Any ideas? Thank you very much in advance.

    
asked by IreneA 27.11.2017 в 21:14
source

3 answers

2

It's strange that you have problems with the character set in PHPMailer. Recent versions of the library come with UTF-8 by default.

To solve your problem, check some things:

  • That you are not establishing a strange character set in your message.
  • If you use an old version of PHPMailer then, you can set the appropriate CharSet.
  • For example:

    $mail->isHTML(true); // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
    /*Si tienes PHPMailer 6, y esto tiene algo extraño, 
     *que no es UTF-8, puedes comentar esta línea
     *En cambio, si tu PHPMailer es antiguo, agrega exactamente esta línea*/
    $mail->CharSet = 'UTF-8'; 
    
    ... resto del código
    

    Note: PHPMailer is currently in your v. 6.0.1 stable, if you have an older version, it is convenient that you update.

        
    answered by 27.11.2017 в 22:16
    1

    It is because you do not recognize (ñ, á, é, í, ó, ú) put the following in your document:

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    

    If it does not work for you, try changing the accents and ñ with your codes something like this:

    "Recuerda que, por seguridad, deber&#225;s cambiar esta contrase&#241;a por una nueva cuando accedas a la plataforma en la configuraci&#243;n de tu cuenta (la encontrar&#225;s en el men&#250; de navegaci&#243;n)."
    
        
    answered by 27.11.2017 в 21:27
    0

    After a lot of fighting with the code, I managed to fix the coding problem within the body of the email, but in the subject I get a rare encoding, like this: "=? UTF8? Q? La_F = C3 = A1brica_ -_Nueva_contrase = C3 = B1a?="

    The code I am using is the following:

        $mail = new PHPMailer(); 
        $body = $cuerpo;
    
        $mail->setFrom('[email protected]', 'Prueba');
        $mail->addReplyTo('[email protected]', 'Prueba');
    
        $address = $direccionEmail;
    
        $mail->addAddress($address, $nombreUsuario);
    
        $mail->Subject = "Nueva contraseña";
    
        $mail->msgHTML($body);
    
        $mail->isHTML(true);
        $mail->CharSet = "UTF­8";
    
        $mail->send();
    

    What am I doing wrong? Thank you very much for your help!

        
    answered by 23.12.2017 в 18:48