In PHPmailer, when attempting to send HTML-formatted emails, users may encounter an unexpected problem: the actual HTML code is displayed in the email body instead of the intended content. To resolve this issue effectively, a specific order in method calls is crucial.
The proper sequencing involves setting the Body property of the PHPmailer object ($mail->Body) before calling the isHTML() method. This subtle adjustment ensures that PHPmailer recognizes the content as HTML and processes it accordingly.
Below is a corrected code snippet that addresses this issue:
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = $Host;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $Username;
$mail->Password = $Password;
$mail->From = $From;
$mail->FromName = $FromName;
$mail->AddAddress($To, $ToName);
$mail->WordWrap = 50; // set word wrap
$mail->Priority = 1;
$mail->Subject = $Subject;
$mail->Body = $Body;
$mail->IsHTML(true); // Call IsHTML() after $mail->Body has been set.
By following this proper sequence, PHPmailer can accurately parse and render HTML content, delivering the intended email format.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3