**the below code suggested in some site to send mail with attachment.

In the below code i have two attachment(TRYTEK.zip,TRYTEK.rar) in the server and with those attachment i want to send mail.

But the below code is not sending mail with those attachment. Its sending only a Blank mail.

Friends and Experts please tell me where i made a mistake. Give me a solution for this.

**

$files = array("TRYTEK.zip","TRYTEK.rar");
    $body = "email in html form";

    // email fields: to, from, subject, and so on
    $to = "$email";
    $from = "example@example.com";
    $subject = "email subject";
    $message = html_entity_decode($body);
    $headers = "From: $from";

    // boundary
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

   // headers for attachment
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"   {$mime_boundary}\"";

    // multipart boundary
    $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
    $message .= "--{$mime_boundary}\n";

    // preparing attachments
    for ($x = 0; $x < count($files); $x++) {
        $file = fopen($files[$x], "rb");
        $data = fread($file, filesize($files[$x]));
        fclose($file);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
            "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
            "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $message .= "--{$mime_boundary}\n";
    }

    // send

    $ok = @mail($to, $subject, $message, $headers);
    if ($ok) {
        echo "<p>mail sent to $to!</p>";
    } else {
        echo "<p>mail could not be sent!</p>";
    }

Dani AI

Generated

Quick diagnosis and a focused fix. The blank message almost always comes from malformed MIME headers/part boundaries or wrong line endings. Common problems in this thread: using "\n" instead of required CRLF ("\r\n") for headers and part separators, extra whitespace inside the quoted boundary so the body parts never match the header, a malformed Content-Type line for attachments (curly braces or incorrect syntax), and not checking file paths/permissions. ’s approach can be made to work, but ’s suggestion to use a library is valid — libraries avoid these low-level pitfalls.

Quick checklist to debug and fix

  • Use "\r\n" for all header lines and between MIME parts (many MTAs require CRLF).
  • Ensure the boundary value in the Content-Type header exactly matches the strings used to separate parts, and contains no extra spaces.
  • Each part must start with "--boundary" and the final boundary must end with "--".
  • Verify full server paths and file permissions with file_exists() before reading attachments.
  • Base64-encode attachments and include Content-Transfer-Encoding: base64.
  • Remove the error-suppression operator (@) so failures surface; check mail logs or return values.

Example (minimal, corrected mail() pattern)

<?php
$to = 'recipient@example.com';
$from = 'sender@example.com';
$subject = 'Subject';
$html = '<p>HTML body here</p>';
$files = ['/full/path/TRYTEK.zip', '/full/path/TRYTEK.rar'];

$boundary = '==Multipart_Boundary_x'.md5(uniqid()).'x';
$headers  = "From: {$from}\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"{$boundary}\"\r\n";

$body  = "--{$boundary}\r\n";
$body .= "Content-Type: text/html; charset=\"UTF-8\"\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= $html . "\r\n\r\n";

foreach ($files as $path) {
    if (!file_exists($path)) continue;
    $data = chunk_split(base64_encode(file_get_contents($path)));
    $name = basename($path);
    $body .= "--{$boundary}\r\n";
    $body .= "Content-Type: application/octet-stream; name=\"{$name}\"\r\n";
    $body .= "Content-Disposition: attachment; filename=\"{$name}\"\r\n";
    $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
    $body .= $data . "\r\n\r\n";
}

$body .= "--{$boundary}--\r\n";
mail($to, $subject, $body, $headers);
?>

If reliability or authenticated SMTP is needed, use a maintained library (for example, PHPMailer). Libraries handle encoding, headers, large attachments and SMTP errors for you — especially helpful when debugging a server that silently drops mail.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.