Sending an Email PHP Form with File attachment

Hi My name is Raghav… I was trying the above mentioned requirement from morning. Till now i did not get what i want…

Could anyone help me out of this problem??

its urgent… :frowning:

 if(isset($_POST['submit']) && $_POST['submit']=='Submit')


$subject="File sent by ".$_POST['name'];

// get the sender's name and email address
 // we'll just plug them a variable to be used later

 $from = stripslashes($_POST['name'])."<".stripslashes($_POST['email']).">";

$name = $_POST['name'];
 $email_address = $_POST['email'];
 $message = $_POST['comment'];

// generate a random string to be used as the boundary marker
 if($_FILES['filename']['tmp_name'] != ''){
 // store the file information to variables for easier access
 $tmp_name = $_FILES['filename']['tmp_name'];
 $type = $_FILES['filename']['type'];
 $file_name = $_FILES['filename']['name'];
 $size = $_FILES['filename']['size'];
 // here we'll hard code a text message
 // again, in reality, you'll normally get this from the form submission

if($tmp_name != ''){
 $message = "nn Name: $name nn Email: $email_address nnMessage: nn $message nnHere is your file: $file_name";
 $message = "nn Name: $name nn Email: $email_address nnMessage: nn $message.";
 // if the upload succeded, the file will exist
 if($tmp_name != ''){
 if (file_exists($tmp_name)){

// check to make sure that it is an uploaded file and not a system file

// open the file for a binary read
 $file = fopen($tmp_name,'rb');

// read the file content into a variable
 $data = fread($file,filesize($tmp_name));

// close the file

// now we encode it and split it into acceptable length lines
 $data = chunk_split(base64_encode($data));

// now we'll build the message headers
  $headers = "From: $fromrn";
if( $tmp_name != '' ){
$headers .= "MIME-Version: 1.0rn" .
"Content-Type: multipart/mixed;rn" ;

// 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" .
 "Content-Type:text/plain;charset=iso-8859-1" .
 "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 .=
 "Content-Type: ".$type."" .
 " name=".$file_name."n" .
 //"Content-Disposition: attachment;n" .
 //" filename="{$fileatt_name}"n" .
 "Content-Transfer-Encoding: base64nn" .
 $data . "nn" ;
 // now we just send the message
 if (mail($to, $subject, $message, $headers))
 echo "<div class='msg msg-ok'><p><strong>Message Sent</strong></p></div><br><br>";
 echo "<div class='msg msg-ok'><p><strong>Message sending failed</strong></p></div><br><br>";

<form id="comment" action="attachment.php" method="post" enctype="multipart/form-data">
<label>Name <span></span></label>
<input type="text" name="name" id="name">
<label>Email <span></span></label>
<input type="text" name="email" id="email">
<label>Comment <span></span></label>
<input type="text" name="comment" id="email">
<label>Upload file <span></span></label>
<input type="file" name="filename" id="file">
<input type="submit" name="submit" value="Submit">

Hi Raghav,

You’re going to need to be more specific. What, exactly, do you want to be able to do, and what is the above code not doing for you?

Hi Jake

I was trying to send an email with an attachment including form(Like First name, Last name, E-mail, phone number, etc.,). I am receiving an attachment in mail But unfortunately i couldn’t able to see the form elements in mail.

Please tell me what i am missing in that code.