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

Attach file to PHP web form and solve error

Okay so I am working on an expense form to make things easier for us in the office. I reused some code I had for a different email form and have a few issues with it.
First issue is when the page is loaded it submits an email, the email has no data other than the pre-formatted stuff. Second it continues on to the next page before the submit button is hit (meta refresh, set to 90 to give time to fill it out.)

I also need to be able to attach a file to the email. I have searched around and without totally rewriting my code I don't know how to do it, even with rewriting I'm not sure how I would integrate my code.
<?php 
/* |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||Variables|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
	$today = date("m.d.Y"); // sets $today to todays date from client machine
	$year = $_POST['year']; //Sets Client Name
	$month = $_POST['month']; //Sets Client Name
	$day = $_POST['day']; //Sets Client Name
	$employee = $_POST['employee']; //Sets Client Name
	$client = $_POST['client']; //Sets Client Name
	$vendor = $_POST['vendor']; //Sets Client Name
	$desc = $_POST['desc']; //Sets Client Name
	$amount = $_POST['amount']; //Sets Client Name
/* ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||Vailidation||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
 switch (@$_GET['do'])
  {
    case "send":
      if (!preg_match("/\S+/",$client))
      {
        unset($_GET['do']);
        $message = "Client Name required. Please try again.";
         break;
      }
/* |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||Set message to null if no error|||||||||||||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
       $message = '';
       unset($_GET['do']);
     break;
 default: break;
   }
/* ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////phpmailer sends file as attachment/////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/

	require_once('class.phpmailer.php');

	$mail             = new PHPMailer(); // defaults to using php "mail()"

	$body             = file_get_contents('contents.html');
	$body             = eregi_replace("[\]",'',$body);

	$mail->AddReplyTo("Expenses@Graycomputer.net","Tender Care Inc.");

	$mail->SetFrom('Expenses@Graycomputer.net', 'GCRi Expenses');

	$mail->AddReplyTo("Expenses@Graycomputer.net","GCRi Expenses");

	$address = "shawn@graycomputer.net";
	$mail->AddAddress($address, "GCRi Expense Reports");
	
	$mail->Subject    = "$year, $month, $day, $employee, $client, $vendor, $desc, $amount";

	$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // This is the message body alt.

	$mail->MsgHTML($body);

	if(!$mail->Send()) {
	  echo "Mailer Error: " . $mail->ErrorInfo;
	} else {
	  echo '<META HTTP-EQUIV="refresh" content="90;URL=index.htm">';
	}
?>

<!-- ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||HTML CODE||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>GCRi | Expense Reporting</title>
<link rel="stylesheet" href="rtbstyle.css" type="text/css" >
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >

<!-- JS to limit number of characters in text fields -->
<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}
</script>

<script type="text/javascript">
/***********************************************
* Disable "Enter" key in Form 
***********************************************/
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}
</script>

</head>
<body>
<center>
<form action="expense.php?do=send" method="POST">
<?php
	if ($message)
	echo "$message"; 
?>
		<!-- Coding for Months -->
   <font color="red">*</font> Month: <select id="select01" name="month"
   						onkeypress="return handleEnter(this, event);">
			<option value="<?php echo @$month ?>">Select...</option>
			<option value="01">January</option>
			<option value="02">Febuary</option>
			<option value="03">March</option>
			<option value="04">April</option>
			<option value="05">May</option>
			<option value="06">June</option>
			<option value="07">July</option>
			<option value="08">August</option>
			<option value="09">September</option>
			<option value="10">October</option>
			<option value="11">November</option>
			<option value="12">December</option>
		</select> <br/>
		<!-- Coding for Days -->
		   <font color="red">*</font> Day: <select id="select02" name="day"
   						onkeypress="return handleEnter(this, event);">
			<option value="<?php echo @$day ?>">Select...</option>
			<option value="01">1st</option>
			<option value="02">2nd</option>
			<option value="03">3rd</option>
			<option value="04">4th</option>
			<option value="05">5th</option>
			<option value="06">6th</option>
			<option value="07">7th</option>
			<option value="08">8th</option>
			<option value="09">9th</option>
			<option value="10">10th</option>
			<option value="11">11th</option>
			<option value="12">12th</option>
			<option value="13">13th</option>
			<option value="14">14th</option>
			<option value="15">15th</option>
			<option value="16">16th</option>
			<option value="17">17th</option>
			<option value="18">18th</option>
			<option value="19">19th</option>
			<option value="20">20th</option>
			<option value="21">21st</option>
			<option value="22">22nd</option>
			<option value="23">23rd</option>
			<option value="24">24th</option>
			<option value="25">25th</option>
			<option value="26">26th</option>
			<option value="27">27th</option>
			<option value="28">28th</option>
			<option value="29">29th</option>
			<option value="30">30th</option>
			<option value="31">31st</option>
		</select> <br/>
		<!-- Coding for Years -->
		   <font color="red">*</font> Year: <select id="select03" name="year"
   						onkeypress="return handleEnter(this, event);">
			<option value="<?php echo @$year ?>">Select...</option>
			<option value="2010">2010</option>
			<option value="2011">2011</option>
			<option value="2012">2012</option>
			</select> <br/>
		<!-- Coding for Employees -->
   <font color="red">*</font> Employee: <select id="select04" name="employee"
   						onkeypress="return handleEnter(this, event);">
			<option value="<?php echo @$employee ?>">Select...</option>
			<option value="Rodney">Rodney</option>
			<option value="Shawn">Shawn</option>
			<option value="Josh">Josh</option>
		</select> <br/>
		<!-- Coding for Client Name -->
   <font color="red">*</font> Client Name: <input type="text" name="client" size="20" value="<?php echo @$client ?>"
   						onkeypress="return handleEnter(this, event);"
   						onKeyDown="limitText(this.form.client,this.form.countdown1,45);" 
						onKeyUp="limitText(this.form.client,this.form.countdown1,45);"><br/>
		<!-- Coding for Vendor Name -->
	<font color="red">*</font> Vendor: <input type="text" name="vendor" size="20" value="<?php echo @$vendor ?>"
   						onkeypress="return handleEnter(this, event);"
   						onKeyDown="limitText(this.form.vendor,this.form.countdown1,45);" 
						onKeyUp="limitText(this.form.vendor,this.form.countdown1,45);"><br/>
		<!-- Coding for Purchase Description -->
	<font color="red">*</font> Purchase Description:<br/><textarea rows="5" cols="40" name="desc" 
   						onkeypress="return handleEnter(this, event);"
   						onKeyDown="limitText(this.form.desc,this.form.countdown18,240);" 
						onKeyUp="limitText(this.form.desc,this.form.countdown18,240);"
						><?php echo @$desc ?></textarea><br/>
		<!-- Coding for Dollar amount -->
	<font color="red">*</font> Amount: <input type="text" name="amount" size="20" value="<?php echo @$amount ?>"
   						onkeypress="return handleEnter(this, event);"
   						onKeyDown="limitText(this.form.amount,this.form.countdown1,45);" 
						onKeyUp="limitText(this.form.amount,this.form.countdown1,45);"><br/>
<input type="submit" name="submit" value="Click Here to Submit" style="height: 75px; width: 400px; font-size: 24px;">
   </form>
   </center>
</body>
</html>

Open in new window

0
rodneygray
Asked:
rodneygray
  • 5
  • 2
1 Solution
 
rodneygrayAuthor Commented:
Major code change.
I decided to just go with someone else's code.

I am having an issue with the code not actually sending the email now.
<?php
/* Mailer with Attachments */

$action = $_REQUEST['action'];
global $action;

function showForm() {
?>

  <form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">
  <input type="hidden" name="action" value="send" />
  <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
    <p>
	Month: <select id="select01" name="emonth">
			<option value="NA">Select...</option>
			<option value="01">January</option>
			<option value="02">Febuary</option>
			<option value="03">March</option>
			<option value="04">April</option>
			<option value="05">May</option>
			<option value="06">June</option>
			<option value="07">July</option>
			<option value="08">August</option>
			<option value="09">September</option>
			<option value="10">October</option>
			<option value="11">November</option>
			<option value="12">December</option>
		</select> <br/>
		Day: <select id="select02" name="eday">
			<option value="NA">Select...</option>
			<option value="01">1st</option>
			<option value="02">2nd</option>
			<option value="03">3rd</option>
			<option value="04">4th</option>
			<option value="05">5th</option>
			<option value="06">6th</option>
			<option value="07">7th</option>
			<option value="08">8th</option>
			<option value="09">9th</option>
			<option value="10">10th</option>
			<option value="11">11th</option>
			<option value="12">12th</option>
			<option value="13">13th</option>
			<option value="14">14th</option>
			<option value="15">15th</option>
			<option value="16">16th</option>
			<option value="17">17th</option>
			<option value="18">18th</option>
			<option value="19">19th</option>
			<option value="20">20th</option>
			<option value="21">21st</option>
			<option value="22">22nd</option>
			<option value="23">23rd</option>
			<option value="24">24th</option>
			<option value="25">25th</option>
			<option value="26">26th</option>
			<option value="27">27th</option>
			<option value="28">28th</option>
			<option value="29">29th</option>
			<option value="30">30th</option>
			<option value="31">31st</option>
		</select> <br/>
		Year: <select id="select03" name="eyear">
			<option value="NA">Select...</option>
			<option value="2010">2010</option>
			<option value="2011">2011</option>
			<option value="2012">2012</option>
			</select> <br/>
		Employee: <select id="select04" name="employee">
			<option value="NA">Select...</option>
			<option value="Rodney">Rodney</option>
			<option value="Shawn">Shawn</option>
			<option value="Josh">Josh</option>
		</select> <br/>
		Client Name: <input type="text" name="client" size="20" value=""><br/>
		Vendor: <input type="text" name="evendor" size="20" value=""><br/>
		Purchase Description:<br/><textarea rows="5" cols="40" name="edesc"></textarea><br/>
		Amount: <input type="text" name="eamount" size="20" value=""><br/>

    Attachment: <input type="file" name="attachment" size="50" /><br/>
    <input type="submit" value="Send Email" /></p>
  
<?php
}

function sendMail() {
  if (!isset ($_POST['employee'])) { //Oops, forgot to put the employee!
    die ("<p>Oops!  You forgot to fill out the employee making the expense! Click on the back arrow to go back</p>");
  }
  else {
    $to_name = stripslashes($_POST['shawn@graycomputer.net']);
    $from_name = stripslashes($_POST['GCRi Expense Mailer']);
    $subject = "$eyear, $emonth, $eday, $employee, $client, $evendor, $edesc, $eamount";
    $body = stripslashes($_POST['body']);
    $to_email = $_POST['shawn@graycomputer.net'];
    $from_email = $_POST['shawn@graycomputer.net'];
    $attachment = $_FILES['attachment']['tmp_name'];
    $attachment_name = $_FILES['attachment']['name']; 
    if (is_uploaded_file($attachment)) { //Do we have a file uploaded?
      $fp = fopen($attachment, "rb"); //Open it
      $data = fread($fp, filesize($attachment)); //Read it
      $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
        fclose($fp);
    }
    //Let's start our headers
    $headers = "From: $from_name<" . $_POST['from_email'] . ">\n";
    $headers .= "Reply-To: <" . $_POST['from_email'] . ">\n"; 
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n"; 
    $headers .= "X-Sender: $from_name<" . $_POST['from_email'] . ">\n";
    $headers .= "X-Mailer: PHP4\n";
    $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
    $headers .= "Return-Path: <" . $_POST['from_email'] . ">\n"; 
    $headers .= "This is a multi-part message in MIME format.\n";
    $headers .= "------=MIME_BOUNDRY_main_message \n"; 
    $headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n"; 
    
    $message = "------=MIME_BOUNDRY_message_parts\n";
    $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; 
    $message .= "Content-Transfer-Encoding: quoted-printable\n"; 
    $message .= "\n"; 
    /* Add our message, in this case it's plain text.  You could also add HTML by changing the Content-Type to text/html */
    $message .= "$body\n";
    $message .= "\n"; 
    $message .= "------=MIME_BOUNDRY_message_parts--\n"; 
    $message .= "\n"; 
    $message .= "------=MIME_BOUNDRY_main_message\n"; 
    $message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";
    $message .= "Content-Transfer-Encoding: base64\n";
    $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
    $message .= $data; //The base64 encoded message
    $message .= "\n"; 
    $message .= "------=MIME_BOUNDRY_main_message--\n"; 

    // send the message
    mail("$to_name<$to_email>", $subject, $message, $headers); 
    print "Mail sent.  Thank you for using the GCRi Expense Mailer.";
  }
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <style="css" type="text/css">
      <!--
      body {
        margin: 0px;
        font-family: Verdana, Arial, Helvetica, sans-serif;
        font-size: 12px;
      }
      a {color: #0000ff}
      -->
    </style>
  </head>
  <body>

<?php
switch ($action) {
  case "send":
    sendMail();
    showForm();
    break;
  default:
    showForm();
}
?>

  </body>
</html>

Open in new window

0
 
rodneygrayAuthor Commented:
Another update, I am able to get it to send but require the person fill the form to put in the addresses. I want to make them static and I also want to put all info into the subject line.
<?php
/* Mailer with Attachments */

$action = $_REQUEST['action'];
global $action;

function showForm() {
?>

  <form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">
  <input type="hidden" name="action" value="send" />
  <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
  
    <p>
    Recipient Name: <input name="to_name" size="50" /><br/>
    Recipient Email: <input name="to_email" size="50" /><br/>
    From Name:  <input name="from_name" size="50" /><br/>
    From Email:  <input name="from_email" size="50" /><br/>
	Month: <select id="emonth" name="emonth">
			<option value="NA">Select...</option>
			<option value="01">January</option>
			<option value="02">Febuary</option>
			<option value="03">March</option>
			<option value="04">April</option>
			<option value="05">May</option>
			<option value="06">June</option>
			<option value="07">July</option>
			<option value="08">August</option>
			<option value="09">September</option>
			<option value="10">October</option>
			<option value="11">November</option>
			<option value="12">December</option>
		</select> <br/>
		Day: <select id="eday" name="eday">
			<option value="NA">Select...</option>
			<option value="01">1st</option>
			<option value="02">2nd</option>
			<option value="03">3rd</option>
			<option value="04">4th</option>
			<option value="05">5th</option>
			<option value="06">6th</option>
			<option value="07">7th</option>
			<option value="08">8th</option>
			<option value="09">9th</option>
			<option value="10">10th</option>
			<option value="11">11th</option>
			<option value="12">12th</option>
			<option value="13">13th</option>
			<option value="14">14th</option>
			<option value="15">15th</option>
			<option value="16">16th</option>
			<option value="17">17th</option>
			<option value="18">18th</option>
			<option value="19">19th</option>
			<option value="20">20th</option>
			<option value="21">21st</option>
			<option value="22">22nd</option>
			<option value="23">23rd</option>
			<option value="24">24th</option>
			<option value="25">25th</option>
			<option value="26">26th</option>
			<option value="27">27th</option>
			<option value="28">28th</option>
			<option value="29">29th</option>
			<option value="30">30th</option>
			<option value="31">31st</option>
		</select> <br/>
		Year: <select id="eyear" name="eyear">
			<option value="NA">Select...</option>
			<option value="2010">2010</option>
			<option value="2011">2011</option>
			<option value="2012">2012</option>
			</select> <br/>
		Employee: <select id="employee" name="employee">
			<option value="NA">Select...</option>
			<option value="Rodney">Rodney</option>
			<option value="Shawn">Shawn</option>
			<option value="Josh">Josh</option>
		</select> <br/>
		Client Name: <input type="text" name="client" size="20" value=""><br/>
		Vendor: <input type="text" name="evendor" size="20" value=""><br/>
		Purchase Description:<br/><textarea rows="5" cols="40" name="edesc"></textarea><br/>
		Amount: <input type="text" name="eamount" size="20" value=""><br/>

    Attachment: <input type="file" name="attachment" size="50" /><br/>
    <input type="submit" value="Send Email" /></p>
  
<?php
}

function sendMail() {
  if (!isset ($_POST['employee'])) { //Oops, forgot to put the employee!
    die ("<p>Oops!  You forgot to fill out the employee making the expense! Click on the back arrow to go back</p>");
  }
  else {
    $to_name = stripslashes($_POST['to_name']);
    $from_name = stripslashes($_POST['from_name']);
    $subject = "$eyear, $emonth, $eday, $employee, $client, $evendor, $edesc, $eamount";
    $body = stripslashes($_POST['body']);
    $to_email = $_POST['to_email'];
    $attachment = $_FILES['attachment']['tmp_name'];
    $attachment_name = $_FILES['attachment']['name']; 
    if (is_uploaded_file($attachment)) { //Do we have a file uploaded?
      $fp = fopen($attachment, "rb"); //Open it
      $data = fread($fp, filesize($attachment)); //Read it
      $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
        fclose($fp);
    }
    //Let's start our headers
    $headers = "From: $from_name<" . $_POST['from_email'] . ">\n";
    $headers .= "Reply-To: <" . $_POST['from_email'] . ">\n"; 
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n"; 
    $headers .= "X-Sender: $from_name<" . $_POST['from_email'] . ">\n";
    $headers .= "X-Mailer: PHP4\n";
    $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
    $headers .= "Return-Path: <" . $_POST['from_email'] . ">\n"; 
    $headers .= "This is a multi-part message in MIME format.\n";
    $headers .= "------=MIME_BOUNDRY_main_message \n"; 
    $headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n"; 
    
    $message = "------=MIME_BOUNDRY_message_parts\n";
    $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; 
    $message .= "Content-Transfer-Encoding: quoted-printable\n"; 
    $message .= "\n"; 
    /* Add our message, in this case it's plain text.  You could also add HTML by changing the Content-Type to text/html */
    $message .= "$body\n";
    $message .= "\n"; 
    $message .= "------=MIME_BOUNDRY_message_parts--\n"; 
    $message .= "\n"; 
    $message .= "------=MIME_BOUNDRY_main_message\n"; 
    $message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";
    $message .= "Content-Transfer-Encoding: base64\n";
    $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
    $message .= $data; //The base64 encoded message
    $message .= "\n"; 
    $message .= "------=MIME_BOUNDRY_main_message--\n"; 

    // send the message
    mail("$to_name<$to_email>", $subject, $message, $headers); 
    print "Mail sent.  Thank you for using the GCRi Expense Mailer.";
  }
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <style="css" type="text/css">
      <!--
      body {
        margin: 0px;
        font-family: Verdana, Arial, Helvetica, sans-serif;
        font-size: 12px;
      }
      a {color: #0000ff}
      -->
    </style>
  </head>
  <body>

<?php
switch ($action) {
  case "send":
    sendMail();
    showForm();
    break;
  default:
    showForm();
}
?>

  </body>
</html>

Open in new window

0
 
Jagadishwor DulalBraces MediaCommented:
What is the problem now in your code? do you test it ? The first problem which you solve was form enctype
<form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">

Open in new window

which you have used in second solution that is when you want to post file from a form you always need enctype="multipart/form-data" so check your code filling form.
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
rodneygrayAuthor Commented:
Okay so I solved the email to/from issue by making the field hidden with default values, now my last question is this.

I want the subject to show up like this.
$eyear, $emonth, $eday, $employee, $client, $evendor, $edesc, $eamount

But with the values of course. So what did I do wrong? Why can't I set $subject = to the above fields and it work? Do I have to do a post to each var before phrasing the $subject? Forgive me for not being an expert of PHP, I am trying to learn.
<?php
/* Mailer with Attachments */

$action = $_REQUEST['action'];
global $action;

function showForm() {
?>

  <form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">
  <input type="hidden" name="action" value="send" />
  <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
  
    <p>
    <input type="hidden" name="to_name" size="50" readonly="true" value="Recpt Name" /><br/>
    <input type="hidden" name="to_email" size="50" readonly="true" value="Rec@rec.com" /><br/>
    <input type="hidden" name="from_name" size="50" readonly="true" value="GCRi Web Expense Form" /><br/>
    <input type="hidden" name="from_email" size="50" readonly="true" value="rec@rec.com" /><br/>
	Month: <select id="emonth" name="emonth">
			<option value="NA">Select...</option>
			<option value="01">January</option>
			<option value="02">Febuary</option>
			<option value="03">March</option>
			<option value="04">April</option>
			<option value="05">May</option>
			<option value="06">June</option>
			<option value="07">July</option>
			<option value="08">August</option>
			<option value="09">September</option>
			<option value="10">October</option>
			<option value="11">November</option>
			<option value="12">December</option>
		</select> <br/>
		Day: <select id="eday" name="eday">
			<option value="NA">Select...</option>
			<option value="01">1st</option>
			<option value="02">2nd</option>
			<option value="03">3rd</option>
			<option value="04">4th</option>
			<option value="05">5th</option>
			<option value="06">6th</option>
			<option value="07">7th</option>
			<option value="08">8th</option>
			<option value="09">9th</option>
			<option value="10">10th</option>
			<option value="11">11th</option>
			<option value="12">12th</option>
			<option value="13">13th</option>
			<option value="14">14th</option>
			<option value="15">15th</option>
			<option value="16">16th</option>
			<option value="17">17th</option>
			<option value="18">18th</option>
			<option value="19">19th</option>
			<option value="20">20th</option>
			<option value="21">21st</option>
			<option value="22">22nd</option>
			<option value="23">23rd</option>
			<option value="24">24th</option>
			<option value="25">25th</option>
			<option value="26">26th</option>
			<option value="27">27th</option>
			<option value="28">28th</option>
			<option value="29">29th</option>
			<option value="30">30th</option>
			<option value="31">31st</option>
		</select> <br/>
		Year: <select id="eyear" name="eyear">
			<option value="NA">Select...</option>
			<option value="2010">2010</option>
			<option value="2011">2011</option>
			<option value="2012">2012</option>
			</select> <br/>
		Employee: <select id="employee" name="employee">
			<option value="NA">Select...</option>
			<option value="Rodney">Rodney</option>
			<option value="Shawn">Shawn</option>
			<option value="Josh">Josh</option>
		</select> <br/>
		Client Name: <input type="text" name="client" size="20" value=""><br/>
		Vendor: <input type="text" name="evendor" size="20" value=""><br/>
		Purchase Description:<br/><textarea rows="5" cols="40" name="edesc"></textarea><br/>
		Amount: <input type="text" name="eamount" size="20" value=""><br/>

    Attachment: <input type="file" name="attachment" size="50" /><br/>
    <input type="submit" value="Send Email" /></p>
  
<?php
}

function sendMail() {
  if (!isset ($_POST['employee'])) { //Oops, forgot to put the employee!
    die ("<p>Oops!  You forgot to fill out the employee making the expense! Click on the back arrow to go back</p>");
  }
  else {
    $to_name = stripslashes($_POST['to_name']);
    $from_name = stripslashes($_POST['from_name']);
    $subject = "$eyear, $emonth, $eday, $employee, $client, $evendor, $edesc, $eamount";
    $body = stripslashes($_POST['body']);
    $to_email = $_POST['to_email'];
    $attachment = $_FILES['attachment']['tmp_name'];
    $attachment_name = $_FILES['attachment']['name']; 
    if (is_uploaded_file($attachment)) { //Do we have a file uploaded?
      $fp = fopen($attachment, "rb"); //Open it
      $data = fread($fp, filesize($attachment)); //Read it
      $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
        fclose($fp);
    }
    //Let's start our headers
    $headers = "From: $from_name<" . $_POST['from_email'] . ">\n";
    $headers .= "Reply-To: <" . $_POST['from_email'] . ">\n"; 
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n"; 
    $headers .= "X-Sender: $from_name<" . $_POST['from_email'] . ">\n";
    $headers .= "X-Mailer: PHP4\n";
    $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
    $headers .= "Return-Path: <" . $_POST['from_email'] . ">\n"; 
    $headers .= "This is a multi-part message in MIME format.\n";
    $headers .= "------=MIME_BOUNDRY_main_message \n"; 
    $headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n"; 
    
    $message = "------=MIME_BOUNDRY_message_parts\n";
    $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; 
    $message .= "Content-Transfer-Encoding: quoted-printable\n"; 
    $message .= "\n"; 
    /* Add our message, in this case it's plain text.  You could also add HTML by changing the Content-Type to text/html */
    $message .= "$body\n";
    $message .= "\n"; 
    $message .= "------=MIME_BOUNDRY_message_parts--\n"; 
    $message .= "\n"; 
    $message .= "------=MIME_BOUNDRY_main_message\n"; 
    $message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";
    $message .= "Content-Transfer-Encoding: base64\n";
    $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
    $message .= $data; //The base64 encoded message
    $message .= "\n"; 
    $message .= "------=MIME_BOUNDRY_main_message--\n"; 

    // send the message
    mail("$to_name<$to_email>", $subject, $message, $headers); 
    print "Mail sent.  Thank you for using the GCRi Expense Mailer.";
  }
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <style="css" type="text/css">
      <!--
      body {
        margin: 0px;
        font-family: Verdana, Arial, Helvetica, sans-serif;
        font-size: 12px;
      }
      a {color: #0000ff}
      -->
    </style>
  </head>
  <body>

<?php
switch ($action) {
  case "send":
    sendMail();
    showForm();
    break;
  default:
    showForm();
}
?>

  </body>
</html>

Open in new window

0
 
rodneygrayAuthor Commented:
I was able to solve the issue by posting the vars.
0
 
Ray PaseurCommented:
You will learn much more from the structured examples in the SitePoint book than from reading and copying (sometimes shoddy) PHP code.  It has great explanations and come with a code library that you can download and modify for your own use.
http://www.sitepoint.com/books/phpmysql4/

And if you want to learn about how to handle DATETIME values, this article teaches some of the ways.  (Hint - you do not need all that drop-down-select stuff!)
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Best regards, ~Ray
0
 
rodneygrayAuthor Commented:
The date stuff is for entering expenses after the expense is made, also if you don't get to it for days.
Thanks for the links though, I will look them over tonight/tomorrow.
0
 
Ray PaseurCommented:
The "date stuff" will need to be auditable by a certified public accountant before the books can be closed.  It is actually very easy to do it right!
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

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

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