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.
- Sending HTML Email with PHP
- Why HTML Emails Matter
- Using PHP mail() for HTML Email
- Basic HTML-only Example
- Multipart Alternative (HTML + Plain Text)
- Common Pitfalls When Sending HTML Email
- Using PHPMailer for Reliable HTML Email
- Key Steps with PHPMailer
- Building an HTML Email Template
- Deliverability Best Practices
- Testing Your HTML Email
- Choosing the Right Approach
More from this site
Keep reading the latest coverage
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
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
| Factor | Detail | Context |
|---|---|---|
| SPF Record | Authorizes your sending server IP | Prevents spoofing; required by most receivers |
| DKIM Signature | Cryptographic signature of the message | Added via headers; PHPMailer can generate it |
| DMARC Policy | Tells receivers what to do with unauthenticated mail | Published as a DNS TXT record |
| Plain-text fallback | Multipart/alternative with AltBody | Improves deliverability and accessibility |
| Unsubscribe link | Required for marketing lists | Legal 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.