Topic: Disappearing Attachments -Simple attachment mail script (Read 461 times)
Just another rainy day
Posts: 1
10 credits Members referred : 0
« on: Oct 26, 2007, 08:32:18 AM »
When I was doing my research to try to find a PHP script to use as an example, this one looked to me like one of the simpler ones that I could use. So I copied this script and started in on getting it to work in my situation.
As it turns out, I learned a hard lesson. You aren't supposed to use dashes as part of the definition of the mime boundary.
How do I know this? Well, I spend some 10 hours getting everything in basically working order, and got to the point where an attachment showed at first in the email client, but as soon as the email was clicked on, the attachment symbol would disappear, in both email client and web mail.
I even got my helper in on this on a second day. We both looked at various permutations, and and I had explained to him that in my research I had found out that these mime sections were sensitive to the number of line feeds at the end of certain lines, etc.
Even after he and I looked at it together for a couple of hours, we were still getting nowhere.
He went home, and I finally started trying other scripts, including one that I had passed by on my initial research because it looked more complicated than this one, this Simple attachment mail script. Turns out that one worked immediately when I adapted it.
Once I had that other script working, while it was tempting to just use it and not worry about finding out, I was still extremely curious to know what the difference was. So I made my existing script more and more like the one that worked, and tested every single small change, one email at a time.
As my mail_attachment() function became more and more like the other script, at one point, I noticed that my script that didn't work was passing in "" for the "message" portion, and trying to pass all of the message body in through the "headers" option on the call to the PHP "mail" funtion.
Obviously, that seemed to be a big difference, so I changed up my script to have two variables, one for "header" and another for "email_message". Then the call to the PHP mail function, instead of being:
if (mail($mailto, $subject, "", $header))
became:
if (mail($mailto, $subject, $email_message, $header))
I thought to myself, "Well, surely that will be it." But no. It really came down to whether or not you define the mime boundary with dashes in it, or not. Apparently, it confuses the client to have the dashes as part of the mime boundary.
Taking the dashes out of the boundary got me an attachment that stayed put, but still had some odd artifacts on the screen, and the attachment name wasn't coming through correctly. It was coming through as "Part 1.2"
This then, was the part where I changed the last few extra line-feeds to match the successful email.
I was running a KDiff between the source of the successful email and the unsuccessful one every time I made the slightest change, and the last two changes were apparently crucial. You can't have a line feed on the tail end of the $header. It MUST end in a quote, which was the last character of the boundary definition line.
Even then, the base64 encoded text was still showing up in the body of the email as text, after a line that apparently represented the boundary. So I took one extra line feed out of the mime boundary that was just before the attached file, and VOILA! Everything works as it should!!!!!!!
Wow . . . . that was hard!
When I started writing this post, I was thinking that the simple attachment script posted on this site had the dashes in the definition of the mime boundary, however, during the writing of this post, I looked again, and I see that the dashes are not there on the original.
I suppose that I must have at some point looked at the definition of the mime boundary and thought hmm, that doesn't match what I am seeing in the successful message source, and added them in there.
Anyway, for anyone who runs up against disappearing attachments, I hope this helps.
Later, Bruce W. Moore MooreSW dot com
« Last Edit: Oct 26, 2007, 10:20:09 AM by Olaf »
Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6280
38506 credits Members referred : 374