Cant send email with correct characters with PHPMailer
Asked Answered
N

4

17

I'm trying to send a e-mail with the PHPmailer class, but the html i send, is empty, or the characters are unconfigured, and without accents.

<?php
header("Content-Type: text/html; charset=ISO-8859-1", true);
require_once('class.phpmailer.php');
include "config.php";

$nome = trim($_POST['nome']);
$email  = trim($_POST['Imail']);
$usuario = trim($_POST['usuario']);
$senha = trim($_POST['senha']);
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch

$mail->IsSMTP(); // telling the class to use SMTP

try {
  $mail->AddAddress($email, $nome);
  $mail->SetFrom('[email protected]', 'Conectfarma');
  $mail->AddReplyTo('[email protected]', 'Conectarma');
  $subject = 'Guia Rápido de Interações Medicamentosas';
  $sendsubject= "=?utf-8?b?".base64_encode($subject)."?=";
  $mail->Subject = $sendsubject;
 $mensagem  = "<!DOCTYPE html>
<html>
<body>
Bem vindo ao Guia Rápido de Interações Medicamentosas em Neurologia e Psiquiatria
Seu Login e Senha para acesso ao aplicativo são:\n
Login:"  .$nome. "\n, Senha : " .$senha.
"\nAtenciosamente,
Conectfarma Publicações Científicas
</body>
</html>";

  $mail->Body = $mensagem;
  //$mail->CreateBody($mensagem);
  $mail->IsHTML(true);
  $mail->Send();
  //$mail->CharSet="UTF-8";
  echo "<!DOCTYPE html>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<title>Confirmação</title>
</head>
<body>
Não vai maçã.
</body>
</html>
";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}
        }
    }
}

?>

I jumped the SMTP configuration because its working properly.

Nealah answered 19/12, 2012 at 16:21 Comment(4)
Ensure that your code is UTF8, uncomment $mail->CharSet="UTF-8";. Don't write code in your native language.Kansas
@TomaszKowalczyk just commented that, but still, the email that im receiving is "Não vai maçã." and sorry about the native language.Nealah
Are you really, really sure, that your files are UTF-8 encoded?Kansas
Uncommenting that line is not enough - You also have to move it BEFORE the $mail->Send(); line...idealy right after $mail = new PHPMailer(true);...Shawanda
S
47

Double check Your PHP code is also in UTF-8 encoding.

Uncomment the line //$mail->CharSet="UTF-8"; and move it idealy right after the $mail = new PHPMailer(true);, so the code would look like:

// ...
$mail = new PHPMailer(true);
$mail->CharSet = "UTF-8";
// ...

In Your code it is called after the $mail->Send(); thus the charset setting did not take in count...

Shawanda answered 19/12, 2012 at 16:53 Comment(0)
C
2

Yes, right after the "new PHPMailer(true);". I had the same problem with:

$mail = new PHPMailer(true);
try {
    $mail->setLanguage('fr', 'inc'.DIRECTORY_SEPARATOR.'PHPMailer'…);
    $mail->CharSet = 'UTF-8';

and changing to:

$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8';
try {
    $mail->setLanguage('fr', 'inc'.DIRECTORY_SEPARATOR.'PHPMailer'…);

solved the accents problem.

Charley answered 10/11, 2018 at 9:38 Comment(0)
K
1

This solution might also be of help:

I added:

$mail->CharSet = 'UTF-8';

Then to the strings which I had accented letters in I used htmlentities with the exception of html tags. So something like this:

'<p><?php echo htmlentities("För servicerapport") ?></p>'
Khanna answered 29/10, 2022 at 7:56 Comment(0)
C
0

This solution may help somebody, as it helped me.

<?php $this->phpmailer->Subject = "=?UTF-8?B?" . base64_encode($subject) . "?=\r\n"; ?>
Chronopher answered 5/4, 2022 at 4:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.