I've been trying to use PHP Mail function for sending an email with HTML body and an attached file. Without the attached file, I receive my HTML email without problems, but when I try to attach a file, I've got all the MIME info included in the body -also the attachment, encoded.

Here's the code for the email function without attachment - that works just fine:

```
        $this->to = $to;
        $this->subject = $subject;
        $this->message = $message;
        $this->headers = "From: " . Mailer::FROM_EMAIL . "\r\n";
        $this->headers .= "MIME-Version: 1.0\r\n";
        $this->headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

```

And here's the code for the email-with-attachment version:

```
        $this->to = $to;
        $this->subject = $subject;
        $this->attachment = 
                chunk_split(base64_encode(file_get_contents($attachment)));        

        //create a boundary string. It must be unique 
        //so we use the MD5 algorithm to generate a random hash        
        $boundary = md5(date('r', time()));                
        $this->headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com\r\n";        
        $this->headers .= "MIME-Version: 1.0\r\n ";
        $this->headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-$boundary\"\r\n"; 

        $this->message =    
"--PHP-mixed-$boundary
Content-Type: text/html; charset=\"ISO-8859-1\"

" . $message . "

--PHP-mixed-$boundary
Content-Type: application/pdf; name=\"test.pdf\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

".$this->attachment ."
--PHP-mixed-$boundary--";

```

I'm using this function to send the email:

```
public function send(){

    if (preg_match(Mailer::PATTERN, trim(strip_tags($this->to)))) { 
        $cleanedTo = trim(strip_tags($this->to)); 
    } else { 
        return FALSE; 
    }                 
    return mail ($cleanedTo, $this->subject, $this->message, $this->headers); 
}

```

And I create the Mailer object as follows:

```
$mailer = new Mailer("youremail@gmail.com", "test mail", "Some <b>old good</b> HTML email", 'pdf/test.pdf');
//$mailer = new Mailer("youremail@gmail.com", "test mail", "Some <b>old good</b> HTML email");

$mailer->send();

```

And the email I receive is:

```
--PHP-mixed-e37929b72bbc6f8e3b37cf802619aac1
Content-Type: text/html; charset="ISO-8859-1"

Some <b>old good</b> HTML email

--PHP-mixed-e37929b72bbc6f8e3b37cf802619aac1
Content-Type: application/pdf; name="test.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

JVBERi0xLjMKMSAwIG9iago8 ... FT0YK

--PHP-mixed-e37929b72bbc6f8e3b37cf802619aac1--

```

I think I'm really close to the answer, but... I'll need your help to find it.