shadiadiph Posted February 13, 2009 Share Posted February 13, 2009 Does anyone know what I need to do to this in order to add $data to this email?? <? ini_set ("display_errors", "1"); error_reporting(E_ALL); $subject = ($_POST['subject']); $firstname = ($_POST["firstname"]); $lastname = ($_POST["lastname"]); $email = ($_POST['email']); $telephone = ($_POST["telephone"]); $country = ($_POST["country"]); $position = ($_POST["position"]); $message = ($_POST['message']); $random = ($_POST['random']); $validate = ($_POST['validate']); $data = (fread(fopen($form_data, "r"), filesize($form_data))); if ($random==$validate) { $to = "[email protected]"; $subject = "$subject"; $MsgHeader = "From: APA<[email protected]>\n"; $MsgHeader .= "MIME-Version: 1.0\n"; $MsgHeader .= "Content-type: text/html; charset=iso-8859-1"; $MsgBody = " <html> <head> <title>HTML message</title> </head> <body> <table> <tr><td align='left'><img src='http://www.website.com/pix/logo.gif'></td></tr> </table> <table style='padding-left:20px'> <tr><td> </td></tr> <tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>FIRSTNAME: $firstname</font></td></tr> <tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>LASTNAME: $lastname</font></td></tr> <tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>EMAIL: $email</font></td></tr> <tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>TELEPHONE: $telephone</font></td></tr> <tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>COUNTRY: $country</font></td></tr> <tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>POSITION: $position</font></td></tr> <tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>REASON: $message</font></font></td></tr> <tr><td> </td></tr> </table> </body> </html>"; mail($to, $subject, $MsgBody, $MsgHeader); header("Location:thankyou.php?message=careers"); exit; } else { header("Location:thankyou.php?message=invalid"); exit; } ?> Link to comment https://forums.phpfreaks.com/topic/145017-solved-sending-a-file-attachment/ Share on other sites More sharing options...
coder_ Posted February 13, 2009 Share Posted February 13, 2009 First of all, you are setting header after you have output data. This wont work. And you could use phpmailer script. It is very cool. Supports attachments and lots of other cool stuf.. Or maybe this can help you http://webcheatsheet.com/php/send_email_text_html_attachment.php Link to comment https://forums.phpfreaks.com/topic/145017-solved-sending-a-file-attachment/#findComment-760957 Share on other sites More sharing options...
shadiadiph Posted February 13, 2009 Author Share Posted February 13, 2009 mm thanks I have looked at that but it isn't exactly what I am looking for I have it now so it sends the file but it is sending the binary output so in the received mail I am getting a load of rubbish it isn't sending as a filename and type. Link to comment https://forums.phpfreaks.com/topic/145017-solved-sending-a-file-attachment/#findComment-760999 Share on other sites More sharing options...
PFMaBiSmAd Posted February 13, 2009 Share Posted February 13, 2009 The section at that link called "Sending Email with Attachment" is how you send a file attachment. You must create a multipart/mixed mime message with mime boundaries and then output the correct headers followed by the chunk_split/base64_encode contents of the file in its' section of the message. The phpmailer/swiftmailer classes takes care of doing all that for you when you use their attachment methods. Link to comment https://forums.phpfreaks.com/topic/145017-solved-sending-a-file-attachment/#findComment-761086 Share on other sites More sharing options...
shadiadiph Posted February 13, 2009 Author Share Posted February 13, 2009 mm ok I have it working 95% can anyone see what my problem is. The message sends is received ok has the correct subject file attachment logo and html format message but it keeps cutting off the htm message part after the first </table> tag the bottom disclaimer doesn't come through which starts with This message, and any attachments,? <? ini_set ("display_errors", "1"); error_reporting(E_ALL); $subject = ($_POST['subject']); $firstname = ($_POST["firstname"]); $lastname = ($_POST["lastname"]); $email = ($_POST['email']); $telephone = ($_POST["telephone"]); $country = ($_POST["country"]); $position = ($_POST["position"]); $message = ($_POST['message']); $random = ($_POST['random']); $validate = ($_POST['validate']); $fileatt = $_FILES['fileatt']['tmp_name']; $fileatt_type = $_FILES['fileatt']['type']; $fileatt_name = $_FILES['fileatt']['name']; if ($random==$validate) { header("Location:thankyou.php?message=careers"); if (is_uploaded_file($fileatt)) { $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $to = "[email protected]"; $headers = "From: [email protected]"; $headers .= "\nMIME-Version: 1.0\n"; $headers .= "Content-Type: multipart/mixed;\n"; $headers .= " boundary=\"{$mime_boundary}\""; $message .= "\n\n"; $message .= "--{$mime_boundary}\n"; $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n"; $message .= " <html> <head> <title>HTML message</title> </head> <body> <table> <tr><td align='left'><img src='http://www.website.com/pix/logo.gif'></td></tr> </table> <table style='padding-left:20px'> <tr><td> </td></tr> <tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>FIRSTNAME: $firstname</font></td></tr> <tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>LASTNAME: $lastname</font></td></tr> <tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>EMAIL: $email</font></td></tr> <tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>TELEPHONE: $telephone</font></td></tr> <tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>COUNTRY: $country</font></td></tr> <tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>POSITION: $position</font></td></tr> <tr><td><font style='font-size: 13px' style='font-family: Tahoma, Arial'>REASON: $message</font></font></td></tr> <tr><td> </td></tr> </table> <table> <tr><td><font style='font-size: 10px' style='font-family: Tahoma, Arial'>This message, and any attachments, is intended only for the use of the individual or entity to which it is addressed, and may contain confidential, proprietary and/or privileged information that is exempt from disclosure under applicable law which is not waived or lost by any transmission failure. If the reader of this message is not the intended recipient or its employee, or agent responsible for delivering the message to the intended recipient, you are hereby notified that any use, dissemination, distribution or copying of this communication and any attachments hereto is strictly prohibited. If you have received this communication in error, please destroy this message. Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorised to state them to be the views of another person or entity. </font></td></tr> </table> </body> </html>"; $data = chunk_split(base64_encode($data)); $message .= "--{$mime_boundary}\n"; $message .= "Content-Type: {$fileatt_type};\n"; $message .= " name=\"{$fileatt_name}\"\n"; $message .= "Content-Disposition: attachment;\n"; $message .= " filename=\"{$fileatt_name}\"\n"; $message .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; } mail($to, $subject, $message, $headers); exit; } else { header("Location:thankyou.php?message=invalid"); exit; } ?> Link to comment https://forums.phpfreaks.com/topic/145017-solved-sending-a-file-attachment/#findComment-761127 Share on other sites More sharing options...
shadiadiph Posted February 13, 2009 Author Share Posted February 13, 2009 got it working now with or without attachments but is there anything i can add to this to disallow .exe files being attached? <?php ini_set('display_errors', '1'); error_reporting(E_ALL); $subject = $_POST['subject']; $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['email']; $telephone = $_POST['telephone']; $country = $_POST['country']; $position = $_POST['position']; $textmessage = $_POST['message']; $ipaddress = $_POST['ipaddress']; $random = $_POST['random']; $validate = $_POST['validate']; $fileatt = $_FILES['fileatt']['tmp_name']; $fileatt_type = $_FILES['fileatt']['type']; $fileatt_name = $_FILES['fileatt']['name']; if($random == $validate) { $to = '[email protected]'; $boundary = '==Multipart_Boundary_x' .md5(time()). 'x'; // headers $headers = "From: [email protected]\n"; $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-Type: multipart/mixed;\n boundary=\"$boundary\"\n\n"; // message $message = "--{$boundary}\n"; $message .= "Content-Type: text/html; charset=iso-8859-1\n"; $message .= "Content-Transfer-Encoding: 7bit\n\n"; $message .= "<html> <head> <title>HTML message</title> </head> <body> <table> <tr><td><img src=\"http://www.website.com/pix/logo.gif\"></td></tr> </table> <table style=\"padding-left: 20px;\"> <tr><td style=\"font-size: 13px; font-family: tahoma, san-serif;\">FIRSTNAME: $firstname</td></tr> <tr><td style=\"font-size: 13px; font-family: tahoma, san-serif;\">LASTNAME: $lastname</td></tr> <tr><td style=\"font-size: 13px; font-family: tahoma, san-serif;\">EMAIL: $email</td></tr> <tr><td style=\"font-size: 13px; font-family: tahoma, san-serif;\">TELEPHONE: $telephone</td></tr> <tr><td style=\"font-size: 13px; font-family: tahoma, san-serif;\">COUNTRY: $country</td></tr> <tr><td style=\"font-size: 13px; font-family: tahoma, san-serif;\">POSITION: $position</td></tr> <tr><td style=\"font-size: 13px; font-family: tahoma, san-serif;\">REASON: $textmessage</td></tr> <tr><td style=\"font-size: 13px; font-family: tahoma, san-serif;\">IP ADDRESS: $ipaddress</td></tr> </table> <table> <tr><td style=\"font-size: 10px; font-family: tahoma, san-serif;\">This message, and any attachments, is intended only for the use of the individual or entity to which it is addressed, and may contain confidential, proprietary and/or privileged information that is exempt from disclosure under applicable law which is not waived or lost by any transmission failure. If the reader of this message is not the intended recipient or its employee, or agent responsible for delivering the message to the intended recipient, you are hereby notified that any use, dissemination, distribution or copying of this communication and any attachments hereto is strictly prohibited. If you have received this communication in error, please destroy this message. Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorised to state them to be the views of another person or entity.</td></tr> </table> </body> </html> "; if(is_uploaded_file($fileatt)) { // attachment $fp = fopen($fileatt, 'rb'); $data = fread($fp, filesize($fileatt)); fclose($fp); $data = chunk_split(base64_encode($data)); $message .= "--{$boundary}\n"; $message .= "Content-Type: $fileatt_type;\n name=\"$fileatt_name\"\n"; //$message .= "Content-Disposition: attachment;\n filename=\"$fileatt_name\"\n"; $message .= "Content-Transfer-Encoding: base64;\n\n$data\n\n"; $message .= "--{$boundary}--\n"; } mail($to, $subject, $message, $headers); header("Location: thankyou.php?message=careersyes"); exit; } else { header("Location: thankyou.php?message=careersno"); exit; } ?> Link to comment https://forums.phpfreaks.com/topic/145017-solved-sending-a-file-attachment/#findComment-761636 Share on other sites More sharing options...
The Little Guy Posted February 13, 2009 Share Posted February 13, 2009 This is how I did it on my site: $random_hash = md5(date('r', time())); $mime_boundary = "==Multipart_Boundary_x{$random_hash}x"; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-Type: multipart/mixed; boundary="'.$mime_boundary.'"' . "\r\n"; // Additional headers $headers .= 'To: Billy Bob <'.$_POST['to'].'>' . "\r\n"; $headers .= 'From: Jimmy John <[email protected]>' . "\r\n"; // File Info $message = $_POST['message']."\n\n". "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; $fileList = ''; foreach ($_FILES["file"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["file"]["tmp_name"][$key]; $name = $_FILES["file"]["name"][$key]; $fName = "../attach/".md5(uniqid(rand(), false)).$name; move_uploaded_file($tmp_name, $fName); $fileatt = $fName; // Path to the file $fileatt_type = "application/octet-stream"; // File Type $fileatt_name = $name; // Filename that will be used for the file as the attachment $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); $data = chunk_split(base64_encode($data)); $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"; unset($data); unset($file); unset($fileatt); unset($fileatt_type); unset($fileatt_name); } mail($_POST['to'],$_POST['subject'],$message,$headers); header('Location: /mail?action=inbox'); exit; Link to comment https://forums.phpfreaks.com/topic/145017-solved-sending-a-file-attachment/#findComment-761642 Share on other sites More sharing options...
shadiadiph Posted February 13, 2009 Author Share Posted February 13, 2009 mmm I am a bit confused as according to w3schools you are allowing .exe file octet-stream can be a .exe file?? Link to comment https://forums.phpfreaks.com/topic/145017-solved-sending-a-file-attachment/#findComment-761672 Share on other sites More sharing options...
The Little Guy Posted February 13, 2009 Share Posted February 13, 2009 Now it checks (it is the second if statement): Edit (forgot a parenthesis): $random_hash = md5(date('r', time())); $mime_boundary = "==Multipart_Boundary_x{$random_hash}x"; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-Type: multipart/mixed; boundary="'.$mime_boundary.'"' . "\r\n"; // Additional headers $headers .= 'To: Billy Bob <'.$_POST['to'].'>' . "\r\n"; $headers .= 'From: Jimmy John <[email protected]>' . "\r\n"; // File Info $message = $_POST['message']."\n\n". "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; $fileList = ''; foreach ($_FILES["file"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["file"]["tmp_name"][$key]; $name = $_FILES["file"]["name"][$key]; $fName = "../attach/".md5(uniqid(rand(), false)).$name; move_uploaded_file($tmp_name, $fName); $path_parts = pathinfo($fName); if($path_parts['extension'] != 'exe'){ // Check for extension type $fileatt = $fName; // Path to the file $fileatt_type = "application/octet-stream"; // File Type $fileatt_name = $name; // Filename that will be used for the file as the attachment $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); $data = chunk_split(base64_encode($data)); $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"; unset($data); unset($file); unset($fileatt); unset($fileatt_type); unset($fileatt_name); }else{ unlink($fName); // Remove file if it is an exe } } } mail($_POST['to'],$_POST['subject'],$message,$headers); header('Location: /mail?action=inbox'); exit; Link to comment https://forums.phpfreaks.com/topic/145017-solved-sending-a-file-attachment/#findComment-761675 Share on other sites More sharing options...
shadiadiph Posted February 13, 2009 Author Share Posted February 13, 2009 I have been looking but can't find it anywhere is there a file function that will destroy a file Link to comment https://forums.phpfreaks.com/topic/145017-solved-sending-a-file-attachment/#findComment-761717 Share on other sites More sharing options...
The Little Guy Posted February 14, 2009 Share Posted February 14, 2009 I have been looking but can't find it anywhere is there a file function that will destroy a file unlink Link to comment https://forums.phpfreaks.com/topic/145017-solved-sending-a-file-attachment/#findComment-761800 Share on other sites More sharing options...
shadiadiph Posted February 16, 2009 Author Share Posted February 16, 2009 thanks Link to comment https://forums.phpfreaks.com/topic/145017-solved-sending-a-file-attachment/#findComment-763240 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.