14, October 2008

Disappearing Attachments -Simple attachment mail script - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  PHP classes @finalwebsites.com  >  Miscellaneous scripts or snippets (Moderator: Olaf)
Topic: Disappearing Attachments -Simple attachment mail script
« previous next »
Pages: [1] Print

Author Topic: Disappearing Attachments -Simple attachment mail script  (Read 518 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: Male
Posts: 6353
38942 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Oct 26, 2007, 10:22:29 AM »

Creating a mail script that can handle attachments (MIME) is a lot of work. this Snipped was a first start and I wrote this later:
http://www.finalwebsites.com/snippets.php?id=41 Visit through proxy

and today I use phpmailer because I send every email via SMTP Smiley


Last blog : Upload images for usage in TinyMCE
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=7289
Tags : [email attachments] [PHP email] [MIME attachments] [MIME boundaries] [dashes] Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  PHP classes @finalwebsites.com  >  Miscellaneous scripts or snippets (Moderator: Olaf)
Topic: Disappearing Attachments -Simple attachment mail script
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Oct 14, 2008, 01:30:29 PM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 36.975
Total Topics: 7.563
Total Members: 4.164
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: blueberry62

32 Guests, 5 Users online :

19 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.