brentar Posted April 15, 2009 Share Posted April 15, 2009 Hi- I am pretty new to php. I have a script that takes form information, saves it to a txt file on a network and then attaches the files and emails them. This works on most email apps like outlook, hotmail, etc.. however it does not work for gmail. Here is the code and below that is how it comes to my gmail acct. any thoughts would be great.. I am thinking it is some \n or \r\n problem.. but not sure.. Thanks in advance Hi- I am pretty new to php. I have a script that takes form information, saves it to a txt file on a network and then attaches the files and emails them. This works on most email apps like outlook, hotmail, etc.. however it does not work for gmail. Here is the code and below that is how it comes to my gmail acct. [code] <?php ini_set ("SMTP","xxx.xxx"); ini_set ("sendmail_from","[email protected]"); $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 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 = "D:/"; $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 = "D:/" . $_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 = "D:/"; $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 = "D:/" . $_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 = "D:/"; $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 = "D:/" . $_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"; } } 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 = "D:/" . $infoFileUnique; // Path to the file $infoFile_type = "application/octet-stream";// File Type $Handle = fopen($infoFile, 'w') or die("can't open file"); $Data = "Employment Application\r\n"; fwrite($Handle, $Data); $Data = "\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 = "D:/" . $_POST[First_Name] . $_POST[Last_Name] . "_characteristic.txt"; $infoFile_type1 = "application/octet-stream";// File Type $Handle = fopen($infoFile1, 'w'); $Data = "Employment Application 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 = "[email protected]". ', '; // note the comma $to .= $_POST[email_address]; $subject = "Employment Application"; $email_from = "[email protected]"; // Who the email is from //$headers .= "From: ".$email_from; $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"; $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); ?> Here is how it arrives to gmail... This is a multi-part message in MIME format. --==Multipart_Boundary_x0fb3ddb2b59a453e1eaa8878c8d54dbax Content-Type:text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit --==Multipart_Boundary_x0fb3ddb2b59a453e1eaa8878c8d54dbax Content-Type: application/octet-stream; name="test1_Info.txt" Content-Disposition: attachment; filename3="test1_Info.txt" Content-Transfer-Encoding: base64 U3QuIFBhdWwgUEhBIEVtcGxveW1lbnQgQXBwbGljYXRpb24NCg0KQXBwbGljYW50IEluZm9ybWF0 --==Multipart_Boundary_x0fb3ddb2b59a453e1eaa8878c8d54dbax --==Multipart_Boundary_x0fb3ddb2b59a453e1eaa8878c8d54dbax Content-Type: application/octet-stream; name="test_Characteristics.txt" Content-Disposition: attachment; filename4="test_Characteristics.txt" Content-Transfer-Encoding: base64 U3QuIFBhdWwgUEhBIEVtcGxveW1lbnQgQXBwbGljYXRpb24NCg0KQXBwbGljYW50IENoYXJhY3Rl --==Multipart_Boundary_x0fb3ddb2b59a453e1eaa8878c8d54dbax Link to comment https://forums.phpfreaks.com/topic/154249-solved-mail-function-and-gmail-problem/ Share on other sites More sharing options...
brentar Posted April 17, 2009 Author Share Posted April 17, 2009 The issue was the $headers .= "From: $email_from\n"; I removed that and it works fine now. Link to comment https://forums.phpfreaks.com/topic/154249-solved-mail-function-and-gmail-problem/#findComment-812057 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.