Ashish Sood Posted June 12, 2014 Share Posted June 12, 2014 Hi Everyone, I am working on the email part of my project , i don't have any issue regarding sending a text email but i added the feature of attachment now it like the send emails with attachment i read lots of online tutorial for email attachment but none of them help me out. :Sad: my_code.php <html xmlns="http://www.w3.org/1999/xhtml"><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type" /><title>email</title></head><body><form action="" method="post"><input type="text" name="name" /><br/><br/><input name="File1" type="file" /><p><input name="submit" type="submit" value="submit" /></p></form></body></html> email_inc.php if(isset($_POST['submit'])) {$header = 'MIME-Version: 1.0' . "rn"; $header .= 'Content-type: text/html; charset=iso-8859-1' . "rn"; $header .= "From: {$from}rn"; $header .= "Reply-To: {$to}rn"; $header .= "Return-path: " . $to; mail( $to, $subject, $message, $header, "-f{$from}"); } Above script is for simple email without attachment, Please help me out what to do for attachment emails. Thanks In Advance Link to comment Share on other sites More sharing options...
niche Posted June 13, 2014 Share Posted June 13, 2014 http://stackoverflow.com/questions/826265/simple-php-form-attachment-to-email-code-golf Link to comment Share on other sites More sharing options...
Don Jajo Posted June 13, 2014 Share Posted June 13, 2014 Or you can post what you've done so far then we make corrections Link to comment Share on other sites More sharing options...
Ashish Sood Posted June 14, 2014 Author Share Posted June 14, 2014 @Don, before adding changes to my original script which already runs in production i want to test it by my own if everything is ok then i will move my modification into production environment . So above i had posted my email script which is replica of production one, except the message,subject variable. I just want to know what i need to add so that i can send email with attachment. Thanks for the reply Link to comment Share on other sites More sharing options...
Don Jajo Posted June 14, 2014 Share Posted June 14, 2014 Ok then, if so Niche has already posted a link to that The code is a lil bit bulky Link to comment Share on other sites More sharing options...
Ashish Sood Posted June 15, 2014 Author Share Posted June 15, 2014 (edited) @Don this is what i did so far with my script , after that its not sending any mail with attachment. //Get the uploaded file information $name_of_uploaded_file = basename($_FILES['uploaded_file']['name']); //get the file extension of the file $type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1); $size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1024; $allowed_ext = false; for($i=0; $i<sizeof($allowed_extensions); $i++) { if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0) { $allowed_ext = true; } } if(!$allowed_ext) { $errors .= "n The uploaded file is not supported file type. ". " Only the following file types are supported: ".implode(',',$allowed_extensions); } //send the email if(empty($errors)) { //copy the temp. uploaded file to uploads folder $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file; $tmp_path = $_FILES["uploaded_file"]["tmp_name"]; if(is_uploaded_file($tmp_path)) { if(!copy($tmp_path,$path_of_uploaded_file)) { $errors .= 'n error while copying the uploaded file'; } } $from="REPORT@abc.com"; $to="xyz@abc.com"; $subject="Midrange|Testing of $date"; $header .= "From: {$from}rn"; $header .= "Reply-To: {$to}rn"; $header .= "Return-path: " . $to; $filename = $name_of_uploaded_file; $message ="Please find the link of "; $file_size = $size_of_uploaded_file; $content = chunk_split(base64_encode(file_get_contents('http://x.x.x.x/dummy/upload/'.$name_of_uploaded_file))); $uid = md5(uniqid(time())); $from = str_replace(array("r", "n"), '', $from); // to prevent email injection $header = "From: ".$from."rn" ."MIME-Version: 1.0rn" ."Content-Type: multipart/mixed; boundary="".$uid.""rnrn" ."This is a multi-part message in MIME format.rn" ."--".$uid."rn" ."Content-type:text/plain; charset=iso-8859-1rn" ."Content-Transfer-Encoding: 7bitrnrn" .$message."rnrn" ."--".$uid."rn" ."Content-Type: application/octet-stream; name="".$filename.""rn" ."Content-Transfer-Encoding: base64rn" ."Content-Disposition: attachment; filename="".$filename.""rnrn" .$content."rnrn" ."--".$uid."--"; mail( $to, $subject, $message, $header, "-f{$from}"); } Edited June 15, 2014 by Ashish Sood Link to comment Share on other sites More sharing options...
Don Jajo Posted June 15, 2014 Share Posted June 15, 2014 I've wrote this once, try this $email = $_POST['email']; $subject = $_POST['subject']; $message = $_POST['body']; $filename = $_FILES['file']['name']; $size = $_FILES['file']['size']; $mime = $_FILES['file']['type']; $tmp = $_FILES['file']['tmp_name']; $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";$file = fopen($tmp,'rb'); // Now read the file content into a variable $data = fread($file,filesize($tmp)); // close the file fclose($file); // Now we need to encode it and split it into acceptable length lines $data = chunk_split(base64_encode($data)); // Now we'll build the message headers $headers = "From: $emailrn" . "MIME-Version: 1.0rn" . "Content-Type: multipart/mixed;rn" . " boundary="{$mime_boundary}""; // Next, we'll build the message body note that we insert two dashes in front of the MIME boundary when we use it $message = "This is a multi-part message in MIME format.nn" . "--{$mime_boundary}n" . "Content-Type: text/plain; charset="iso-8859-1"n" . "Content-Transfer-Encoding: 7bitnn" . $message . "nn"; // Now we'll insert a boundary to indicate we're starting the attachment we have to specify the content type, file name, and disposition as an attachment, then add the file content and set another boundary to indicate that the end of the file has been reached $message .= "--{$mime_boundary}n" . "Content-Type: {$mime};n" . " name="{$filename}"n" . //"Content-Disposition: attachment;n" . //" filename="{$fileatt_name}"n" . "Content-Transfer-Encoding: base64nn" . $data . "nn" . "--{$mime_boundary}--n"; mail('yourmail@mail.com',$subject,$message,$headers); echo 'Mail Sent!'; Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now