You have to MIME encode your message. The BEST thing you could ever do is take a look at the source of an email message that has an attachment and it'll make a lot more sense (Thunderbird makes this easy to do: click on message -> View -> Message Source).
Look into base64 encoding and chunk_parts (check them out on php.net and you just might find code to do the entire attachment process).