brentar Posted March 31, 2009 Share Posted March 31, 2009 HI- First off, I am not a php programmer. This is my first crack at it. Must say I like it though. I have a form I am working on that users enter applicant info into.. they have the option to add 3 attachments. Upon hitting submit the "applicant info" gets saved to a .txt and the optional "characteristic" information gets saved to a txt. If the user attaches a resume and/or cover letter, etc thos get attached too. Then it all gets emailed to HR. This means that there could potentially be 5 attachments. 3 the user selects and the 2 that get saved to a txt with the user info they entered. My issue is that if the user does not select to add an attachment... the 2 text files come out as gibberish in the email body. But if they attach a file, all the attachments come out fine.I am sure I have a messed up $headers or $messages or something similar but can't put my finer on it. I know the code is a bit wacky... I did a lot of if() because the user could have chosen any attachment box.... that is the way they wanted it.. not sure why... So here is the code. I removed some of it, but the majority is here. If anyone wants it broken down further or has suggestions, let me know.. My mime stuff is choppy at best. Thanks much <?php ini_set ("SMTP","xxxxx"); ini_set ("sendmail_from","xxx@xxx"); if (($_FILES['uploadedfile']['name'][0] != '') || ($_FILES['uploadedfile']['name'][1] != '') || ($_FILES['uploadedfile']['name'][2] != '')) { if ($_FILES['uploadedfile']['name'][0] != ''){ $file = $_FILES['uploadedfile']['name'][0]; $target_path = "xxx"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name'][0]); //$message = "";// Message that the email has in it if($file['error'] == UPLOAD_ERR_OK) { # Do uploading here if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][0], $target_path)) { //echo "The file ". basename( $_FILES['uploadedfile']['name']). //" has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } } else { echo "Invalid file type"; } $fileatt = "xxx" . $_FILES['uploadedfile']['name'][0]; // Path to the file $fileatt_type = "application/octet-stream";// File Type $files = fopen($fileatt,'rb'); $data = fread($files,filesize($fileatt)); fclose($files); $data = chunk_split(base64_encode($data)); //echo $_FILES['uploadedfile']['name']; function findexts ($filename) //splits off extension for dynamic use later. { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } $fileattachment = findexts($_FILES['uploadedfile']['name'][0]);//file extension $fileatt_name = $_FILES['uploadedfile']['name'][0]; $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; } if ($_FILES['uploadedfile']['name'][1] != ''){ $file1 = $_FILES['uploadedfile']['name'][1]; $target_path1 = "xxx"; $target_path1 = $target_path1 . basename( $_FILES['uploadedfile']['name'][1]); if($file1['error'] == UPLOAD_ERR_OK) { # Do uploading here if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][1], $target_path1)) { //echo "The file ". basename( $_FILES['uploadedfile']['name']). //" has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } } else { echo "Invalid file type"; } $fileatt1 = "xxx" . $_FILES['uploadedfile']['name'][1]; // Path to the file $fileatt_type1 = "application/octet-stream";// File Type $files1 = fopen($fileatt1,'rb'); $data1 = fread($files1,filesize($fileatt1)); fclose($files1); $data1 = chunk_split(base64_encode($data1)); //echo $_FILES['uploadedfile']['name']; function findexts1 ($filename1) //splits off extension for dynamic use later. { $filename1 = strtolower($filename1) ; $exts1 = split("[/\\.]", $filename1) ; $n1 = count($exts1)-1; $exts1 = $exts[$n1]; return $exts1; } $fileattachment1 = findexts1($_FILES['uploadedfile']['name'][1]);//file extension $fileatt_name1 = $_FILES['uploadedfile']['name'][1]; $semi_rand1 = md5(time()); $mime_boundary1 = "==Multipart_Boundary_x{$semi_rand}x"; } if ($_FILES['uploadedfile']['name'][2] != ''){ $file2 = $_FILES['uploadedfile']['name'][2]; $target_path2 = "xxx"; $target_path2 = $target_path2 . basename( $_FILES['uploadedfile']['name'][2]); if($file2['error'] == UPLOAD_ERR_OK) { # Do uploading here if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][2], $target_path2)) { //echo "The file ". basename( $_FILES['uploadedfile']['name']). //" has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } } else { echo "Invalid file type"; } $fileatt2 = "xxx" . $_FILES['uploadedfile']['name'][2]; // Path to the file $fileatt_type2 = "application/octet-stream";// File Type $files2 = fopen($fileatt2,'rb'); $data2 = fread($files2,filesize($fileatt2)); fclose($files2); $data2 = chunk_split(base64_encode($data2)); //echo $_FILES['uploadedfile']['name']; function findexts2 ($filename2) //splits off extension for dynamic use later. { $filename2 = strtolower($filename2) ; $exts2 = split("[/\\.]", $filename2) ; $n2 = count($exts2)-1; $exts2 = $exts[$n2]; return $exts2; } $fileattachment2 = findexts2($_FILES['uploadedfile']['name'][2]);//file extension $fileatt_name2 = $_FILES['uploadedfile']['name'][2]; $semi_rand2 = md5(time()); $mime_boundary2 = "==Multipart_Boundary_x{$semi_rand}x"; } $headers = "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $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" . $message . "\n\n"; } function left($string,$count) { $string = substr($string,0,$count); return $string; } if ($_POST[fulltime] == "Yes") { $fulltime = "Yes"; } elseif ($_POST[fulltime] == "No") { $fulltime = "No"; } else { $fulltime = "None Selected"; } if ($_POST[parttime] == "Yes") { $parttime = "Yes"; } elseif ($_POST[parttime] == "No") { $parttime = "No"; } else { $parttime = "None Selected"; } if ($_POST[tempwork] == "Yes") { $tempwork = "Yes"; } elseif ($_POST[tempwork] == "No") { $tempwork = "No"; } else { $tempwork = "None Selected"; } if ($_POST[internwork] == "Yes") { $internwork = "Yes"; } elseif ($_POST[internwork] == "No") { $internwork = "No"; } else { $internwork = "None Selected"; } if ($_POST[discharge] == "Honorable") { $discharge = "Honorable"; } elseif ($_POST[discharge] == "Medical") { $discharge = "Medical"; } elseif ($_POST[discharge] == "Other") { $discharge = "Medical"; } else { $discharge = ""; } if ($_POST[ethnicity] == "hispanic") { $ethnicity = "Hispanic"; } elseif ($_POST[ethnicity] == "white") { $ethnicity = "white" ; } elseif ($_POST[ethnicity] == "africanAm") { $ethnicity = "africanAm"; } elseif ($_POST[ethnicity] == "PacIsland") { $ethnicity = "PacIsland"; } elseif ($_POST[ethnicity] == "Asian") { $ethnicity = "Asian"; } elseif ($_POST[ethnicity] == "other") { $ethnicity = "other"; } else{ $ethnicity = "None Selected"; } if ($_POST[militatySpouseDeathMonth] == "Month" || $_POST[militatySpouseDeathDay] == "Day" || $_POST[militatySpouseDeathYear] == "Year") { $milDecease = "0000-00-00"; } else{ if ($_POST[militatySpouseDeathMonth] == "January") { $militatySpouseDeathMonth = "01"; } elseif ($_POST[militatySpouseDeathMonth] == "February"){ $militatySpouseDeathMonth = "02"; } elseif ($_POST[militatySpouseDeathMonth] == "March"){ $militatySpouseDeathMonth = "03"; } elseif ($_POST[militatySpouseDeathMonth] == "April"){ $militatySpouseDeathMonth = "04"; } elseif ($_POST[militatySpouseDeathMonth] == "May"){ $militatySpouseDeathMonth = "05"; } elseif ($_POST[militatySpouseDeathMonth] == "June"){ $militatySpouseDeathMonth = "06"; } elseif ($_POST[militatySpouseDeathMonth] == "July"){ $militatySpouseDeathMonth = "07"; } elseif ($_POST[militatySpouseDeathMonth] == "August"){ $militatySpouseDeathMonth = "08"; } elseif ($_POST[militatySpouseDeathMonth] == "September"){ $militatySpouseDeathMonth = "09"; } elseif ($_POST[militatySpouseDeathMonth] == "October"){ $militatySpouseDeathMonth = "10"; } elseif ($_POST[militatySpouseDeathMonth] == "November"){ $militatySpouseDeathMonth = "11"; } else { $militatySpouseDeathMonth = "12"; } } if ($_POST[startMonth] == "Month" || $_POST[startDay] == "Day" || $_POST[startYear] == "Year") { $startdate = "0000-00-00"; } else{ if ($_POST[startMonth] == "January") { $startmonth = "01"; } elseif ($_POST[startMonth] == "February"){ $startmonth = "02"; } elseif ($_POST[startMonth] == "March"){ $startmonth = "03"; } elseif ($_POST[startMonth] == "April"){ $startmonth = "04"; } elseif ($_POST[startMonth] == "May"){ $startmonth = "05"; } elseif ($_POST[startMonth] == "June"){ $startmonth = "06"; } elseif ($_POST[startMonth] == "July"){ $startmonth = "07"; } elseif ($_POST[startMonth] == "August"){ $startmonth = "08"; } elseif ($_POST[startMonth] == "September"){ $startmonth = "09"; } elseif ($_POST[startMonth] == "October"){ $startmonth = "10"; } elseif ($_POST[startMonth] == "November"){ $startmonth = "11"; } else { $startmonth = "12"; } } if ($_POST[Company1StartMonth] == "Month" || $_POST[Company1StartYear] == "Year") { $company1startdate = "00-0000"; } else { $company1startdate = $_POST[Company1StartMonth] . "-" . $_POST[Company1StartYear]; } if ($_POST[Company1EndMonth] == "Month" || $_POST[Company1EndYear] == "Year") { $company1enddate = "00-0000"; } else { $company1enddate = $_POST[Company1EndMonth] . "-" . $_POST[Company1EndYear]; } if ($_POST[Company2StartMonth] == "Month" || $_POST[Company2StartYear] == "Year") { $company2startdate = "00-0000"; } else { $company2startdate = $_POST[Company2StartMonth] . "-" . $_POST[Company2StartYear]; } if ($_POST[Company2EndMonth] == "Month" || $_POST[Company2EndYear] == "Year") { $company2enddate = "00-0000"; } else { $company2enddate = $_POST[Company2EndMonth] . "-" . $_POST[Company2EndYear]; } if ($_POST[Company3StartMonth] == "Month" || $_POST[Company3StartYear] == "Year") { $company3startdate = "00-0000"; } else { $company3startdate = $_POST[Company3StartMonth] . "-" . $_POST[Company3StartYear]; } if ($_POST[Company3EndMonth] == "Month" || $_POST[Company3EndYear] == "Year") { $company3enddate = "00-0000"; } else { $company3enddate = $_POST[Company3EndMonth] . "-" . $_POST[Company3EndYear]; } if ($_POST[Company4EndMonth] == "Month" || $_POST[Company4EndYear] == "Year") { $company4enddate = "00-0000"; } else { $company4enddate = $_POST[Company4EndMonth] . "-" . $_POST[Company4EndYear]; } $middleName = left($_POST[Middle_Name], 1); $startdate = $_POST[startYear] . "-" . $startmonth . "-" . $_POST[startDay]; $milDecease = $_POST[militatySpouseDeathYear] . "-" . $militatySpouseDeathMonth . "-" . $_POST[militatySpouseDeathDay]; $todaysdate = date("y-m-d"); //echo $todaysdate; $phone = $_POST[HphoneAC]; $cellphone = $_POST[MphoneAC]; $busphone = $_POST[bphone]; $infoFileUnique = $_POST[First_Name] . $_POST[Last_Name] . "_Info.txt"; // create and populate text files. $infoFile = "xxx" . $infoFileUnique; // Path to the file $infoFile_type = "application/octet-stream";// File Type $Handle = fopen($infoFile, 'w') or die("can't open file"); $Data = "Applicant Information\r\n"; fwrite($Handle, $Data); $Data = "\r\n"; fwrite($Handle, $Data); $Data = "-------------------------------------------------------\r\n"; fwrite($Handle, $Data); $Data = "\r\n"; fwrite($Handle, $Data); $Data = "Personal Information\r\n"; fwrite($Handle, $Data); fclose($Handle); $files3 = fopen($infoFile,'rb'); $data3 = fread($files3,filesize($infoFile)); fclose($files3); $data3 = chunk_split(base64_encode($data3)); //echo $_FILES['uploadedfile']['name']; $fileatt_name3 = $infoFile; //characteristic survey $infoFile1 = "xxx" . $_POST[First_Name] . $_POST[Last_Name] . "_characteristic.txt"; $infoFile_type1 = "application/octet-stream";// File Type $Handle = fopen($infoFile1, 'w'); $Data = "Applicant Characteristics\r\n"; fwrite($Handle, $Data); $Data = "\r\n"; fwrite($Handle, $Data); fclose($Handle); $infoFileUnique1 = $_POST[First_Name] . $_POST[Last_Name] . "_Characteristics.txt"; $files4 = fopen($infoFile1,'rb'); $data4 = fread($files4,filesize($infoFile1)); fclose($files4); $data4 = chunk_split(base64_encode($data4)); //echo $_FILES['uploadedfile']['name']; $fileatt_name4 = $infoFile1; $to = "xxx@xxx". ', '; // note the comma $to .= $_POST[email_address]; $subject = "Application"; $email_from = "xxx@xxx"; // Who the email is from //$headers .= "From: ".$email_from; $headers .= "From: ".$email_from; //if (($_FILES['uploadedfile']['name'][0] != '') || ($_FILES['uploadedfile']['name'][1] != '') || ($_FILES['uploadedfile']['name'][2] != '')){ //$headers .= $headers = "MIME-Version: 1.0\n" . //"Content-type: text/html; charset=iso-8859-1\n"; //} if (($_FILES['uploadedfile']['name'][0] != '') || ($_FILES['uploadedfile']['name'][1] != '') || ($_FILES['uploadedfile']['name'][2] != '')){ // Always set content-type when sending HTML email $message .= "--{$mime_boundary}\n"; } if ($_FILES['uploadedfile']['name'][0] != ''){ $message .= "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"; $message .= "--{$mime_boundary}\n"; unset($data); unset($file); unset($fileatt); unset($fileatt_type); unset($fileatt_name); } if ($_FILES['uploadedfile']['name'][1] != ''){ $message .="Content-Type: {$fileatt_type1};\n" . " name=\"{$fileatt_name1}\"\n" . "Content-Disposition: attachment;\n" . " filename1=\"{$fileatt_name1}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data1 . "\n\n"; $message .= "--{$mime_boundary}\n"; unset($data1); unset($file1); unset($fileatt1); unset($fileatt_type1); unset($fileatt_name1); } if ($_FILES['uploadedfile']['name'][2] != ''){ $message .="Content-Type: {$fileatt_type2};\n" . " name=\"{$fileatt_name2}\"\n" . "Content-Disposition: attachment;\n" . " filename2=\"{$fileatt_name2}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data2 . "\n\n"; $message .= "--{$mime_boundary}\n"; unset($data2); unset($file2); unset($fileatt2); unset($fileatt_type2); unset($fileatt_name2); } $semi_rand3 = md5(time()); $mime_boundary3 = "==Multipart_Boundary_x{$semi_rand3}x"; $message .= "--{$mime_boundary3}\n"; $message .="Content-Type: {$infoFile_type};\n" . " name=\"{$infoFileUnique}\"\n" . "Content-Disposition: attachment;\n" . " filename3=\"{$infoFileUnique}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data3 . "\n\n"; $message .= "--{$mime_boundary3}\n"; $message .= "--{$mime_boundary3}\n"; $message .="Content-Type: {$infoFile_type1};\n" . " name=\"{$infoFileUnique1}\"\n" . "Content-Disposition: attachment;\n" . " filename4=\"{$infoFileUnique1}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data4 . "\n\n"; $message .= "--{$mime_boundary3}\n"; unset($data3); unset($files3); unset($data4); unset($files4); unset($infoFile); unset($infoFile1); unset($fileatt_name3); unset($fileatt_name4); mail($to,$subject,$message,$headers); ?> Link to comment https://forums.phpfreaks.com/topic/151931-email-attachment-issue-mimeheadermessage/ Share on other sites More sharing options...
Brian W Posted March 31, 2009 Share Posted March 31, 2009 please use the code tags around your php or even html snipits, you will likely get more help [ code ]Like this but without the spaces in the tags[ /code ] would be Like this but without the spaces in the tags Link to comment https://forums.phpfreaks.com/topic/151931-email-attachment-issue-mimeheadermessage/#findComment-797839 Share on other sites More sharing options...
brentar Posted March 31, 2009 Author Share Posted March 31, 2009 as an additional note, here is what gets put in the body of the email --==Multipart_Boundary_xf3bfd25dd1f31e84c48560f005576ebbx Content-Type: application/octet-stream; name="xxx.txt" Content-Disposition: attachment; filename3="xxx.txt" Content-Transfer-Encoding: base64 U3QuIFBhdWwgUEhBIEVtcGxveW1lbnQgQXBwbGljYXRpb24NCg0KQXBwbGljYW50IEluZm9ybWF0 aW9uDQoNCi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t LS0tLS0NCg0KUGVyc29uYWwgSW5mb3JtYXRpb24NCk5hbWU6R2lsYmVydHNvbiAsIEJyZW50IA0K QXBwbGljYXRpb24gRGF0ZTogMDktMDMtMzENClBvc2l0aW9uIERlc2lyZWQ6IHByb2dyYW1tZXIN CkFkZHJlc3M6IDY4MTEgMTQ2dGggQ2lyY2xlIE5XDQpDaXR5OiBSYW1zZXkNClN0YXRlOiBNaW5u ZXNvdGENClpJUDogNTUzMDMNCkhvbWUgUGhvbmU6IDc2Mzc0MjI2MTYNCkJ1c2luZXNzIFBob25l OiA3NjM3NDIyNjE2DQpDZWxsIFBob25lOiA3NjM3NDIyNjE2DQpEZXNpcmVkIFN0YXJ0IERhdGU6 Link to comment https://forums.phpfreaks.com/topic/151931-email-attachment-issue-mimeheadermessage/#findComment-798019 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.