【问题标题】:Cant send email with correct characters with PHPMailer无法使用 PHPMailer 发送带有正确字符的电子邮件
【发布时间】:2012-12-07 01:23:09
【问题描述】:

我正在尝试使用 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 配置,因为它工作正常。

【问题讨论】:

  • 确保您的代码是 UTF8,取消注释 $mail-&gt;CharSet="UTF-8";。不要用您的母语编写代码。
  • @TomaszKowalczyk 刚刚评论说,但我收到的电子邮件仍然是“Não vai maçã”。对母语感到抱歉。
  • 您确定您的文件是 UTF-8 编码的吗?
  • 取消注释该行是不够的 - 您还必须在 $mail-&gt;Send(); 行之前移动它...最好在 $mail = new PHPMailer(true); 之后...

标签: php html email phpmailer


【解决方案1】:

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

取消注释//$mail-&gt;CharSet="UTF-8"; 并将其移动到$mail = new PHPMailer(true); 之后,这样代码如下所示:

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

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

【讨论】:

    【解决方案2】:

    是的,就在“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'…);
    

    解决了重音问题。

    【讨论】:

      猜你喜欢
      • 2015-09-06
      • 2020-11-25
      • 2019-01-19
      • 2023-03-20
      • 2017-07-19
      • 2017-10-05
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      相关资源
      最近更新 更多