Why Your PHP Email Body Disappears When Using Multipart/Mixed
The core issue in your script isn't that the PDF attachment is missing; it's how you are constructing the MIME message structure. You are attempting to send a multipart/mixed email, which allows for both text content and attachments, but you have violated the strict formatting rules required by the multipart boundary mechanism.
In a multipart/mixed email, every distinct part must begin with a specific header block that includes a Content-ID or is implicitly defined by the boundary string in the main envelope headers. Your current implementation appends the text message after the attachment block without correctly associating it as a separate MIME part under the shared boundary.
The Boundary String Misconfiguration
The most critical error lies in how you define and use $random_hash. You generate a unique MD5 hash to create your boundary string:
$random_hash = md5(date('r', time()));
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
This header tells the email client, "Everything that follows is a collection of parts separated by this specific string." However, your $msg variable structure breaks this contract. You are concatenating the attachment block and the text block directly into one continuous string before passing it to mail().
In a valid MIME message, each part must be preceded by the boundary delimiter followed immediately by a dash (--). Your code sends the attachment, then abruptly switches to the text content without restarting the boundary context or defining a second part correctly. The email parser sees the first block as the complete message and ignores the subsequent text because it doesn't match the expected MIME format for a mixed multipart message.
Correcting the Header Structure
To fix this, you must explicitly define each part within the $msg string. You cannot simply append headers for the second part; they must be prefixed by the boundary delimiter. Furthermore, the Content-Type header for the text part usually needs to include boundary="..." only if it is a sub-part of another container, but in a flat multipart/mixed, each part starts with the boundary marker.
Here is the corrected approach using raw string concatenation:
$boundary = "PHP-mixed-".md5(date('r', time()));
$headers = "From: webmaster@test.ch\r\n";
$headers .= "Reply-To: webmaster@test.ch\r\n";
// Define the main multipart header with the boundary
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n\r\n";
// Part 1: The PDF Attachment
$attach_pdf_multipart = "--".$boundary."\r\n";
$attach_pdf_multipart .= "Content-Type: application/pdf; name=\"invoice.pdf\"\r\n"; // Assuming .pdf extension
$attach_pdf_multipart .= "Content-Disposition: attachment; filename=\"invoice.pdf\"\r\n";
$attach_pdf_multipart .= "Content-Transfer-Encoding: base64\r\n\r\n";
// Append the actual PDF content
$pdf_content = $pdf->Output('', 'S');
$attach_pdf_multipart .= chunk_split(base64_encode($pdf_content));
// Part 2: The HTML Body Message
$body_part = "--".$boundary."\r\n";
$body_part .= "Content-Type: text/html; charset=\"UTF-8\"\r\n\r\n"; // Use UTF-8 instead of iso-8859-1 for better compatibility
$body_part .= "<p>This is text message from shohag</p>\r\n";
// Combine and send
$msg = $attach_pdf_multipart . $body_part . "--".$boundary."--\r\n"; // Terminate the multipart message
$mail_sent = mail($to, $subject, $msg, $headers);
Key Implementation Details
Notice the terminator string at the very end: --".$boundary."--. This is mandatory. The email client expects the stream to end with two dashes following the boundary name. Without this, the parser may hang or truncate the message, potentially discarding your HTML body entirely.
Additionally, consider the Content-Transfer-Encoding for the text body. You used 7bit in your original code, which is acceptable for simple ASCII, but switching to UTF-8 (as shown above) ensures special characters in your email body render correctly across modern clients like Gmail or Outlook. The PDF part remains base64, which is necessary because binary data cannot be transmitted as-is over standard email protocols without corruption.
Modern Alternatives and Security Considerations
While the mail() function works on shared hosting environments, it lacks headers for tracking delivery or setting authentication (SPF/DKIM). For production applications where you need to guarantee deliverability, consider using a dedicated SMTP library like PHPMailer or SwiftMailer. These libraries handle the MIME boundary logic automatically, preventing exactly this type of header collision error.
If you are working within the Laravel ecosystem, the Mailable class provides a clean abstraction for sending PDF attachments alongside HTML bodies without manually managing chunk encoding or boundary strings. This reduces the risk of malformed headers and simplifies maintenance. For more details on modern PHP email practices, see LaravelCompany.
Finally, be aware that @mail() suppresses errors. If the server's mail queue fills up or the remote domain rejects the message, you will receive a "success" signal even if the email failed to leave the server. Always check the return value explicitly and consider logging the full message headers for debugging purposes during development.