On Friday I finally cracked a PHP problem that had been plaguing one of my applications. The problem was actually kind of simple in retrospect. I had to have my script send an email with an attachment. No biggie, right? Well, easier said then done at least. So had my script all set up and the emails were sending out perfectly, except Outlook couldn’t didn’t receive the attachements. (Pine could though!) After a couple of days of arguing with the code, I cleared my head and sent myself an email with Outlook and there was the solution. It seems that all of the guides that tell you how to use PHP’s mail function to send an attachment have Content-type: multipart/alternative in the header, which isn’t technically wrong, but really doesn’t work. All it takes is some simple thoughts (which were obviously beyond me at the time) to figure out that alternative means exactly that and mixed, means many types in the one message. Here’s the script I ended up with:
I was using it to send jpegs but the code could be adapted for anything. Also, if you wanted the body of the message to be either HTML or plain text, you could make the body’s content type multipart/alternative and have it’s own boundary, then in each parts of the body one would be text/html and the other text/plain. I know there’s some scripts that I need to update in the nearer future now too, like my web mail program which hasn’t really been working right since I moved it to my terabyte.