Send HTML in email via PHP
Sending an HTML-formatted email from a PHP application is a common requirement for notifications, newsletters, and transactional messages. However, it often trips up developers because standard mail() functions are notoriously simplistic by default. To render images and style your message properly, you must explicitly set the MIME type to text/html and include inline CSS or base64-encoded assets.
The most straightforward approach for embedding images without setting up a complex mail server is Data URIs. Instead of attaching external files that might fail due to CORS restrictions or blocked attachments, you encode the image directly into the HTML string. This ensures the recipient sees your branding immediately, provided the user has an internet connection. You can read the file content using file_get_contents() and then base64-encode it with base64_encode().
Here is a practical pattern for constructing such an email:
$imagePath = __DIR__ . '/assets/logo.png';
$encodedImage = 'data:image/png;base64,' . base64_encode(file_get_contents($imagePath));
$htmlContent = <<<HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333;">
<div style="max-width: 600px; margin: 0 auto; padding: 20px;">
<img src="$encodedImage" alt="Company Logo" style="display: block; margin-bottom: 20px;" width="200">
<h1>Welcome to our platform</h1>
<p>This is a fully styled HTML email sent directly from PHP. No external attachments are required.</p>
<a href="#" style="background-color: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 4px;">Get Started</a>
</div>
</body>
</html>
HTML;
// Ensure the message body is treated as HTML, not plain text
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: Notification <noreply@example.com>" . "\r\n";
mail($to, $subject, $htmlContent, $headers);
While Data URIs solve the image delivery problem, they can increase email size significantly if you are sending large assets or high-resolution images. If file size is a concern, you must move to multipart MIME messages. This involves creating a multipart/alternative body that contains both a plain text version and an HTML version, allowing email clients that strip HTML to still read the content.
When handling attachments in PHP, the standard mail() function is insufficient because it does not support MIME boundaries for multiple parts out of the box. You would need to manually construct the headers with Content-Type: multipart/mixed; boundary="----=_Part_...". This requires calculating the boundary string and separating the body into distinct sections for each attachment.
Alternatively, using a framework like Laravel simplifies this process significantly. The Mail facade handles MIME type detection and boundary generation automatically. If you are building on top of Laravel, you can pass an HTML view directly to the mailer without worrying about manual header construction:
use Illuminate\Support\Facades\Mail;
Mail::raw('This is a plain text email', function ($message) {
$message->subject('Notification');
});
// Or for HTML with attachments
Mail::html($htmlContent, function ($message) use ($filePath) {
$message->to($recipient);
$message->attach($filePath, [
'as' => 'document.pdf',
'mime' => 'application/pdf'
]);
});
A critical pitfall to avoid is relying on mail() for production applications that require high deliverability. Many modern email clients block emails sent via simple SMTP or sendmail commands if they lack proper authentication headers like DKIM, SPF, and DMARC. For robust transactional emails, consider switching to a dedicated service like Mailgun, SendGrid, or AWS SES. These services handle the complex MIME multipart logic, IP reputation, and bounce management for you.
If you are integrating with Laravel specifically, check out https://laravelcompany.com for resources on setting up mail drivers and configuring your environment for optimal email delivery.