• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1445
  • Last Modified:

PHP mail attachment form

I got a mail form now that will allow for users filling out the form to submit a message with an attachment.  Now I want to it to display an error message if the the name field, the message field, or the email field are left blank.  
<?php
 if ($_SERVER['REQUEST_METHOD']=="POST"){
 $path_info = pathinfo($_FILES['filename']['name']);
        if (!in_array(strtolower($path_info['extension']),array('pdf','zip'))) {
                die ("can not upload this type of file");
        }
 
 
   // we'll begin by assigning the To address and message subject
   $to="reddyfire@gmail.com";
 
   $subject="E-mail with attachment";
 
   // get the sender's name and email address
   // we'll just plug them a variable to be used later
   $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
 
   
   
   // generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
 
   // store the file information to variables for easier access
   $tmp_name = $_FILES['filename']['tmp_name'];
   $type = $_FILES['filename']['type'];
   $name = $_FILES['filename']['name'];
   $size = $_FILES['filename']['size'];
   $nimi =  $_REQUEST['nimi'];
   $toimi =  $_REQUEST['toimi'];
   $Puhelinnumero =  $_REQUEST['Puhelinnumero'];
   $sahkoposti =  $_REQUEST['fromemail'];
 
 
   // here we'll hard code a text message
   // again, in reality, you'll normally get this from the form submission
   $message = stripslashes($_POST['message'])."$message";
 
   // if the upload succeded, the file will exist
   if (file_exists($tmp_name)){
 
      // check to make sure that it is an uploaded file and not a system file
      if(is_uploaded_file($tmp_name)){
 
         // 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
         fclose($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: $from\r\n" .
         "MIME-Version: 1.0\r\n" .
         "Content-Type: multipart/mixed;\r\n" .
         " 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.\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
         $message . "\n\n";
 
      // 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: {$type};\n" .
         " name=\"{$name}\"\n" .
         //"Content-Disposition: attachment;\n" .
         //" filename=\"{$fileatt_name}\"\n" .
         "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n" .
         "--{$mime_boundary}--\n";
 
      // now we just send the message
      if (@mail($to, $subject, $message, $headers))
         echo "OK, sent.";
      else
         echo "Failed to send";
	  }
	  }
?>
<p>Send an e-mail with an attachment:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" 
   enctype="multipart/form-data" name="form1">
   <p>From name: <input type="text" name="fromname"></p>
   <p>From e-mail: <input type="text" name="fromemail"></p>
   <p>Message:<br> <textarea name="message" rows="3" cols="40"></textarea></p>
   <p>File: <input type="file" name="filename"></p>
   <p><input type="submit" name="Submit" value="Submit"></p>
</form>

Open in new window

0
reddyfire
Asked:
reddyfire
1 Solution
 
babuno5Commented:
try this
<?php
 /* if ($_SERVER['REQUEST_METHOD']=="POST"){
           
                //get info about file in array path_info
        $path_info = pathinfo($_FILES['userfile']['name']);
        //check if extension of uploaded file is in array of allowed extensions
        if (!in_array(strtolower($path_info['extension']),array('pdf','doc','txt'))) {
                //if not redirect to error page
                header('Location: http://www.site.com/error.php');
                //don't output anything else on the page, exit script.
                exit;
        }
*/ 
	if ($_POST)
	{
   // we'll begin by assigning the To address and message subject
   $to="info@gmail.com";
 
   $subject="Submission";
 
   // get the sender's name and email address
   // we'll just plug them a variable to be used later
   $from = stripslashes($_POST['nimi'])."<".stripslashes($_POST['fromemail']).">";
 
   // generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
 
   // store the file information to variables for easier access
   $tmp_name = $_FILES['filename']['tmp_name'];
   $type = $_FILES['filename']['type'];
   $name = $_FILES['filename']['name'];
   $size = $_FILES['filename']['size'];
   $nimi =  $_REQUEST['nimi']; 
   $comments	= $_REQUEST['comments'];
 
   // here we'll hard code a text message
   // again, in reality, you'll normally get this from the form submission
   $message = "Here is your file: $name";
 
   // if the upload succeded, the file will exist
   if (file_exists($tmp_name)){
 
      // check to make sure that it is an uploaded file and not a system file
      if(is_uploaded_file($tmp_name)){
 
         // 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
         fclose($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: $from\r\n" .
         "MIME-Version: 1.0\r\n" .
         "Content-Type: multipart/mixed;\r\n" .
         " 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 = "User comments ". $comments .".\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
         $data . "\n\n";
 
      // 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: {$type};\n" .
         " name=\"{$name}\"\n" .
         //"Content-Disposition: attachment;\n" .
         //" filename=\"{$fileatt_name}\"\n" .
         "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n" .
         "--{$mime_boundary}--\n";
 
      // now we just send the message
      if (@mail($to, $subject, $message, $headers))
			header("Location: thankyou.html");
      else
			header("Location: sorry.html");
          }
		die;
	}
?>
<p>Send an e-mail with an attachment:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" 
   enctype="multipart/form-data" name="form1">
   <p>From name: <input type="text" name="fromname"></p>
   <p>From e-mail: <input type="text" name="fromemail"></p>
   <p>message:<br>      </b> <textarea name="comments" rows="3" cols="40"></textarea></p>
   <p>File: <input type="file" name="filename"></p>
   <p><input type="submit" name="Submit" value="Submit"></p>
</form>

Open in new window

0
 
reddyfireAuthor Commented:
I tried your solution but I got the following error:
Warning: Cannot modify header information - headers already sent by (output started at /home/guanajuato/public_html/Guanajuato/sendmail.php:2) in /home/guanajuato/public_html/Guanajuato/sendmail.php on line 88

0
 
agamalCommented:
check the source code of the page containing the header error ... and post it
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
reddyfireAuthor Commented:
Here is the code.

<?php
 if ($_SERVER['REQUEST_METHOD']=="POST"){
 $path_info = pathinfo($_FILES['filename']['name']);
        if (!in_array(strtolower($path_info['extension']),array('pdf','zip'))) {
                die ("can not upload this type of file");
        }
 
 
   // we'll begin by assigning the To address and message subject
   $to="reddyfire@gmail.com";
 
   $subject="E-mail with attachment";
 
   // get the sender's name and email address
   // we'll just plug them a variable to be used later
   $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
 
   
   
   // generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
 
   // store the file information to variables for easier access
   $tmp_name = $_FILES['filename']['tmp_name'];
   $type = $_FILES['filename']['type'];
   $name = $_FILES['filename']['name'];
   $size = $_FILES['filename']['size'];
   $nimi =  $_REQUEST['nimi'];
   $toimi =  $_REQUEST['toimi'];
   $Puhelinnumero =  $_REQUEST['Puhelinnumero'];
   $sahkoposti =  $_REQUEST['fromemail'];
 
 
   // here we'll hard code a text message
   // again, in reality, you'll normally get this from the form submission
   $message = stripslashes($_POST['message'])."$message";
 
   // if the upload succeded, the file will exist
   if (file_exists($tmp_name)){
 
      // check to make sure that it is an uploaded file and not a system file
      if(is_uploaded_file($tmp_name)){
 
         // 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
         fclose($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: $from\r\n" .
         "MIME-Version: 1.0\r\n" .
         "Content-Type: multipart/mixed;\r\n" .
         " 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.\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
         $message . "\n\n";
 
      // 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: {$type};\n" .
         " name=\"{$name}\"\n" .
         //"Content-Disposition: attachment;\n" .
         //" filename=\"{$fileatt_name}\"\n" .
         "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n" .
         "--{$mime_boundary}--\n";
 
      // now we just send the message
      if (@mail($to, $subject, $message, $headers))
            header("Location: thankyou.html");
      else
            header("Location: sorry.html");
          }
                die;
 }
?>
<p>Submit your stories:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" 
   enctype="multipart/form-data" name="form1">
   <p>From name: <input type="text" name="fromname"></p>
   <p>From e-mail: <input type="text" name="fromemail"></p>
   <p>Message:<br> <textarea name="message" rows="3" cols="40"></textarea></p>
   <p>File: <input type="file" name="filename"></p>
   <p><input type="submit" name="Submit" value="Submit"></p>
</form>

Open in new window

0
 
agamalCommented:
i tried youes and babuno5's as weel


babuno5 code worked fine for me ..... try it again ......
0
 
reddyfireAuthor Commented:
I tested it again and still didn't work.  If you look at the 2 codes carefully you will notice that babuno5's code is different than mine and does not contain the message box that allows for comments.  
0
 
agamalCommented:
first f it worked for you and you only missing the comment box we can add it .... we need a working code ... then we can add the extra code we need
0
 
reddyfireAuthor Commented:
Ok the working code is located in the original post.  To save you some time from going back up and looking at it here it is.
<?php
 if ($_SERVER['REQUEST_METHOD']=="POST"){
 $path_info = pathinfo($_FILES['filename']['name']);
        if (!in_array(strtolower($path_info['extension']),array('pdf','zip'))) {
                die ("can not upload this type of file");
        }
 
 
   // we'll begin by assigning the To address and message subject
   $to="reddyfire@gmail.com";
 
   $subject="E-mail with attachment";
 
   // get the sender's name and email address
   // we'll just plug them a variable to be used later
   $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
 
   
   
   // generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
 
   // store the file information to variables for easier access
   $tmp_name = $_FILES['filename']['tmp_name'];
   $type = $_FILES['filename']['type'];
   $name = $_FILES['filename']['name'];
   $size = $_FILES['filename']['size'];
   $nimi =  $_REQUEST['nimi'];
   $toimi =  $_REQUEST['toimi'];
   $Puhelinnumero =  $_REQUEST['Puhelinnumero'];
   $sahkoposti =  $_REQUEST['fromemail'];
 
 
   // here we'll hard code a text message
   // again, in reality, you'll normally get this from the form submission
   $message = stripslashes($_POST['message'])."$message";
 
   // if the upload succeded, the file will exist
   if (file_exists($tmp_name)){
 
      // check to make sure that it is an uploaded file and not a system file
      if(is_uploaded_file($tmp_name)){
 
         // 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
         fclose($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: $from\r\n" .
         "MIME-Version: 1.0\r\n" .
         "Content-Type: multipart/mixed;\r\n" .
         " 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.\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
         $message . "\n\n";
 
      // 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: {$type};\n" .
         " name=\"{$name}\"\n" .
         //"Content-Disposition: attachment;\n" .
         //" filename=\"{$fileatt_name}\"\n" .
         "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n" .
         "--{$mime_boundary}--\n";
 
      // now we just send the message
      if (@mail($to, $subject, $message, $headers))
         echo "OK, sent.";
      else
         echo "Failed to send";
	  }
	  }
?>
<p>Send an e-mail with an attachment:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" 
   enctype="multipart/form-data" name="form1">
   <p>From name: <input type="text" name="fromname"></p>
   <p>From e-mail: <input type="text" name="fromemail"></p>
   <p>Message:<br> <textarea name="message" rows="3" cols="40"></textarea></p>
   <p>File: <input type="file" name="filename"></p>
   <p><input type="submit" name="Submit" value="Submit"></p>
</form>

Open in new window

0
 
agamalCommented:
i added t script to check the form before submit


check and waiting for your updates

<?php
 if ($_SERVER['REQUEST_METHOD']=="POST"){
 $path_info = pathinfo($_FILES['filename']['name']);
        if (!in_array(strtolower($path_info['extension']),array('pdf','zip'))) {
                die ("can not upload this type of file");
        }
 
 
   // we'll begin by assigning the To address and message subject
   $to="reddyfire@gmail.com";
 
   $subject="E-mail with attachment";
 
   // get the sender's name and email address
   // we'll just plug them a variable to be used later
   $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
 
 
 
   // generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
 
   // store the file information to variables for easier access
   $tmp_name = $_FILES['filename']['tmp_name'];
   $type = $_FILES['filename']['type'];
   $name = $_FILES['filename']['name'];
   $size = $_FILES['filename']['size'];
   $nimi =  $_REQUEST['nimi'];
   $toimi =  $_REQUEST['toimi'];
   $Puhelinnumero =  $_REQUEST['Puhelinnumero'];
   $sahkoposti =  $_REQUEST['fromemail'];
 
 
   // here we'll hard code a text message
   // again, in reality, you'll normally get this from the form submission
   $message = stripslashes($_POST['message'])."$message";
 
   // if the upload succeded, the file will exist
   if (file_exists($tmp_name)){
 
      // check to make sure that it is an uploaded file and not a system file
      if(is_uploaded_file($tmp_name)){
 
         // 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
         fclose($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: $from\r\n" .
         "MIME-Version: 1.0\r\n" .
         "Content-Type: multipart/mixed;\r\n" .
         " 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.\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
         $message . "\n\n";
 
      // 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: {$type};\n" .
         " name=\"{$name}\"\n" .
         //"Content-Disposition: attachment;\n" .
         //" filename=\"{$fileatt_name}\"\n" .
         "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n" .
         "--{$mime_boundary}--\n";
 
      // now we just send the message
      if (@mail($to, $subject, $message, $headers))
         echo "OK, sent.";
      else
         echo "Failed to send";
	  }
	  }
?>
<script language="JavaScript">
 
function checkform() {
if (form.fromname.value == "" || form.fromemail.value == "" || form.message.value == "") {
    alert("you error message here");
    } else {
    form.submit();
    }
}
</script>
<p>Send an e-mail with an attachment:</p>
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
   enctype="multipart/form-data" name="form1">
   <p>From name: <input type="text" name="fromname"></p>
   <p>From e-mail: <input type="text" name="fromemail"></p>
   <p>Message:<br> <textarea name="message" rows="3" cols="40"></textarea></p>
   <p>File: <input type="file" name="filename"></p>
   <p><input type="button" onclick=checkform() name="Submit" value="Submit"></p>
</form>

Open in new window

0
 
reddyfireAuthor Commented:
Sorry that didn't work for me.  Now when I click on the submit button it wont even do anything.  
<?php
 if ($_SERVER['REQUEST_METHOD']=="POST"){
 $path_info = pathinfo($_FILES['filename']['name']);
        if (!in_array(strtolower($path_info['extension']),array('pdf','zip'))) {
                die ("can not upload this type of file");
        }
 
 
   // we'll begin by assigning the To address and message subject
   $to="reddyfire@gmail.com";
 
   $subject="E-mail with attachment";
 
   // get the sender's name and email address
   // we'll just plug them a variable to be used later
   $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
 
   
   
   // generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
 
   // store the file information to variables for easier access
   $tmp_name = $_FILES['filename']['tmp_name'];
   $type = $_FILES['filename']['type'];
   $name = $_FILES['filename']['name'];
   $size = $_FILES['filename']['size'];
   $nimi =  $_REQUEST['nimi'];
   $comments = $_REQUEST['comments'];
 
 
 
   // here we'll hard code a text message
   // again, in reality, you'll normally get this from the form submission
   $message = stripslashes($_POST['comments'])."$comments";
 
   // if the upload succeded, the file will exist
   if (file_exists($tmp_name)){
 
      // check to make sure that it is an uploaded file and not a system file
      if(is_uploaded_file($tmp_name)){
 
         // 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
         fclose($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: $from\r\n" .
         "MIME-Version: 1.0\r\n" .
         "Content-Type: multipart/mixed;\r\n" .
         " 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.\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
         $message . "\n\n";
 
      // 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: {$type};\n" .
         " name=\"{$name}\"\n" .
         //"Content-Disposition: attachment;\n" .
         //" filename=\"{$fileatt_name}\"\n" .
         "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n" .
         "--{$mime_boundary}--\n";
 
      // now we just send the message
      if (@mail($to, $subject, $message, $headers))
       echo "Submission sent.";
      else
       echo "Failed to send";
 
 
	  }
	  }
?>
<script language="JavaScript">
 
function checkform() {
if (form.fromname.value == "" || form.fromemail.value == "" || form.comments.value == "") {
    alert("you error message here");
    } else 
	{
    form.submit();
    }
}
</script>
 
<p>Submit your stories:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" 
   enctype="multipart/form-data" name="form1">
   <p>From name: <input type="text" name="fromname"></p>
   <p>From e-mail: <input type="text" name="fromemail"></p>
   <p>Message:<br> <textarea name="comments" rows="3" cols="40"></textarea></p>
   <p>File: <input type="file" name="filename"></p>
   <p><input type="button" onclick=checkform() name="Submit" value="Submit"></p>
   </form>

Open in new window

0
 
agamalCommented:
you named the form to form1 ... that is why the script can't see form fields

i renamed the fields to have form1 name


check attached

<?php
 if ($_SERVER['REQUEST_METHOD']=="POST"){
 $path_info = pathinfo($_FILES['filename']['name']);
        if (!in_array(strtolower($path_info['extension']),array('pdf','zip'))) {
                die ("can not upload this type of file");
        }
 
 
   // we'll begin by assigning the To address and message subject
   $to="reddyfire@gmail.com";
 
   $subject="E-mail with attachment";
 
   // get the sender's name and email address
   // we'll just plug them a variable to be used later
   $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
 
 
 
   // generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
 
   // store the file information to variables for easier access
   $tmp_name = $_FILES['filename']['tmp_name'];
   $type = $_FILES['filename']['type'];
   $name = $_FILES['filename']['name'];
   $size = $_FILES['filename']['size'];
   $nimi =  $_REQUEST['nimi'];
   $comments = $_REQUEST['comments'];
 
 
 
   // here we'll hard code a text message
   // again, in reality, you'll normally get this from the form submission
   $message = stripslashes($_POST['comments'])."$comments";
 
   // if the upload succeded, the file will exist
   if (file_exists($tmp_name)){
 
      // check to make sure that it is an uploaded file and not a system file
      if(is_uploaded_file($tmp_name)){
 
         // 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
         fclose($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: $from\r\n" .
         "MIME-Version: 1.0\r\n" .
         "Content-Type: multipart/mixed;\r\n" .
         " 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.\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
         $message . "\n\n";
 
      // 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: {$type};\n" .
         " name=\"{$name}\"\n" .
         //"Content-Disposition: attachment;\n" .
         //" filename=\"{$fileatt_name}\"\n" .
         "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n" .
         "--{$mime_boundary}--\n";
 
      // now we just send the message
      if (@mail($to, $subject, $message, $headers))
       echo "Submission sent.";
      else
       echo "Failed to send";
 
 
	  }
	  }
?>
<script language="JavaScript">
 
function checkform() {
if (form1.fromname.value == "" || form1.fromemail.value == "" || form1.comments.value == "") {
    alert("you error message here");
    } else
	{
    form1.submit();
    }
}
</script>
 
<p>Submit your stories:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1">
   <p>From name: <input type="text" name="fromname"></p>
   <p>From e-mail: <input type="text" name="fromemail"></p>
   <p>Message:<br> <textarea name="comments" rows="3" cols="40"></textarea></p>
   <p>File: <input type="file" name="filename"></p>
   <p><input type="button" onclick=checkform() name="Submit" value="Submit"></p>
   </form>

Open in new window

0
 
reddyfireAuthor Commented:
Ok I renamed all the forms to form1 and it submits stuff now but even if I leave any of the forms blank it will still submit without giving any kind of error message.

<?php
 if ($_SERVER['REQUEST_METHOD']=="POST"){
 $path_info = pathinfo($_FILES['filename']['name']);
        if (!in_array(strtolower($path_info['extension']),array('pdf','zip'))) {
                die ("can not upload this type of file");
        }
 
 
   // we'll begin by assigning the To address and message subject
   $to="reddyfire@gmail.com";
 
   $subject="E-mail with attachment";
 
   // get the sender's name and email address
   // we'll just plug them a variable to be used later
   $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
 
   
   
   // generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
 
   // store the file information to variables for easier access
   $tmp_name = $_FILES['filename']['tmp_name'];
   $type = $_FILES['filename']['type'];
   $name = $_FILES['filename']['name'];
   $size = $_FILES['filename']['size'];
   $nimi =  $_REQUEST['nimi'];
   $comments = $_REQUEST['comments'];
 
 
 
   // here we'll hard code a text message
   // again, in reality, you'll normally get this from the form submission
   $message = stripslashes($_POST['comments'])."$comments";
 
   // if the upload succeded, the file will exist
   if (file_exists($tmp_name)){
 
      // check to make sure that it is an uploaded file and not a system file
      if(is_uploaded_file($tmp_name)){
 
         // 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
         fclose($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: $from\r\n" .
         "MIME-Version: 1.0\r\n" .
         "Content-Type: multipart/mixed;\r\n" .
         " 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.\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
         $message . "\n\n";
 
      // 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: {$type};\n" .
         " name=\"{$name}\"\n" .
         //"Content-Disposition: attachment;\n" .
         //" filename=\"{$fileatt_name}\"\n" .
         "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n" .
         "--{$mime_boundary}--\n";
 
      // now we just send the message
      if (@mail($to, $subject, $message, $headers))
       echo "Submission sent.";
      else
       echo "Failed to send";
 
 
	  }
	  }
?>
<script language="JavaScript">
 
function checkform() {
if (form1.fromname.value == "" || form1.fromemail.value == "" || form.1message.value == "") {
    alert("you error message here");
    } else 
	{
    form1.submit();
    }
}
</script>
 
<p>Submit your stories:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1">
   <p>From name: <input type="text" name="fromname"></p>
   <p>From e-mail: <input type="text" name="fromemail"></p>
   <p>Message:<br> <textarea name="message" rows="3" cols="40"></textarea></p>
   <p>File: <input type="file" name="filename"></p>
   <p><input type="button" onclick=checkform() name="Submit" value="Submit"></p>
   </form>

Open in new window

0
 
agamalCommented:
on line 97 your last condition is wrong
if (form1.fromname.value == "" || form1.fromemail.value == "" || form.1message.value == "") {

it is
form.1message.value
but it must be
form1.message.value
0
 
reddyfireAuthor Commented:
Thank you this really helped.
0
 
reddyfireAuthor Commented:
Thanks you for your help.  I was finally able to get it working.  
0
 
teampanduCommented:
hello, "agamal"

How to separate HTML and PHP code as two files and adding extra fields like phone, address etc.

Thanks
Pandu
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now