didster Posted December 8, 2008 Share Posted December 8, 2008 Hello, Could someone please have a look at my code. I have tried on several forums now, everyone looks at it but dont give me any advice. I am hoping this time, I might be in luck I have no idea what the problem is with it. I altered an existing webform so that I could add attachments including text/images/pdf files. The email works with the attachment but the attachment has zero content. Please please please could someone try and fix it and explain what I am doing wrong. <?php //error_reporting(E_ALL); //ini_set("display_errors", 1); // ------------- CONFIGURABLE SECTION ------------------------ $to = '' ; $subject = "Plug Gauge Quote Request Form" ; $formurl = "/enquiryform/pluggauge.htm"; $errorurl = "/enquiryform/error.htm" ; $thankyouurl = "/enquiryform/thankyou.htm" ; // -------------------- END OF CONFIGURABLE SECTION --------------- $contactname = $_POST['contactname'] ; $companyname = $_POST['companyname'] ; $currentaddress = $_POST['currentaddress'] ; $contactnumber = $_POST['contactnumber'] ; $rfqreference = $_POST['rfqreference'] ; $teeth = $_POST['teeth'] ; $dpmoduletext = $_POST['dpmoduletext'] ; $dpmoduledrop = $_POST['dpmoduledrop'] ; $pressureangle = $_POST['pressureangle'] ; $helixangle = $_POST['helixangle'] ; $helixhand = $_POST['helixhand'] ; $pitchtext = $_POST['pitchtext'] ; $pitchdrop = $_POST['pitchdrop'] ; $gauge = $_POST['gauge'] ; $typefit = $_POST['typefit'] ; $facetext = $_POST['facetext'] ; $facedrop = $_POST['facedrop'] ; $quantity = $_POST['quantity'] ; $materials = $_POST['materials'] ; $notes = $_POST['notes'] ; $chknotest = $_POST['chkno'] ; // Read POST request params into global vars $from = $_POST['from']; // Obtain file upload vars $fileatt = $_FILES['fileatt']['tmp_name']; $fileatt_type = $_FILES['fileatt']['type']; $fileatt_name = $_FILES['fileatt']['name']; $headers = "From: $from"; // 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"; $message .= "\n\n-- Chk --\n\n" . $_POST['chkno'][0] . $_POST['chkno'][1] . $_POST['chkno'][2] ; if (!empty($contactname)) { $message .= "\n\n-- Contact Name --\n\n" . $contactname; } if (!empty($companyname)) { $message .= "\n\n-- Company Name --\n\n" . $companyname; } if (!empty($currentaddress)) { $message .= "\n\n-- Current Address --\n\n" . $currentaddress; } if (!empty($contactnumber)) { $message .= "\n\n-- Contact Number --\n\n" . $contactnumber; } if (!empty($rfqreference)) { $message .= "\n\n-- RFQ Reference Number --\n\n" . $rfqreference; } if (!empty($teeth)) { $message .= "\n\n-- Teeth --\n\n" . $teeth; } if (!empty($dpmoduletext)) { $message .= "\n\n-- DP Module Text --\n\n" . $dpmoduletext; } if (!empty($dpmoduledrop)) { $message .= "\n\n-- DP Module Drop --\n\n" . $dpmoduledrop; } if (!empty($pressureangle)) { $message .= "\n\n-- Pressure Angle --\n\n" . $pressureangle; } if (!empty($helixangle)) { $message .= "\n\n-- Helix Angle --\n\n" . $helixangle; } if (!empty($helixhand)) { $message .= "\n\n-- Helix Hand --\n\n" . $helixhand; } if (!empty($pitchtext)) { $message .= "\n\n-- Pitch Text (Check If Serration) --\n\n" . $dpmoduledrop . " " . $pitchtext; } if (!empty($pitchdrop)) { $message .= "\n\n-- Pitch Drop (Check If Serration) --\n\n" . $dpmoduledrop . " " . $pitchdrop; } if (!empty($gauge)) { $message .= "\n\n-- Gauge Ref/Tool No. --\n\n" . $gauge; } if (!empty($typefit)) { $message .= "\n\n-- Type Fit --\n\n" . $typefit; } if (!empty($facetext)) { $message .= "\n\n-- Face Width Text --\n\n" . $facetext; } if (!empty($facedrop)) { $message .= "\n\n-- Face Width Drop --\n\n" . $facedrop; } if (!empty($quantity)) { $message .= "\n\n-- Quantity --\n\n" . $quantity; } if (!empty($materials)) { $message .= "\n\n-- Materials --\n\n" . $materials; } if (!empty($notes)) { $message .= "\n\n-- Notes --\n\n" . $notes; } // 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"; $http_referrer = getenv( "HTTP_REFERER" ); if (!isset($_POST['from'])) { header( "Location: $formurl" ); exit ; } if (empty($contactname) || empty($from) || empty($companyname) || empty($contactnumber) || empty($teeth) || empty($quantity) || empty($chknotest)) { header( "Location: $errorurl" ); exit ; } $contactname = strtok( $contactname, "\r\n" ); $email = strtok( $email, "\r\n" ); if (get_magic_quotes_gpc()) { $chkno = stripslashes( $chkno ); $companyname = stripslashes( $companyname ); $currentaddress = stripslashes( $currentaddress ); $contactnumber = stripslashes( $contactnumber ); $rfqreference = stripslashes( $rfqreference ); $teeth = stripslashes( $teeth ); $dpmoduletext = stripslashes( $dpmoduletext ); $dpmoduledrop = stripslashes( $dpmoduledrop ); $pressureangle = stripslashes( $pressureangle ); $helixangle = stripslashes( $helixangle ); $helixhand = stripslashes( $helixhand ); $pitchtext = stripslashes( $pitchtext ); $pitchdrop = stripslashes( $pitchdrop ); $gauge = stripslashes( $gauge ); $typefit = stripslashes( $typefit ); $facetext = stripslashes( $facetext ); $facedrop = stripslashes( $facedrop ); $quantity = stripslashes( $quantity ); $materials = stripslashes( $materials ); $notes = stripslashes( $notes ); } ini_set("sendmail_from", $from); mail($to, $subject, $message, $headers); header( "Location: $thankyouurl" ); exit ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/136077-can-no-one-help/ Share on other sites More sharing options...
JonnoTheDev Posted December 8, 2008 Share Posted December 8, 2008 Its because you are using the standard mail() function to send attachments and this is just a daft idea. For mime email use PEAR::Mail_Mime http://pear.php.net/package/Mail_Mime Alternatively use Swiftmailer: http://swiftmailer.org/ Quote Link to comment https://forums.phpfreaks.com/topic/136077-can-no-one-help/#findComment-709531 Share on other sites More sharing options...
PFMaBiSmAd Posted December 8, 2008 Share Posted December 8, 2008 And have you checked that $data contains what you expect? Is the file uploading even working? Quote Link to comment https://forums.phpfreaks.com/topic/136077-can-no-one-help/#findComment-709533 Share on other sites More sharing options...
didster Posted December 8, 2008 Author Share Posted December 8, 2008 Hi thanks for the quick replies! I can send a small text file and that seems to work but pdfs or jpgs dont send properly, just the filename. I am only a beginner so I will look into Pear, cheers guys Quote Link to comment https://forums.phpfreaks.com/topic/136077-can-no-one-help/#findComment-709544 Share on other sites More sharing options...
PFMaBiSmAd Posted December 8, 2008 Share Posted December 8, 2008 It is likely that your upload is failing due to the file size. Your code is not checking $_FILES['fileatt']['error'] before blindly using the uploaded data - http://us2.php.net/manual/en/features.file-upload.errors.php Quote Link to comment https://forums.phpfreaks.com/topic/136077-can-no-one-help/#findComment-709562 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.