Home >Backend Development >PHP Tutorial >In PHPmailer, Sending HTML Code Only Sends Raw HTML: How to Fix?
Question:
When sending emails using PHPmailer, I've set the content type to HTML, but my recipients receive the raw HTML code instead of the rendered content.
Code Snippet:
<code class="php">$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->IsHTML(true); $mail->Subject = $Subject; $mail->Body = $Body;</code>
Solution:
The issue is that the isHTML() method is called before the Body property is set. To fix this, set the Body property first, then call isHTML():
<code class="php">$mail->Subject = $Subject; $mail->Body = $Body; $mail->IsHTML(true); // <=== Call IsHTML() after $mail->Body has been set.</code>
The above is the detailed content of In PHPmailer, Sending HTML Code Only Sends Raw HTML: How to Fix?. For more information, please follow other related articles on the PHP Chinese website!