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

标签 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 配置,因为它工作正常。

最佳答案

仔细检查您的 PHP 代码是否也是 UTF-8 编码。

取消注释行 //$mail->CharSet="UTF-8"; 并将其移动到 $mail = new PHPMailer(true); 之后,所以代码看起来像:

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

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

关于php - 无法使用 PHPMailer 发送包含正确字符的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13956663/

相关文章:

html - 如何在单行中显示来自两种不同形式的输入按钮

iOS 7 : Blank Screen when presenting MFMailComposeViewController on navigationController

c# - 使用 MailMessage 的多部分电子邮件

php exec() 不适用于 PredictionIO 部署

php - 对多列求和,但在 mysql 查询中有一个列

php - urlencode vs rawurlencode?

php - 将 UTF-8 编码的字符串插入 UTF-8 编码的 mysql 表失败​​,并显示 "Incorrect string value"

python - 使用 Python 进行 HTML 编码

javascript - 在哪里可以找到窗口 API 列表?

linux - 在 Linux 上通过 Qt 发送邮件通知