无法使用PHPMailer发送包含正确字符的电子邮件

时间:2012-12-19 16:21:35

标签: php html email phpmailer

我正在尝试使用PHPmailer类发送电子邮件,但我发送的html是空的,或者字符是未配置的,没有重音符号。

<?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('editora@conectfarma.net', 'Conectfarma');
  $mail->AddReplyTo('editora@conectfarma.net', '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!
}
        }
    }
}

?>

我跳过了SMTP配置,因为它工作正常。

2 个答案:

答案 0 :(得分:37)

仔细检查您的PHP代码也采用UTF-8编码。

取消对//$mail->CharSet="UTF-8";行的注释,然后在$mail = new PHPMailer(true);之后移动它,因此代码看起来像:

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

在您的代码中,它在$mail->Send();之后被调用,因此字符集设置没有计入...

答案 1 :(得分:0)

是,就在“ new PHPMailer(true);”之后。 我也有同样的问题:

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

并更改为:

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

解决了口音问题。