News

How to Send HTML Email with PHP: A Practical Guide

By 4 min read 121 views
Featured image for How to Send HTML Email with PHP: A Practical Guide

Sending HTML Email with PHP

Sending HTML email with PHP means more than dropping markup into a string. You must tell the receiving client that your message contains HTML by setting the right MIME headers, and you must handle inline styles, images, and multipart boundaries correctly. This guide covers the native mail() approach, the safer library-based route, and the gotchas that trip up most beginners.

More from this site

Keep reading the latest coverage

Browse latest →

Why HTML Emails Matter

Plain-text email works for simple notifications, but HTML email lets you include bold headings, links, product images, and branded layouts that drive engagement. The trade-off is complexity: inconsistent client rendering, spam-filter triggers, and stricter header requirements. When you send HTML email with PHP, you are responsible for structuring the message so it degrades gracefully when images are blocked or the client prefers plain text.

Using PHP mail() for HTML Email

The native mail() function is the simplest entry point. The critical step is setting the Content-Type header to multipart/alternative when you want both HTML and plain-text versions, or text/html when you are sending only the HTML version.

Basic HTML-only Example

  • Set the To, Subject, and message body as your HTML string.
  • Add a Content-Type header: Content-Type: text/html; charset=UTF-8.
  • Optionally add From, Reply-To, and MIME-Version headers.

Multipart Alternative (HTML + Plain Text)

A multipart message uses a unique boundary string to separate the plain-text and HTML parts. Each part needs its own Content-Type declaration, and the final boundary must include two hyphens to signal the end of the message. This approach increases deliverability because spam filters can read the plain-text fallback.

Common Pitfalls When Sending HTML Email

  • Missing or mismatched boundary strings cause the entire message to render as raw code.
  • Using external CSS or <link> tags — most email clients strip them. Inline styles are the standard approach.
  • Long lines in headers or body content can break MIME encoding and trigger spam filters.
  • Sending HTML email with PHP via mail() on an unconfigured server often lands in spam because of missing SPF, DKIM, and DMARC records.

Using PHPMailer for Reliable HTML Email

PHPMailer handles MIME boundaries, encoding, and SMTP authentication automatically, which is why it is the go-to library for production PHP applications. It supports SMTP, sendmail, and PHP mail(), and it makes it straightforward to add inline images, attachments, and HTML templates.

Key Steps with PHPMailer

  • Install via Composer: composer require phpmailer/phpmailer.
  • Create an instance, set isHTML(true), and assign the HTML body with Body and the plain-text alternative with AltBody.
  • Configure SMTP credentials (host, port, encryption, username, password) or use the local sendmail binary.
  • Add recipients, set From and Reply-To addresses, then call send().
  • Building an HTML Email Template

    Keep your HTML email table-based for maximum compatibility across Outlook and older clients. Use inline styles instead of <style> blocks, and test with tools like Mailtrap or Litmus before deploying. When you send HTML email with PHP from a template, load the HTML file into a string, replace placeholder variables, and assign the result to the message body.

    Deliverability Best Practices

    FactorDetailContext
    SPF RecordAuthorizes your sending server IPPrevents spoofing; required by most receivers
    DKIM SignatureCryptographic signature of the messageAdded via headers; PHPMailer can generate it
    DMARC PolicyTells receivers what to do with unauthenticated mailPublished as a DNS TXT record
    Plain-text fallbackMultipart/alternative with AltBodyImproves deliverability and accessibility
    Unsubscribe linkRequired for marketing listsLegal compliance in many jurisdictions

    Testing Your HTML Email

    Before sending to real users, test with a local SMTP capture tool such as Mailtrap, or send to yourself across Gmail, Outlook, and Apple Mail. Check that images load, links work, and the plain-text version is readable. If you see broken layouts or missing styles, inline CSS is likely the issue — refactor and retest.

    Choosing the Right Approach

    For low-volume scripts, PHP mail() with proper headers works. For anything reaching customers or handling transactional email at scale, a library like PHPMailer or a dedicated service such as SendGrid or Mailgun is the safer choice. The library handles encoding and authentication; the service handles reputation and delivery monitoring.

    Editor's pick

    Keep exploring our latest stories

    Fresh reads, picked daily.

    Browse latest
    Share: