PHP mail函数使用:发送HTML邮件及嵌入图片方法
Hey there! Let's tackle your HTML email and PHP mail() questions step by step.
First up, there's a small bug in your current code: you're overwriting the $headers variable, which means your MIME version and content-type settings are getting lost. Here's the corrected, working version:
$to = 'abc@gmail.com'; // Don't forget quotes around email addresses! $subject = "New good bye profile claim request"; $from = "xyz@gmail.com"; // Removed the extra leading space // Build headers correctly (don't overwrite them!) $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= "From: $from" . "\r\n"; // Add \r\n to properly separate header lines $message = '<html><body>'; $message .= '<h1 style="color:#f40;">Hi Jane!</h1>'; $message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>'; // Add your remaining HTML content here $message .= '</body></html>'; // Send the email mail($to, $subject, $message, $headers);
How the PHP mail() Function Works
The mail() function is PHP's built-in way to send emails, with this basic syntax:
mail(string $to, string $subject, string $message, array|string $headers = [], string $additional_params = ""): bool
Let's break down each key parameter:
- $to: The recipient's email address (you can add multiple recipients separated by commas, e.g.,
'user1@example.com, user2@example.com'). - $subject: The email subject — critical note: this can't contain any newline characters (
\nor\r), otherwise many mail servers will flag it as spam. - $message: The email body. For HTML emails, this needs to be full valid HTML (like your example). For plain text, just regular unformatted text.
- $headers: Optional but essential for HTML emails. This is where you set MIME version, content type, sender address, reply-to, and other metadata. Each header line should end with
\r\n. - $additional_params: Optional, used to pass extra flags to your mail server (like specifying a custom sendmail path).
Embedding Images in HTML Emails
There are two reliable ways to add images to your HTML emails:
1. Using Absolute URLs (Simplest & Most Reliable)
Link to an image hosted on a public server. This avoids attachment headaches and works across most email clients:
$message .= '<img src="https://yourdomain.com/images/proposal-photo.jpg" alt="Proposal Photo" style="width:300px;">';
Just make sure the URL is publicly accessible and use a full absolute path (not a relative one like /images/photo.jpg).
2. Using CID (Inline Attachments)
If you want to embed the image directly into the email (so it doesn't rely on external links), you can attach it and reference it via a CID (Content ID):
// Create a unique boundary to separate email parts $boundary = uniqid('email-boundary'); // Update headers for multipart email $headers = "MIME-Version: 1.0\r\n"; $headers .= "From: $from\r\n"; $headers .= "Content-Type: multipart/related; boundary=" . $boundary . "\r\n"; // Build the HTML part of the email $message = "--" . $boundary . "\r\n"; $message .= "Content-Type: text/html; charset=iso-8859-1\r\n"; $message .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; $message .= '<html><body>'; $message .= '<h1 style="color:#f40;">Hi Jane!</h1>'; // Reference the image using its CID $message .= '<img src="cid:proposal_image" alt="Proposal Image">'; $message .= '</body></html>' . "\r\n\r\n"; // Attach the image file $image_path = '/path/to/your/proposal-photo.jpg'; $image_name = 'proposal-photo.jpg'; $image_type = 'image/jpeg'; $message .= "--" . $boundary . "\r\n"; $message .= "Content-Type: $image_type; name=\"$image_name\"\r\n"; $message .= "Content-Transfer-Encoding: base64\r\n"; $message .= "Content-ID: <proposal_image>\r\n"; // Matches the CID in the HTML $message .= "Content-Disposition: inline; filename=\"$image_name\"\r\n\r\n"; // Encode the image to base64 for email $image_content = file_get_contents($image_path); $message .= chunk_split(base64_encode($image_content)) . "\r\n"; // Close the boundary $message .= "--" . $boundary . "--"; // Send the email mail($to, $subject, $message, $headers);
Keep in mind that some email clients might block inline attachments by default, so using absolute URLs is often the more consistent choice.
内容的提问来源于stack exchange,提问作者shoeb

