php 无法使用 PHPMailer 发送带有正确字符的电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13956663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:21:07  来源:igfitidea点击:

Cant send email with correct characters with PHPMailer

phphtmlemailphpmailer

提问by darkman

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.

我正在尝试使用 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('[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.

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

回答by shadyyx

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

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

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->CharSet="UTF-8";并将其理想地移动到 之后$mail = new PHPMailer(true);,因此代码如下所示:

// ...
$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...

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

回答by Michel D

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

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

$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.

解决了口音问题。