Add HTML formatting in phpmailer

Add HTML formatting in phpmailer

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Missing Header: Why Your PHPMailer Body Renders as Text

When you switch from mail() to PHPMailer, you inherit a robust library for handling SMTP authentication and connection details. However, this transition often reveals a subtle configuration gap regarding MIME types. If your HTML strings—composed of <p> tags, <br /> elements, or inline styles—are arriving in recipients' inboxes as raw code rather than rendered content, the culprit is almost invariably the Content-Type header.

In the scenario described, the developer correctly constructs the message body using standard HTML syntax:

$message = '<p>The following request was sent from:</p>';
$message .= '<p>Name: '.$name.'</p><br />';
$message .= '<p>Phone: '.$phone.'</p><br />';
$message .= '<p>Email: '.$email.'</p><br />';

The logic here is sound. The problem emerges during transmission. By default, PHPMailer's Send() method often defaults to a text/plain content type unless explicitly instructed otherwise. When the receiving mail client (like Outlook or Gmail) encounters an email marked as plain text, it does not attempt to parse HTML tags; instead, it displays them literally. This explains why the recipient sees <p> and <br /> rather than formatted paragraphs.

The solution requires setting the ContentType property before invoking the send method. In PHPMailer v6 and later, this is handled by a dedicated property:

$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->Host = "smtp.example.com";
$mail->Port = 587; // Using port 25 with SMTP auth often requires STARTTLS or explicit configuration
$mail->SMTPAuth = true;
$mail->Username = EMAIL_USER;
$mail->Password = EMAIL_PASS;
$mail->FromName = $from_name;
$mail->From = $from;
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AddAddress($recipient_email);

// The critical step: explicitly define the content type
$mail->ContentType = 'text/html'; 

While setting ContentType to 'text/html' resolves the rendering issue, relying solely on this property is not best practice for modern email delivery. Modern mail clients and servers expect a multipart MIME structure if you plan to include attachments or if you want to ensure maximum compatibility across different user agents. The standard approach is to enable the alternative send method, which automatically handles the Content-Type header and sets up the necessary multipart boundaries.

$mail->isHTML(true); 
// OR in newer versions where properties are preferred:
// $mail->setContentType('text/html');

When you call $mail->isHTML(true) (or set the content type directly in newer API versions), PHPMailer automatically prepends the required headers, such as Content-Type: text/html; charset=UTF-8. This signals to the mail server that the body contains markup, prompting clients to render the tags.

There is a trade-off worth noting regarding the use of <br /> versus CSS spacing. While HTML allows for <br />, many modern email clients strip out non-standard tags or ignore them in favor of inline styles. Relying on <br /> for layout can sometimes lead to inconsistent rendering, particularly in older versions of Outlook that prefer table-based layouts over flexible flexbox or grid systems. For critical transactional emails, it is often safer to use a single <p> tag with explicit line breaks or CSS line-height properties if the client supports them.

Furthermore, ensure your SMTP port configuration matches your provider's requirements. The original example used port 25 without authentication, which is typical for unauthenticated relay servers but incompatible with modern ISP and corporate firewalls that require TLS/SSL (usually ports 465 or 587). If you intend to use authentication (SMTPAuth = true), verify that your Host and Port settings align with the specific SMTP gateway you are connecting to.

Finally, if you are operating within a Laravel ecosystem, consider leveraging existing packages or services like those offered by LaravelCompany for streamlined mail handling, which can abstract these header configurations entirely. However, understanding the underlying mechanics of ContentType remains essential for debugging when custom logic is injected into the message body.

Our network