title: How to send email attachments in PHP
body: <pre><code>  &lt;?php
    // array with filenames to be sent as attachment
    $files = array("sendFiles.php");
  
    // email fields: to, from, subject, and so on
    $to = "dfjdsoj@googlemail.com";
    $from = "mail@mail.com";
    $subject ="My subject";
    $message = "A logo has been sen't by". $_SESSION['loggedin_business_name'];
    $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/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
    $message .= "--{$mime_boundary}\n";
  
    // preparing attachments
    for($x=0;$x&lt;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       
    echo sizeof($files);
    $ok = @mail($to, $subject, $message, $headers);
    if ($ok) {
        echo "&lt;p&gt;mail sent to $to!&lt;/p&gt;";
    } else {
        echo "&lt;p&gt;mail could not be sent!&lt;/p&gt;";
    }
    ?&gt;
  </code></pre>
  
  <p>I get an email with my sendfiles.php then a text file ATT00424.txt. The number changes everytime. Send it to my gmail and it's fine! Very Strange!</p>
  
  <hr>
  
  <pre><code>$files = array("sendFiles.php");
  
    // email fields: to, from, subject, and so on
  
    $to = "hdfiuhufsadhfu@yaho.com";
  
    $from = "mail@mail.com";
  
    $subject ="My subject";
  
    $message = "A logo has been sen't by". $_POST['loggedin_business_name'];
  
    $headers = "From: $from";
  
    // boundary
  
    $semi_rand = md5(time());
  
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  
  // headers for attachment
  
    $headers .= "\r\nMIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\"";
    // multipart boundary
  
    $message = "This is a multi-part message in MIME format.\r\n" . "--{$mime_boundary}\r\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" . "Content-Transfer-Encoding: 7bit\r\n" . $message . "\r\n";
  
    $message .= "--{$mime_boundary}\r\n";
    // preparing attachments
  
    for($x=0;$x&lt;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\"};\r\n" . " name=\"$files[$x]\"\r\n" .
  
        "Content-Disposition: attachment;\r\n" . " filename=\"$files[$x]\"\r\n" .
  
        "Content-Transfer-Encoding: base64\r\n" . $data . "\r\n";
  
        $message .= "--{$mime_boundary}\r\n";
  
    }
  </code></pre>
  
  <p>Adding CRLF in the code fixed the attachment issue however now the message "A logo has been sen't by" has disappeared. Why is this?</p>