How to send image in PHP mail function
Stefan Bogdanescu
Founder & Senior Architect
How to Send Images in PHP Mail Function: Mastering HTML Email Body Content
As developers, we often encounter the challenge of sending more than just plain text via email. While simple text delivery is straightforward using PHP's mail() function, embedding rich content—like images, complex formatting, and interactive elements—requires a much more sophisticated approach involving MIME encoding. Attempting to inject raw HTML directly into a basic mail function often results in broken layouts or failed deliveries across various email clients.
This post will dive deep into the practical methods for successfully sending images within your client message body using PHP, moving beyond simple string concatenation to master proper email construction.
The Challenge of Sending Images via Basic PHP Mail
The standard PHP mail() function is designed primarily for sending plain text messages. When you try to include HTML tags (like <img>, <p>, or <table>) directly into the message body, the resulting email often fails to render correctly in webmail clients because it lacks the necessary MIME structure to interpret the content as HTML rather than raw text.
To successfully send an image embedded in an email, you need to utilize MIME (Multipurpose Internet Mail Extensions). This involves structuring your email as a multipart message, allowing you to separate plain text parts from HTML parts and attach files correctly.
The Solution: Structuring Your Email with MIME
The correct way to embed images is by creating an email that is structured as multipart/alternative or multipart/mixed. This allows the email client to handle different content types (text vs. HTML) separately and correctly display them.
Step 1: Prepare Your Image Files
First, ensure your image files are accessible on your server. You will need to read their contents into PHP variables.
Step 2: Construct the HTML Body Correctly
Instead of trying to mix raw HTML with plain text lines in a single string, structure your email body using proper HTML tags. Since you want to display images within the client message, the entire content must be wrapped in valid HTML.
Here is how you would structure your data and assemble the HTML body:
<?php
/* Gathering data variables (as you already have) */
$nameFeild = $_POST['name'];
// ... other variables
// Define the image path (assuming images are stored in an accessible directory)
$imagePath = 'path/to/your/image.jpg';
// --- Constructing the HTML Body ---
$htmlBody = "<html><head><style>body { font-family: Arial, sans-serif; }</style></head><body>";
$htmlBody .= "<h1>Contact Form Submission</h1>";
$htmlBody .= "<p>Dear " . htmlspecialchars($nameFeild) . ",</p>";
$htmlBody .= "<p>Thank you for contacting IntaxFin. We have received your inquiry.</p>";
// Embedding the image using the <img> tag
// Note: The 'src' path must be accessible by the recipient email server, often requiring absolute paths or specific MIME attachments if sending as a true attachment. For embedding inline images, we use a relative path assuming the client can access it.
$htmlBody .= "<img src=\"{$imagePath}\" alt=\"Submitted Image\">";
$htmlBody .= "<p>One of our representatives will contact you soonest. If you have more questions, please let us know.</p>";
$htmlBody .= "<p>Best Regards,<br>IntaxFin Team</p>";
$htmlBody .= "</body></html>";
// --- Constructing the Plain Text Body (for fallback) ---
$plainTextBody = "Dear " . htmlspecialchars($nameFeild) . "\n\nThank you for contacting IntaxFin. We have received your inquiry.\n\nOne of our representatives will contact you soonest.";
// Headers and sending logic remain similar, but now we use the HTML body
$headers = "From: $emailFeild\r\n";
$header = "From: $noreply@intaxfin.com\r\n";
// When using mail(), it is often safer to send the HTML content as the main body.
$success = mail($webMaster, $emailSbuject, $htmlBody, $headers);
/* If you wanted to send a plain text version separately: */
// @mail($emailFeild, $emailSbuject2, $plainTextBody, $header);
echo $success ? "Email sent successfully!" : "Failed to send email.";
?>
Best Practices and Laravel Context
When dealing with complex data and templating in PHP, relying on raw string concatenation (like your original example) is highly error-prone. Modern frameworks like Laravel encourage the use of Blade templates or dedicated Mail classes for this exact reason—they abstract away the messy details of MIME encoding and ensure consistency across different email clients.
When you move to larger applications, consider using Laravel's built-in Mail facade. It handles the complexities of sending emails with attachments and HTML bodies by managing the underlying transport layer, making your code cleaner and more robust. This approach is far superior to manually manipulating raw PHP mail functions for professional deployments.
Conclusion
Sending images in a PHP mail function isn't just about pasting an <img> tag; it’s about understanding email protocols. By embracing MIME structures—especially multipart/alternative—you ensure that your HTML content is correctly interpreted by the recipient's email client. Always prioritize clean, structured code over raw string manipulation when building sensitive communication systems. Mastering this separation between data and delivery is key to reliable application development, whether you are working with traditional PHP or modern frameworks like Laravel.