Darrenjs1210 Posted September 28, 2011 Share Posted September 28, 2011 Was just wondering if anyone could help out with what should be a fairly simple fix for a contact form with image upload. The image DOES get uploaded to the temporary folder, but doesn't appear when I receive the email into my account. I think the form is almost good to go, just can't figure out why the image is not attatched to the email when i receive it - guessing the problem is somewhere in the last 10 lines of code. Really appreciate if someone could figure what's I'm doing wrong. (...perhaps it was thinking I could learn PHP in the first place!). Thanks. <?php ini_set("sendmail_from", "[email protected]"); ini_set("SMTP", "smtp.myhostingcompany.co.uk"); if($name = filter_var($_POST['name'])) if($address = filter_var($_POST['address'])) if($postcode = filter_var($_POST['postcode'])) if($phone = filter_var($_POST['phone'])) if($email = filter_var($_POST['email'])) if($details = filter_var($_POST['details'])) if($contactby = filter_var($_POST['contactby'])) /* Subject and Email Destinations */ $emailSubject = 'Work Email!'; $webMaster = '[email protected]'; /* Gathering Data Variables */ $nameField = $_POST['name']; $addressField = $_POST['address']; $postcodeField = $_POST['postcode']; $phoneField = $_POST['phone']; $emailField = $_POST['email']; $detailsField = $_POST['details']; $contactbyField = $_POST['contactby']; $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); $max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB) $upload_path = './uploads/'; $filename = $_FILES['userfile']['name']; $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); if(!in_array($ext,$allowed_filetypes)) die('The file you attempted to upload is not allowed.'); if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize) die('The file you attempted to upload is too large.'); if(!is_writable($upload_path)) die('You cannot upload to the specified directory, please CHMOD it to 777.'); if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) echo 'Your file upload was successful, view the file <a href= "' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked. else echo 'There was an error during the file upload. Please try again.'; // It failed $body = <<<EOD Name: $name Address: $address Postcode: $postcode Phone: $phone Email: $email Details: $details Contactby: $contactby EOD; $headers = "From: $email\r\n"; $headers .= "Contact-type: text/html\r\n"; $attachment = chunk_split(base64_encode(file_get_contents($temp_file))); $tmp_file = $_FILES['userfile']['tmp_name']; $success = mail($webMaster, $attatchment, $body, $headers, "[email protected]"); /* Results rendered as HTML */ $theResults = <<<EOD SENT SENT SENT EOD; echo "$theResults" ?> Quote Link to comment https://forums.phpfreaks.com/topic/248028-simple-attatchment-fix-or-so-i-thought/ Share on other sites More sharing options...
Buddski Posted September 28, 2011 Share Posted September 28, 2011 The second parameter of mail is the subject of the email. Sending an attachment can be tricky, but take a look at this example. php-e-mail-attachment-script Quote Link to comment https://forums.phpfreaks.com/topic/248028-simple-attatchment-fix-or-so-i-thought/#findComment-1273543 Share on other sites More sharing options...
Darrenjs1210 Posted September 28, 2011 Author Share Posted September 28, 2011 Is a slightly different way of getting the attatchment in that example. They use MIME rather than storing the image in a temporary folder. Better for security I guess.... but harder to get working. I looked at the first answer from this thread: http://stackoverflow.com/questions/7323943/php-contact-form-with-file-attachment and thought it seemed a bit easier?? Well........ at least if it actually worked! Quote Link to comment https://forums.phpfreaks.com/topic/248028-simple-attatchment-fix-or-so-i-thought/#findComment-1273554 Share on other sites More sharing options...
Buddski Posted September 28, 2011 Share Posted September 28, 2011 The answer there is using a PHP class called PHPMailer, even though the OP hasnt got it in his code. The only way (as far as I know) to send an attachment in an email is to use MIME. Ive attached a basic example below of how you can add this to your script.. It might require a few tweaks to get it to work. $body = <<<EOD Name: $name Address: $address Postcode: $postcode Phone: $phone Email: $email Details: $details Contactby: $contactby EOD; // Create a Subject // $subject = 'Test Attachment'; // Create your MIME boundary // $semi_rand = md5(time()); $mime_boundary = '==Multipart_Boundary_x'.$semi_rand.'x'; // Make up some headers $headers = 'From: ' . $email . "\r\n" . 'To: '.$webMaster."\r\n" . 'Reply-To: ' . $email . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-Type: multipart/mixed;' . "\r\n" . " boundary=\"".$mime_boundary."\""; // Create a message variable that we can append the data to // $message = ''; // Get the attachment ready // $temp_file = $upload_path . $filename; if (file_exists($temp_file)) { $file_type = mime_content_type($temp_file); $data = file_get_contents($temp_file); $data = chunk_split(base64_encode($data)); // Add the attachment to the message data // $message .= "--".$mime_boundary."\n" . "Content-Type: {$file_type};\n" . " name=\"{$filename}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$filename}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; } // Chuck in the rest of the BODY // $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" . $body . "\n" . "--".$mime_boundary."--\n\n"; $success = mail($webMaster, $subject, $message, $headers, "[email protected]"); You can see where this code is supposed to go in relation to your script.. I have never used the mail() function to send an attachment so I cannot make any guarantees. Also this code was hacked together from a Mail class I wrote many moons ago. Quote Link to comment https://forums.phpfreaks.com/topic/248028-simple-attatchment-fix-or-so-i-thought/#findComment-1273724 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.