Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPMailer Displays HTML Entities In Message

Tags:

php

phpmailer

I'm having problems with PHPMailer and HTML entities displaying in a message.

Example:

Hi there, I'd it's David here, blah, blah.

Here's the setup I'm using for 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('[email protected]');
$mail->addReplyTo($email, $name);
$mail->addAddress('[email protected]', 'Some One');
$mail->Subject = 'Site Message From: ' . $name;
$mail->Body = $emailBody;

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

Any help with this would be greatly appreciated :)

like image 674
frontendstu Avatar asked Dec 28 '25 22:12

frontendstu


1 Answers

By simply adding $mail->IsHTML(true); above the charset declaration I managed to fix the issue. I then needed alter the form variables to improve formatting when received through email:

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

Thanks to the commenters for taking the time to help :)

like image 170
frontendstu Avatar answered Dec 30 '25 12:12

frontendstu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!