amberb617 Posted July 15, 2009 Share Posted July 15, 2009 The code's attached. It refuses to upload/attach images to e-mails that are sent once the form is submitted. I can't seem to get it to work... [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/166007-having-problems-attaching-images-to-e-mails/ Share on other sites More sharing options...
ignace Posted July 15, 2009 Share Posted July 15, 2009 Add the code here, don't attach it. Link to comment https://forums.phpfreaks.com/topic/166007-having-problems-attaching-images-to-e-mails/#findComment-875657 Share on other sites More sharing options...
amberb617 Posted July 15, 2009 Author Share Posted July 15, 2009 Woops, sorry! <?php require('class.phpmailer.php'); $EmailFrom = Trim(stripslashes($_POST['element_13'])); $EmailTo = "[email protected]"; $Subject = "Lab Member Update Form"; $element_1_1 = Trim(stripslashes($_POST['element_1_1'])); $element_1_2 = Trim(stripslashes($_POST['element_1_2'])); $element_2 = Trim(stripslashes($_POST['element_2'])); $element_13 = Trim(stripslashes($_POST['element_13'])); $element_12 = Trim(stripslashes($_POST['element_12'])); $element_3_1 = Trim(stripslashes($_POST['element_3_1'])); $element_3_2 = Trim(stripslashes($_POST['element_3_2'])); $element_3_3 = Trim(stripslashes($_POST['element_3_3'])); $element_3_4 = Trim(stripslashes($_POST['element_3_4'])); $element_3_5 = Trim(stripslashes($_POST['element_3_5'])); $element_3_6 = Trim(stripslashes($_POST['element_3_6'])); $element_4_1 = Trim(stripslashes($_POST['element_4_1'])); $element_4_2 = Trim(stripslashes($_POST['element_4_2'])); $element_4_3 = Trim(stripslashes($_POST['element_4_3'])); $element_5_1 = Trim(stripslashes($_POST['element_5_1'])); $element_5_2 = Trim(stripslashes($_POST['element_5_2'])); $element_5_3 = Trim(stripslashes($_POST['element_5_3'])); $element_7 = Trim(stripslashes($_POST['element_7'])); $element_8 = Trim(stripslashes($_POST['element_8'])); $element_10 = Trim(stripslashes($_POST['element_10'])); $element_11 = Trim(stripslashes($_POST['element_11'])); $uploaded_file = Trim(stripslashes($_POST['uploaded_file'])); $Header = "MIME-Version: 1.0\r\n"; $Header .= "Content-type: text/html; charset=iso-8859-1\r\n"; $Body = "<br><br>"; $Body .= "<b>Name:</b> "; $Body .= $_POST['element_1_1'] . $_POST['element_1_2']; $Body .= "<BR>"; $Body .= "<b>Title: </b>"; $Body .= $element_2; $Body .= "<BR>"; $Body .= "<b>E-mail: </b>"; $Body .= $element_13; $Body .= "<BR>"; $Body .= "<b>Website: </b>"; $Body .= $element_12; $Body .= "<BR>"; $Body .= "<b>Address: </b>"; $Body .= "<BR>"; $Body .= $_POST['element_3_1'] ."<BR>". $_POST['element_3_2'] ."<BR>". $_POST['element_3_3'] ."<BR>". $_POST['element_3_4'] . $_POST['element_3_5'] ."<BR>". $_POST['element_3_6']; $Body .= "<BR>"; $Body .= "<b>Phone Number: </b>"; $Body .= $_POST['element_4_1'] . $_POST['element_4_2'] . $_POST['element_4_3']; $Body .= "<BR>"; $Body .= "<b>Fax number: </b>"; $Body .= $_POST['element_5_1'] . $_POST['element_5_2'] . $_POST['element_5_3']; $Body .= "<BR>"; $Body .= "<b>Scientifc Interests:</b> "; $Body .= $element_7; $Body .= "<BR>"; $Body .= "<b>Collaborations: </b>"; $Body .= $element_8; $Body .= "<BR>"; $Body .= "<b>Journal Publications: </b>"; $Body .= $element_10; $Body .= "<BR>"; $Body .= "<b>Conference Contributions:</b> "; $Body .= $element_11; $Body .= "<BR>"; $Body .= "<b>Portrait: </b>"; $Body .= $uploaded_file; list($name_of_uploaded_file, $type_of_uploaded_file, $size_of_uploaded_file) = GetUploadedFileInfo(); if(!Validate($name_of_uploaded_file, $type_of_uploaded_file, $size_of_uploaded_file, $max_allowed_file_size)) { exit(); } LoadUploadedFile($name_of_uploaded_file); $path_of_uploaded_file = "uploads/" . $name_of_uploaded_file; include_once('Mail.php'); include_once('mime.php'); ComposeMail($path_of_uploaded_file); //////////////////// Functions //////////////////////// function GetUploadedFileInfo() { $file_info[] = basename($_FILES['uploaded_file']['name']); $file_info[] = substr($file_info[0], strrpos($file_info[0], '.') + 1); $file_info[] = $_FILES["uploaded_file"]["size"]/1024; return $file_info; } function Validate($name_of_uploaded_file, $type_of_uploaded_file, $size_of_uploaded_file, $max_allowed_file_size) { if($size_of_uploaded_file>$max_allowed_file_size ) { echo "Size of file is greater than" . $max_allowed_file_size . " KB. <a href='attachment_email_form.html'>Click Here to upload a smaller sized file.</a>"; return false; } $allowed_extension = array("jpg", "jpeg", "gif", "bmp"); for($i=0; $i<sizeof($allowed_extension); $i++) { $allowed_extension[$i] = strtoupper($allowed_extension[$i]); } $type_of_uploaded_file = strtoupper($type_of_uploaded_file); if(!(in_array(strtoupper($type_of_uploaded_file),$allowed_extension))) { echo "You have uploaded a file with an extension of " . $type_of_uploaded_file . " . This type is not allowed. Please upload a file with allowed image extensions like jpg, jpeg, bmp, gif. <a href='attachment_email_form.html'>Click Here to upload a file with allowed extension.</a>"; return false; } return true; } function LoadUploadedFile($name_of_uploaded_file) { move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], "uploads/" . $name_of_uploaded_file); return true; } function ComposeMail($name_of_uploaded_file) { $name = $_POST['element_1_1'] . $_POST['element_1_2']; $user_message = $_POST['message']; $to = "[email protected]"; $subject="The e-mail was successfully sent."; $from = $_POST['element_13']; $text = "A user " . $name . "has sent you this message and an attachment: " . $user_message; $message = new Mail_mime(); $message->setTXTBody($text); $message->addAttachment($name_of_uploaded_file); $body = $message->get(); $extraheaders = array("From"=>$from, "Subject"=>$subject); $headers = $message->headers($extraheaders); $mail = Mail::factory("mail"); $mail->send($to, $headers, $body); echo "Your Email with attachment was sent."; /*$cv= $image_name; // Obtain file upload vars $image = $_FILES['image']['tmp_name']; $image_type = $_FILES['image']['type']; $image_name = $_FILES['image']['name']; $headers .= "\nReply-To: [email protected]"; $headers .= "\nContent-Type: text/html; charset=UTF-8"; $headers .= "\nMIME-Version: 1.0"; if (is_uploaded_file($image)) { // Read the file to be attached ('rb' = read binary) $file = fopen($image,'rb'); $data = fread($file,filesize($image)); 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/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message2 . "\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: {$image_type};\n" . " name=\"{$image_name}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$image_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; } // send email $success = mail($EmailTo, $EmailFrom, $Subject, $Body, "From: $EmailFrom"); // redirect to success page if ($success){ print "The form has been submitted."; echo $Body; } else{ header ("Content-type: text/plain"); print "Form submission has failed."; echo $Body; }*/ ?> Link to comment https://forums.phpfreaks.com/topic/166007-having-problems-attaching-images-to-e-mails/#findComment-875678 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.