In PHPmailer, Sending HTML Code Only Sends Raw HTML: How to Fix?

Barbara Streisand
Release: 2024-10-22 14:19:03
Original
262 people have browsed it

In PHPmailer, Sending HTML Code Only Sends Raw HTML: How to Fix?

PHPmailer: Sending HTML Code

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>
Copy after login

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>
Copy after login

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!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!