Skip to content
PHP Logo

What are some best practices for sending HTML emails in PHP?

When sending HTML emails in PHP, it's important to ensure that the email content is properly formatted and structured to display correctly in various email clients. One best practice is to use inline CSS styles for consistent formatting across different platforms. Additionally, including a plain text version of the email as a fallback is recommended for clients that do not support HTML.

php
// Example code snippet for sending HTML email in PHP
$to = "recipient@example.com";
$subject = "HTML Email Test";
$message = "<html><body><h1>Hello, this is a test email!</h1><p>This is a sample HTML email sent from PHP.</p></body></html>";

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";

// Additional headers
$headers .= "From: sender@example.com" . "\r\n";

// Send the email
mail($to, $subject, $message, $headers);