bcarlson Posted February 15, 2010 Share Posted February 15, 2010 Hi all, I posted a while back about this problem, and I thought I had figured it out. i was seeing attachments, but all were 1kb in size, which wasn't right. So i went back through and re-wrote my entire script for attaching form data to an e-mail, and now i am not seeing any errors on form submit, but i also am not getting an attachment in the e-mail. IF someone could take a look, maybe i am missing something critical! (its obvious something is wrong though) Thanks for whatever help can be provided! <?php session_start(); /* debug levels for php */ error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); /* defining basic e-mail attributes */ $email_to = "my address"; $email_subject = "Web Site Development Request"; $emailaddress = $_POST['emailaddress']; $email_from = $emailaddress; /* defining form fields */ $title = $_POST['title']; $includeHeader = $_POST['header']; if (isset($_POST['headerimage'])) { $headerimage = $_POST['headerimage']; } $headtext = $_POST['headtext']; $headdisplay = $_POST['headerdisplay']; $clubLogoyn = $_POST['logo']; if (isset($_POST['logoimage'])) { $clubLogo = $_POST['logoimage']; } $bgcolor = $_POST['bgcolor']; $bgcolorchooser = $_POST['bgcolorchooser']; $bgimage = $_POST['bgimage']; if (isset($_POST['bgimageuploader'])) { $bgimageuploader = $_POST['bgimageuploader']; } if (isset($_POST['constitution'])) { $constitution = $_POST['constitution']; } if (isset($_POST['bylaws'])) { $bylaws = $_POST['bylaws']; } if (isset($_POST['officermembers'])) { $officermembers = $_POST['officermembers']; } if (isset($_POST['rforums'])) { $rforums = $_POST['rforums']; } $clubforum = $title; if (isset($_POST['subsection'])) { $subsection = $_POST['subsection']; } $section1 = $_POST['section1']; $section2 = $_POST['section2']; $section3 = $_POST['section3']; $section4 = $_POST['section4']; $section5 = $_POST['section5']; $section6 = $_POST['section6']; $email_message = "Site Development Request: <br> \n Below is the information from the form. Some of the data may be null, and that is OK!<br><br> \n Wesite Title: $title <br>\n Header Image: $includeHeader <br>\n Header Text: $headtext <br>\n Actual Text: $headdisplay <br>\n Club Logo: $clubLogoyn <br>\n Background Color: $bgcolor <br>\n Picked Color: $bgcolorchooser <br>\n Background Image: $bgimage <br>\n Contact Address: $email_from <br>\n Request Forum: $rforums <br>\n Forum Title: $clubforum <br>\n Forum Subsection: $subsection <br>\n Subsection 1: $section1 <br>\n Subsection 2: $section2 <br>\n Subsection 3: $section3 <br>\n Subsection 4: $section4 <br>\n Subsection 5: $section5 <br>\n Subsection 6: $section6 <br>"; /*added content-type for text or html e-mails*/ $headers = "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: " .$email_from; /*attaching the header file*/ if (isset($_POST['headerimage'])) { if ($_FILES["headerimage"]["error"] > 0) { echo "Error: " . $_FILES["headerimage"]["error"] . "<br />"; } else { $fileatt = $_FILES['$headerimage']['tmp_name']; // Path to the file $fileatt_type = $_FILES['$headerimage']['type']; // File Type $fileatt_name = "Header Image"; // Filename that will be used for the file as the attachment, could use actual file name by calling $_FILES['fileatt']['name'] /* debug to see $_FILES array */ echo "Upload: " . $_FILES["headerimage"]["name"] . "<br />"; echo "Type: " . $_FILES["headerimage"]["type"] . "<br />"; echo "Size: " . ($_FILES["headerimage"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["headerimage"]["tmp_name"]; } } else { echo "File not set, post failed" . "<br />"; } if (isset($fileatt)) { if(is_uploaded_file($fileatt)) { if(!($file = fopen($fileatt,'rb'))) echo "File Failed To Open Header File"; $data = fread($file,filesize($fileatt)); fclose($file); } else { /* generate a boundary string */ $semi_rand = md5(time()); //retreive time on system $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; //generate random boundray string /* add headers for the file attachment */ $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; /* add a multipart boundary above the plain message */ $email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_message . "\n\n"; /*encode base64 to allow for binary file types*/ $data = chunk_split(base64_encode($data)); /* add file attachment to message */ $email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}\n"; } } else { echo "File not uploaded to temp locaiton" . $_FILES["headerimage"]["tmp_name"] . "<br>"; } $ok = mail($email_to, $email_subject, $email_message, $headers); if($ok) { echo "<font face=verdana size=2>The file was successfully sent!</font>";ary_x{$semi_rand}x"; //generate random boundray string /* add headers for the file attachment */ $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; /* add a multipart boundary above the plain message */ $email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_message . "\n\n"; /*encode base64 to allow for binary file types*/ $data = chunk_split(base64_encode($data)); /* add file attachment to message */ $email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}\n"; } } else { echo "File not uploaded to temp locaiton" . $_FILES["headerimage"]["tmp_name"] . "<br>"; } $ok = mail($email_to, $email_subject, $email_message, $headers); if($ok) { echo "<font face=verdana size=2>The file was successfully sent!</font>"; } else { die("Sorry but the email could not be sent. Please go back and try again!"); } if ($ok) { print "<script>"; print "self.location='thankyou.html';"; print "</script>"; } else { print "Failed To Submit Form"; } ?> Link to comment https://forums.phpfreaks.com/topic/192166-email-attachment-help/ Share on other sites More sharing options...
bcarlson Posted February 15, 2010 Author Share Posted February 15, 2010 doh! missed final /php tag.. <?php session_start(); /* debug levels for php */ error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); /* defining basic e-mail attributes */ $email_to = "my address"; $email_subject = "Web Site Development Request"; $emailaddress = $_POST['emailaddress']; $email_from = $emailaddress; /* defining form fields */ $title = $_POST['title']; $includeHeader = $_POST['header']; if (isset($_POST['headerimage'])) { $headerimage = $_POST['headerimage']; } $headtext = $_POST['headtext']; $headdisplay = $_POST['headerdisplay']; $clubLogoyn = $_POST['logo']; if (isset($_POST['logoimage'])) { $clubLogo = $_POST['logoimage']; } $bgcolor = $_POST['bgcolor']; $bgcolorchooser = $_POST['bgcolorchooser']; $bgimage = $_POST['bgimage']; if (isset($_POST['bgimageuploader'])) { $bgimageuploader = $_POST['bgimageuploader']; } if (isset($_POST['constitution'])) { $constitution = $_POST['constitution']; } if (isset($_POST['bylaws'])) { $bylaws = $_POST['bylaws']; } if (isset($_POST['officermembers'])) { $officermembers = $_POST['officermembers']; } if (isset($_POST['rforums'])) { $rforums = $_POST['rforums']; } $clubforum = $title; if (isset($_POST['subsection'])) { $subsection = $_POST['subsection']; } $section1 = $_POST['section1']; $section2 = $_POST['section2']; $section3 = $_POST['section3']; $section4 = $_POST['section4']; $section5 = $_POST['section5']; $section6 = $_POST['section6']; $email_message = "Site Development Request: <br> \n Below is the information from the form Some of the data may be null, and that is OK!<br><br> \n Wesite Title: $title <br>\n Header Image: $includeHeader <br>\n Header Text: $headtext <br>\n Actual Text: $headdisplay <br>\n Club Logo: $clubLogoyn <br>\n Background Color: $bgcolor <br>\n Picked Color: $bgcolorchooser <br>\n Background Image: $bgimage <br>\n Contact Address: $email_from <br>\n Request Forum: $rforums <br>\n Forum Title: $clubforum <br>\n Forum Subsection: $subsection <br>\n Subsection 1: $section1 <br>\n Subsection 2: $section2 <br>\n Subsection 3: $section3 <br>\n Subsection 4: $section4 <br>\n Subsection 5: $section5 <br>\n Subsection 6: $section6 <br>"; /*added content-type for text or html e-mails*/ $headers = "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: " .$email_from; /*attaching the header file*/ if (isset($_POST['headerimage'])) { if ($_FILES["headerimage"]["error"] > 0) { echo "Error: " . $_FILES["headerimage"]["error"] . "<br />"; } else { $fileatt = $_FILES['$headerimage']['tmp_name']; // Path to the file $fileatt_type = $_FILES['$headerimage']['type']; // File Type $fileatt_name = "Header Image"; // Filename that will be used for the file as the attachment, could use actual file name by calling $_FILES['fileatt']['name'] /* debug to see $_FILES array */ echo "Upload: " . $_FILES["headerimage"]["name"] . "<br />"; echo "Type: " . $_FILES["headerimage"]["type"] . "<br />"; echo "Size: " . ($_FILES["headerimage"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["headerimage"]["tmp_name"]; } } else { echo "File not set, post failed" . "<br />"; } if (isset($fileatt)) { if(is_uploaded_file($fileatt)) { if(!($file = fopen($fileatt,'rb'))) echo "File Failed To Open Header File"; $data = fread($file,filesize($fileatt)); fclose($file); } else { /* generate a boundary string */ $semi_rand = md5(time()); //retreive time on system $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; //generate random boundray string /* add headers for the file attachment */ $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; /* add a multipart boundary above the plain message */ $email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_message . "\n\n"; /*encode base64 to allow for binary file types*/ $data = chunk_split(base64_encode($data)); /* add file attachment to message */ $email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}\n"; } } else { echo "File not uploaded to temp locaiton" . $_FILES["headerimage"]["tmp_name"] . "<br>"; } $ok = mail($email_to, $email_subject, $email_message, $headers); if($ok) { echo "<font face=verdana size=2>The file was successfully sent!</font>"; } else { die("Sorry but the email could not be sent. Please go back and try again!"); } if ($ok) { print "<script>"; print "self.location='thankyou.html';"; print "</script>"; } else { print "Failed To Submit Form"; } ?> Link to comment https://forums.phpfreaks.com/topic/192166-email-attachment-help/#findComment-1012750 Share on other sites More sharing options...
bcarlson Posted February 16, 2010 Author Share Posted February 16, 2010 For some reason the code in the initial post is all horked up. Ingnore that post. the second post with the code within the php start/end tags is right. If anyone has any helpful suggestion, debugging tips, tricks etc that would be much appreciated. Thanks, Brian Link to comment https://forums.phpfreaks.com/topic/192166-email-attachment-help/#findComment-1013102 Share on other sites More sharing options...
bcarlson Posted February 16, 2010 Author Share Posted February 16, 2010 Me thinks I figured it out. ... its a logic bug within the if/else of (is_upload_...). if(is_uploaded_file($fileatt)) { if(!($file = fopen($fileatt,'rb'))) echo "File Failed To Open Header File"; $data = fread($file,filesize($fileatt)); fclose($file); else... do the rest /* generate a boundary string */ $semi_rand = md5(time()); //retreive time on system $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; //generate random boundray string /* add headers for the file attachment */ $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; . . . . . . so if False, attach the file, if true, just read the dang bytes. .. LOL i moved the else to the end... so now it looks like: if (isset($fileatt)) { if(is_uploaded_file($fileatt)) { if(!($file = fopen($fileatt,'rb'))) echo "File Failed To Open Header File"; $data = fread($file,filesize($fileatt)); fclose($file); /* generate a boundary string */ $semi_rand = md5(time()); //retreive time on system $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; //generate random boundray string /* add headers for the file attachment */ $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; /* add a multipart boundary above the plain message */ $email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_message . "\n\n"; /*encode base64 to allow for binary file types*/ $data = chunk_split(base64_encode($data)); /* add file attachment to message */ $email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}\n"; } else { echo "File not uploaded to temp locaiton" . $_FILES["headerimage"]["tmp_name"] . "<br>"; } } else { echo "File not set post failed!"; } I will test this later when I have FTP access to my webserver. Does anyone else know if this is right or looks like it shoudl work? Link to comment https://forums.phpfreaks.com/topic/192166-email-attachment-help/#findComment-1013255 Share on other sites More sharing options...
bcarlson Posted February 20, 2010 Author Share Posted February 20, 2010 Figured out what was wrong... Here is an example of my form + php script. This is different code than posted before because its for my "email me" scripts. The Form Code: <div id="emailform"> <form name="email" id="email" method="post" enctype="multipart/form-data" action="emailme.php"> <table> <tr> <td align="center" colspan=2"><span style="color:#000000"><b>Send The Developer An Email</b></span></td> </tr> <tr> <td align="right">* From:</td> <td align="left"><input type="text" name="from" size="60"></td> </tr> <tr> <td align="right">* Subject</td> <td align="left"><input type="text" name="subject" size="60"></td> </tr> <tr> <td align="right">CC:</td> <td align="left"><input type="text" name="cc" size="60"></td> </tr> <tr> <td align="right">BCC:</td><td align="left"><input type="text" name="bcc" size="60"></td> </tr> <tr> <td align="right">* Message:</td><td align="left"><TextArea name="message" rows="3" cols="45">Message Here</TextArea></td> </tr> <tr> <td align="right">Attach File</td><td align="left"><input type="file" name="attachment" size="60"></td> </tr> <tr> <td align="right">* Security Code:</td><td><img id="captcha" src="/securimage/securimage_show.php" alt="CAPTCHA Image" /></td> </tr> <tr> <td align="right">* Enter Code Here:</td><td align="left"><input type="text" name="captcha_code" size="10" maxlength="6" /> <a href="#" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false"><span style="color:#000000">Reload Image</span></a></td> </tr> </table> <table> <tr> <td><input class="submit" type="submit" name="Submit" value="Submit"></td><td><input type="reset" name="Reset" value="Reset"></td> </tr> </table> </form> </div><!--end of form --> The PHP code <?php // Read POST request params into global vars $to = "your email address here!"; $from = $_POST['from']; $cc = $_POST['cc']; $bcc = $_POST['bcc']; $subject = $_POST['subject']; $message = $_POST['message']; // Obtain file upload vars $fileatt = $_FILES['attachment']['tmp_name']; $fileatt_type = $_FILES['attachment']['type']; $fileatt_name = $_FILES['attachment']['name']; $headers = "From: $from" . "\r\n" . "CC: $cc"; if (is_uploaded_file($fileatt)) { // Read the file to be attached ('rb' = read binary) $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); // Generate a boundary string $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // Add the headers for a file attachment $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; // Add a multipart boundary above the plain message $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; // Base64 encode the file data $data = chunk_split(base64_encode($data)); // Add file attachment to the message $message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; } //secure capthca check include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php'; $securimage = new Securimage(); if ($securimage->check($_POST['captcha_code']) == false) { // the code was incorrect // handle the error accordingly with your other error checking // or you can do something really basic like this die('The code you entered was incorrect. Go back and try again.'); } else{ // Send the message $ok = @mail($to, $subject, $message, $headers); if ($ok) { echo "Success!"; print "<script>"; print "self.location='thankyou.html';"; print "</script>"; } else { echo "mail failed to send"; print "<script>"; print "self.location='emailme.html';"; print "</script>"; } } ?> This is fully functional. Feel free to use it, modify it, and enjoy it. This took me quite a while to figure out as I am new to the whole PHP world! Enjoy! Link to comment https://forums.phpfreaks.com/topic/192166-email-attachment-help/#findComment-1015262 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.