PHPMailer在消息中显示HTML实体

时间:2018-09-30 12:24:27

标签: php phpmailer

我在邮件中显示PHPMailer和HTML实体时遇到问题。

示例:

嗨,我'把它'是大卫在这里,等等。

这是我用于PHPMailer的设置:

$name = trim(filter_input(INPUT_POST, "name", FILTER_SANITIZE_STRING));
$phone = trim(filter_input(INPUT_POST, "phone", FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST, "email", FILTER_SANITIZE_EMAIL));
$message = trim(filter_input(INPUT_POST, "message", FILTER_SANITIZE_SPECIAL_CHARS));    

$emailBody = "";
$emailBody .= "Name: " . $name . "\n";
$emailBody .= "Phone: " . $phone . "\n";
$emailBody .= "Email: " . $email . "\n";
$emailBody .= "Message: " . $message . "\n";

$mail = new PHPMailer;

$mail->SMTPOptions = array(
        'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

$mailer->Host = 'tls://smtp.gmail.com';
$mailer->SMTPAuth = true;
$mailer->SMTPSecure = 'tls';
$mailer->Port = 587;
$mail->Username = "*********@gmail.com";
$mail->Password = "***********";

$mail->CharSet = 'utf-8';
$mail->Encoding = 'base64';
$mail->setFrom('some@example.com');
$mail->addReplyTo($email, $name);
$mail->addAddress('someone@example.com', 'Some One');
$mail->Subject = 'Site Message From: ' . $name;
$mail->Body = $emailBody;

if(!$mail->send()) {
    $msg .= "Mailer Error: " . $mail->ErrorInfo;
    exit;
}

对此的任何帮助将不胜感激:)

2 个答案:

答案 0 :(得分:0)

只需在字符集声明上方添加$mail->IsHTML(true);即可解决此问题。然后,我需要更改表单变量以改善通过电子邮件接收时的格式:

$emailBody .= "Name: " . $name . "<br />";
$emailBody .= "Phone: " . $phone . "<br />";
$emailBody .= "Email: " . $email . "<br />";
$emailBody .= "Message:<br />" . $message;

感谢评论者抽出宝贵的时间来帮助:)

答案 1 :(得分:0)

在尝试发送与号(&)时,我遇到了类似的问题;在电子邮件中显示为&amp;

在构造电子邮件时,我只需在变量上使用html_entity_decode()即可解决此问题。

一个例子:

$mail->Subject = 'A message from ' . html_entity_decode($name);