Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How can I make this script not require a file to be uploaded for the contact to be made?

Posted on 2011-09-29
4
Medium Priority
?
302 Views
Last Modified: 2012-08-13
I found some code free of charge online and have been tweaking it for the past few hours to work for my needs. This is a contact form on a site for a t-shirt printer. We'd like to allow a file to be uploaded as an example if the client wants, but not required to do so.

Everything seems functional, except I do not want the file upload field to be required. Even though it isn't necessarily currently required, there seems to be an issue with the fact that the script verifies that the file potentially being uploaded has one of the allowed file extension types (.jpg, .gif, etc). How can I keep the file extension check present but make it not check if no file is being uploaded?

Sorry if this is unclear. Doing my best to explain haha.
<?php 
// Pear library includes
// You should have the pear lib installed
include_once('PEAR/Mail.php');
include_once('PEAR/Mail/mime.php');

//Settings 
$max_allowed_file_size = 20480; // size in KB 
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp", "png");
$upload_folder = './uploads/'; //<-- this folder must be writeable by the script
$your_email = 'editedoutforprivacy';//<<--  update this to your email address

$errors ='';

if(isset($_POST['submit']))
{
	//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;
	
	///------------Do Validations-------------
	if(empty($_POST['name'])||empty($_POST['email']))
	{
		$errors .= "\n Name and Email are required fields. ";	
	}
	if(IsInjected($visitor_email))
	{
		$errors .= "\n Bad email value!";
	}
	
	if($size_of_uploaded_file > $max_allowed_file_size ) 
	{
		$errors .= "\n Size of file should be less than $max_allowed_file_size";
	}
	
	//------ Validate the file extension -----
	$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';
		    }
		}
		
		//send the email
		$shirttype = $_POST['shirttype'];
		$quantity = $_POST['quantity'];
		$instructions = $_POST['instructions'];
		$colors = $_POST['colors'];
		$band = $_POST['band'];
		$phone = $_POST['phone'];
		$name = $_POST['name'];
		$email = $_POST['email'];
		$to = $your_email;
		$subject="Price Quote Request";
		$from = $your_email;
		$text = "$name has requested a printing quote. Information below:
		
		\nContact Name: $name
		\nBand Name: $band
		\nEmail Address: $email
		\nPhone Number: $phone
		\nType of Shirt: $shirttype
		\nQuantity: $quantity
		\nNumber of Colors: $colors
		
		\nSpecial Instructions:
		\n$instructions";
		
		$message = new Mail_mime(); 
		$message->setTXTBody($text); 
		$message->addAttachment($path_of_uploaded_file);
		$body = $message->get();
		$extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$email);
		$headers = $message->headers($extraheaders);
		$mail = Mail::factory("mail");
		$mail->send($to, $headers, $body);
		//redirect to 'thank-you page
		header('Location: thank-you.html');
	}
}
///////////////////////////Functions/////////////////
// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
	<title>File upload form</title>
<!-- define some style elements-->
<style>
label,a, body 
{
	font-family : Arial, Helvetica, sans-serif;
	font-size : 12px; 
}

</style>	
<!-- a helper script for vaidating the form-->
<script language="JavaScript" src="scripts/gen_validatorv31.js" type="text/javascript"></script>	
</head>

<body>
<?php
if(!empty($errors))
{
	echo nl2br($errors);
}
?>
<form method="POST" name="email_form_with_php" 
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data"> 
<p>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="25">
  <tr>
    <td>PICK A SHIRT<br />
    <select name="shirttype" id="select">
      <option value="Standard">Standard</option>
<option value="Soft fit shirt">Soft fit shirt</option>
<option value="American Apparel">American Apparel</option>
    </select></td>
    <td>QUANTITY<br />
    <input name="quantity" type="text"></td>
  </tr>
  <tr>
  <td>NUMBER OF COLORS<br />
    <select name="colors" id="select">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="CMKY">CMKY</option>
    </select></td>
    <td colspan="2">ATTACH ARTWORK<br />
    <div style="font-size: 12px;">20MB Limit - Low-resolution artwork only.</div>
    <input type="file" name="uploaded_file"></td>
  </tr>
  <tr>
    <td colspan="2">SPECIAL INSTRUCTIONS<br />
    <textarea name="instructions" cols="" rows=""></textarea></td>
  </tr>
  <tr>
    <td>BAND NAME<br />
    <input name="band" type="text"></td>
    <td>CONTACT NAME<br />
    <input name="name" type="text"></td>
  </tr>
  <tr>
    <td>EMAIL ADDRESS<br />
    <input name="email" type="text"></td>
    <td>PHONE NUMBER<br />
    <input name="phone" type="text"></td>
  </tr>
  <tr>
    <td colspan="2"><input type="submit" value="Submit" name='submit'></td>
  </tr>
  <tr>
    <td colspan="2">* Indicates a required field.</td>
  </tr>
</table>
</form>
<script language="JavaScript">
// Code for validating the form
// Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
// for details
var frmvalidator  = new Validator("email_form_with_php");
frmvalidator.addValidation("name","req","Please provide your name"); 
frmvalidator.addValidation("quantity","req","Please provide number of shirts to be printed"); 
frmvalidator.addValidation("band","req","Please provide your band name"); 
frmvalidator.addValidation("colors","req","Please provide the number of colors"); 
frmvalidator.addValidation("shirttype","req","Please provide the type of shirts"); 
frmvalidator.addValidation("email","email","Please enter a valid email address"); 
</script>

</body>
</html>

Open in new window

0
Comment
Question by:TJOP
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 15

Expert Comment

by:ncoo
ID: 36841796
You need to just put an if round each file check, see the following
<?php 
// Pear library includes
// You should have the pear lib installed
include_once('PEAR/Mail.php');
include_once('PEAR/Mail/mime.php');

//Settings 
$max_allowed_file_size = 20480; // size in KB 
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp", "png");
$upload_folder = './uploads/'; //<-- this folder must be writeable by the script
$your_email = 'editedoutforprivacy';//<<--  update this to your email address

$errors ='';

if(isset($_POST['submit']))
{
	//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;
	
	///------------Do Validations-------------
	if(empty($_POST['name'])||empty($_POST['email']))
	{
		$errors .= "\n Name and Email are required fields. ";	
	}
	if(IsInjected($visitor_email))
	{
		$errors .= "\n Bad email value!";
	}
	
	//Are they uploading a file
	if ($name_of_uploaded_file) {
		if($size_of_uploaded_file > $max_allowed_file_size ) 
		{
			$errors .= "\n Size of file should be less than $max_allowed_file_size";
		}

		//------ Validate the file extension -----
		$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))
	{
		//Are they uploading a file
		if ($name_of_uploaded_file) {
			//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';
			    }
			}
		}
		
		//send the email
		$shirttype = $_POST['shirttype'];
		$quantity = $_POST['quantity'];
		$instructions = $_POST['instructions'];
		$colors = $_POST['colors'];
		$band = $_POST['band'];
		$phone = $_POST['phone'];
		$name = $_POST['name'];
		$email = $_POST['email'];
		$to = $your_email;
		$subject="Price Quote Request";
		$from = $your_email;
		$text = "$name has requested a printing quote. Information below:
		
		\nContact Name: $name
		\nBand Name: $band
		\nEmail Address: $email
		\nPhone Number: $phone
		\nType of Shirt: $shirttype
		\nQuantity: $quantity
		\nNumber of Colors: $colors
		
		\nSpecial Instructions:
		\n$instructions";
		
		$message = new Mail_mime(); 
		$message->setTXTBody($text); 
		//Are they uploading a file
		if ($name_of_uploaded_file) {
			$message->addAttachment($path_of_uploaded_file);
		}
		$body = $message->get();
		$extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$email);
		$headers = $message->headers($extraheaders);
		$mail = Mail::factory("mail");
		$mail->send($to, $headers, $body);
		//redirect to 'thank-you page
		header('Location: thank-you.html');
	}
}
///////////////////////////Functions/////////////////
// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
	<title>File upload form</title>
<!-- define some style elements-->
<style>
label,a, body 
{
	font-family : Arial, Helvetica, sans-serif;
	font-size : 12px; 
}

</style>	
<!-- a helper script for vaidating the form-->
<script language="JavaScript" src="scripts/gen_validatorv31.js" type="text/javascript"></script>	
</head>

<body>
<?php
if(!empty($errors))
{
	echo nl2br($errors);
}
?>
<form method="POST" name="email_form_with_php" 
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data"> 
<p>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="25">
  <tr>
    <td>PICK A SHIRT<br />
    <select name="shirttype" id="select">
      <option value="Standard">Standard</option>
<option value="Soft fit shirt">Soft fit shirt</option>
<option value="American Apparel">American Apparel</option>
    </select></td>
    <td>QUANTITY<br />
    <input name="quantity" type="text"></td>
  </tr>
  <tr>
  <td>NUMBER OF COLORS<br />
    <select name="colors" id="select">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="CMKY">CMKY</option>
    </select></td>
    <td colspan="2">ATTACH ARTWORK<br />
    <div style="font-size: 12px;">20MB Limit - Low-resolution artwork only.</div>
    <input type="file" name="uploaded_file"></td>
  </tr>
  <tr>
    <td colspan="2">SPECIAL INSTRUCTIONS<br />
    <textarea name="instructions" cols="" rows=""></textarea></td>
  </tr>
  <tr>
    <td>BAND NAME<br />
    <input name="band" type="text"></td>
    <td>CONTACT NAME<br />
    <input name="name" type="text"></td>
  </tr>
  <tr>
    <td>EMAIL ADDRESS<br />
    <input name="email" type="text"></td>
    <td>PHONE NUMBER<br />
    <input name="phone" type="text"></td>
  </tr>
  <tr>
    <td colspan="2"><input type="submit" value="Submit" name='submit'></td>
  </tr>
  <tr>
    <td colspan="2">* Indicates a required field.</td>
  </tr>
</table>
</form>
<script language="JavaScript">
// Code for validating the form
// Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
// for details
var frmvalidator  = new Validator("email_form_with_php");
frmvalidator.addValidation("name","req","Please provide your name"); 
frmvalidator.addValidation("quantity","req","Please provide number of shirts to be printed"); 
frmvalidator.addValidation("band","req","Please provide your band name"); 
frmvalidator.addValidation("colors","req","Please provide the number of colors"); 
frmvalidator.addValidation("shirttype","req","Please provide the type of shirts"); 
frmvalidator.addValidation("email","email","Please enter a valid email address"); 
</script>

</body>
</html>

Open in new window

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 36841815
Try this:

<?php 
// Pear library includes
// You should have the pear lib installed
include_once('PEAR/Mail.php');
include_once('PEAR/Mail/mime.php');

//Settings 
$max_allowed_file_size = 20480; // size in KB 
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp", "png");
$upload_folder = './uploads/'; //<-- this folder must be writeable by the script
$your_email = 'editedoutforprivacy';//<<--  update this to your email address

$errors ='';

if(isset($_POST['submit']))
{
	//Get the uploaded file information
	$name_of_uploaded_file =  basename($_FILES['uploaded_file']['name']);
	if ($name_of_uploaded_file != '')
	{
		//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;
		
		///------------Do Validations-------------
		if(empty($_POST['name'])||empty($_POST['email']))
		{
			$errors .= "\n Name and Email are required fields. ";	
		}
		if(IsInjected($visitor_email))
		{
			$errors .= "\n Bad email value!";
		}
		
		if($size_of_uploaded_file > $max_allowed_file_size ) 
		{
			$errors .= "\n Size of file should be less than $max_allowed_file_size";
		}
		
		//------ Validate the file extension -----
		$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))
	{
		if ($name_of_uploaded_file != '')
		{
			//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';
			    }
			}
		}
		
		//send the email
		$shirttype = $_POST['shirttype'];
		$quantity = $_POST['quantity'];
		$instructions = $_POST['instructions'];
		$colors = $_POST['colors'];
		$band = $_POST['band'];
		$phone = $_POST['phone'];
		$name = $_POST['name'];
		$email = $_POST['email'];
		$to = $your_email;
		$subject="Price Quote Request";
		$from = $your_email;
		$text = "$name has requested a printing quote. Information below:
		
		\nContact Name: $name
		\nBand Name: $band
		\nEmail Address: $email
		\nPhone Number: $phone
		\nType of Shirt: $shirttype
		\nQuantity: $quantity
		\nNumber of Colors: $colors
		
		\nSpecial Instructions:
		\n$instructions";
		
		$message = new Mail_mime(); 
		$message->setTXTBody($text); 
		
		if ($name_of_uploaded_file != '')
		{
			$message->addAttachment($path_of_uploaded_file);
		}
		$body = $message->get();
		$extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$email);
		$headers = $message->headers($extraheaders);
		$mail = Mail::factory("mail");
		$mail->send($to, $headers, $body);
		//redirect to 'thank-you page
		header('Location: thank-you.html');
	}
}
///////////////////////////Functions/////////////////
// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
	<title>File upload form</title>
<!-- define some style elements-->
<style>
label,a, body 
{
	font-family : Arial, Helvetica, sans-serif;
	font-size : 12px; 
}

</style>	
<!-- a helper script for vaidating the form-->
<script language="JavaScript" src="scripts/gen_validatorv31.js" type="text/javascript"></script>	
</head>

<body>
<?php
if(!empty($errors))
{
	echo nl2br($errors);
}
?>
<form method="POST" name="email_form_with_php" 
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data"> 
<p>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="25">
  <tr>
    <td>PICK A SHIRT<br />
    <select name="shirttype" id="select">
      <option value="Standard">Standard</option>
<option value="Soft fit shirt">Soft fit shirt</option>
<option value="American Apparel">American Apparel</option>
    </select></td>
    <td>QUANTITY<br />
    <input name="quantity" type="text"></td>
  </tr>
  <tr>
  <td>NUMBER OF COLORS<br />
    <select name="colors" id="select">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="CMKY">CMKY</option>
    </select></td>
    <td colspan="2">ATTACH ARTWORK<br />
    <div style="font-size: 12px;">20MB Limit - Low-resolution artwork only.</div>
    <input type="file" name="uploaded_file"></td>
  </tr>
  <tr>
    <td colspan="2">SPECIAL INSTRUCTIONS<br />
    <textarea name="instructions" cols="" rows=""></textarea></td>
  </tr>
  <tr>
    <td>BAND NAME<br />
    <input name="band" type="text"></td>
    <td>CONTACT NAME<br />
    <input name="name" type="text"></td>
  </tr>
  <tr>
    <td>EMAIL ADDRESS<br />
    <input name="email" type="text"></td>
    <td>PHONE NUMBER<br />
    <input name="phone" type="text"></td>
  </tr>
  <tr>
    <td colspan="2"><input type="submit" value="Submit" name='submit'></td>
  </tr>
  <tr>
    <td colspan="2">* Indicates a required field.</td>
  </tr>
</table>
</form>
<script language="JavaScript">
// Code for validating the form
// Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
// for details
var frmvalidator  = new Validator("email_form_with_php");
frmvalidator.addValidation("name","req","Please provide your name"); 
frmvalidator.addValidation("quantity","req","Please provide number of shirts to be printed"); 
frmvalidator.addValidation("band","req","Please provide your band name"); 
frmvalidator.addValidation("colors","req","Please provide the number of colors"); 
frmvalidator.addValidation("shirttype","req","Please provide the type of shirts"); 
frmvalidator.addValidation("email","email","Please enter a valid email address"); 
</script>

</body>
</html>

Open in new window

0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 2000 total points
ID: 36844592
Corrected version:

<?php 
// Pear library includes
// You should have the pear lib installed
include_once('PEAR/Mail.php');
include_once('PEAR/Mail/mime.php');

//Settings 
$max_allowed_file_size = 20480; // size in KB 
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp", "png");
$upload_folder = './uploads/'; //<-- this folder must be writeable by the script
$your_email = 'editedoutforprivacy';//<<--  update this to your email address

$errors ='';

if(isset($_POST['submit']))
{

	///------------Do Validations-------------
	if(empty($_POST['name'])||empty($_POST['email']))
	{
		$errors .= "\n Name and Email are required fields. ";	
	}
	if(IsInjected($visitor_email))
	{
		$errors .= "\n Bad email value!";
	}
		
	//Get the uploaded file information
	$name_of_uploaded_file =  basename($_FILES['uploaded_file']['name']);
	if ($name_of_uploaded_file != '')
	{
		//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;

		if($size_of_uploaded_file > $max_allowed_file_size ) 
		{
			$errors .= "\n Size of file should be less than $max_allowed_file_size";
		}
		
		//------ Validate the file extension -----
		$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))
	{
		if ($name_of_uploaded_file != '')
		{
			//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';
			    }
			}
		}
		
		//send the email
		$shirttype = $_POST['shirttype'];
		$quantity = $_POST['quantity'];
		$instructions = $_POST['instructions'];
		$colors = $_POST['colors'];
		$band = $_POST['band'];
		$phone = $_POST['phone'];
		$name = $_POST['name'];
		$email = $_POST['email'];
		$to = $your_email;
		$subject="Price Quote Request";
		$from = $your_email;
		$text = "$name has requested a printing quote. Information below:
		
		\nContact Name: $name
		\nBand Name: $band
		\nEmail Address: $email
		\nPhone Number: $phone
		\nType of Shirt: $shirttype
		\nQuantity: $quantity
		\nNumber of Colors: $colors
		
		\nSpecial Instructions:
		\n$instructions";
		
		$message = new Mail_mime(); 
		$message->setTXTBody($text); 
		
		if ($name_of_uploaded_file != '')
		{
			$message->addAttachment($path_of_uploaded_file);
		}
		$body = $message->get();
		$extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$email);
		$headers = $message->headers($extraheaders);
		$mail = Mail::factory("mail");
		$mail->send($to, $headers, $body);
		//redirect to 'thank-you page
		header('Location: thank-you.html');
	}
}
///////////////////////////Functions/////////////////
// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
	<title>File upload form</title>
<!-- define some style elements-->
<style>
label,a, body 
{
	font-family : Arial, Helvetica, sans-serif;
	font-size : 12px; 
}

</style>	
<!-- a helper script for vaidating the form-->
<script language="JavaScript" src="scripts/gen_validatorv31.js" type="text/javascript"></script>	
</head>

<body>
<?php
if(!empty($errors))
{
	echo nl2br($errors);
}
?>
<form method="POST" name="email_form_with_php" 
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data"> 
<p>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="25">
  <tr>
    <td>PICK A SHIRT<br />
    <select name="shirttype" id="select">
      <option value="Standard">Standard</option>
<option value="Soft fit shirt">Soft fit shirt</option>
<option value="American Apparel">American Apparel</option>
    </select></td>
    <td>QUANTITY<br />
    <input name="quantity" type="text"></td>
  </tr>
  <tr>
  <td>NUMBER OF COLORS<br />
    <select name="colors" id="select">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="CMKY">CMKY</option>
    </select></td>
    <td colspan="2">ATTACH ARTWORK<br />
    <div style="font-size: 12px;">20MB Limit - Low-resolution artwork only.</div>
    <input type="file" name="uploaded_file"></td>
  </tr>
  <tr>
    <td colspan="2">SPECIAL INSTRUCTIONS<br />
    <textarea name="instructions" cols="" rows=""></textarea></td>
  </tr>
  <tr>
    <td>BAND NAME<br />
    <input name="band" type="text"></td>
    <td>CONTACT NAME<br />
    <input name="name" type="text"></td>
  </tr>
  <tr>
    <td>EMAIL ADDRESS<br />
    <input name="email" type="text"></td>
    <td>PHONE NUMBER<br />
    <input name="phone" type="text"></td>
  </tr>
  <tr>
    <td colspan="2"><input type="submit" value="Submit" name='submit'></td>
  </tr>
  <tr>
    <td colspan="2">* Indicates a required field.</td>
  </tr>
</table>
</form>
<script language="JavaScript">
// Code for validating the form
// Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
// for details
var frmvalidator  = new Validator("email_form_with_php");
frmvalidator.addValidation("name","req","Please provide your name"); 
frmvalidator.addValidation("quantity","req","Please provide number of shirts to be printed"); 
frmvalidator.addValidation("band","req","Please provide your band name"); 
frmvalidator.addValidation("colors","req","Please provide the number of colors"); 
frmvalidator.addValidation("shirttype","req","Please provide the type of shirts"); 
frmvalidator.addValidation("email","email","Please enter a valid email address"); 
</script>

</body>
</html>

Open in new window

0
 

Author Closing Comment

by:TJOP
ID: 36890054
THANK YOU!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question