CREATING A FORM THAT WILL E-MAIL AN ATTACHMENT WITH THE FORM DATA (BETTER) - Aug 6th, 2010


Forms are easy and many of you have been using them for quite a while. The trick comes in when you are trying to allow
the sender to attach a document or image to the submitted form. Here’s a simple set of PHP scripts that will
accomplish the task.

This one’s based on the post by bokeh at:

http://www.webdeveloper.com/forum/showthread.php?p=344578

If security is an issue, you may want to password protect this page from being accessed by unauthorized users.

You can cut and paste the code below into new documents or you can download the set from:

http://www.thecmsbcookbook.com/downloads/forms1.zip

The first one is the form that you’ll use to collect the information. You can call this anything appropriate. Like
eventform.php This one is set up to collect event submission data for an arts organization, so you’ll have to modify
it for your use.

The second is the PHP document that actually handles the form submission mail2.php

CHANGES TO EMAIL2.PHP

The first thing to change is the e-mail address of the recipient ($to) on line 5 of mail2.php and the “from” e-mail
on line 189.

After that, if you change the form fields in eventform.php you’ll have to mirror those changes in the mail.php file on
line 7, and in the ifelse” statement that currently starts on line 97 (the check that all required fields are filled
in before submission)

Near the end of the document on line 187, you’ll find the text of the confirmation e-mail that is sent to the e-mail
address entered on the form. And on line 189 you’ll find the field that is copied into the “RE:” of that e-mail
and the “from” address.

On line 191 you’ll find the text of the confirmation web page that appears after a successful form submission.

If you want to change the error message that comes up if the e-mail addresses entered do not match, that’s found on
line 194

EVENTFORM.PHP



<form action="email2.php" enctype="multipart/form-data" method="post" onsubmit="
document.getElementById('container0').style.display='';
document.getElementById('container2').style.display='none';
progress();
return true;">
<table width="90%" border="0">

</td>
</tr>
<tr>
<td width="17%">
<label for="name">Member's<br />Full Name: <span style="color: red;">*</span></label></td>
<td width="83%">
<input id="name" type="text" size="50" name="name" value="<?php print "$name"; ?>" />
</td>
</tr>
<tr>
<td>
<label for="email">Member's<br />E-mail address: <span style="color:red;">*</span></label>
</td>
<td>
<input id="email" type="text" name="email" size="50" value="<?php print "$email"; ?>" />
</td>
</tr>
<tr>
<td>
<label for="confirm_email">Confirm e-mail: <span style="color: red;">*</span></label>
</td>
<td>
<input id="confirm_email" type="text" name="confirm_email" size="50" value="<?php print "$confirm_email"; ?>" /></td>
</tr>
<tr>
<td><label for="event_title">Event Title: <span style="color: red;">*</span></label>
</td>
<td><input id="event_title" type="text" name="event_title" size="50" value="<?php print "$event_title"; ?>" /></td>
</tr>
<tr>
<td>
<label for="venue_name">Venue Name: <span style="color: red;">*</span></label>
</td>
<td>
<input id="venue_name" type="text" name="venue_name" size="50" value="<?php print "$venue_name"; ?>" />
</td>
</tr>
<tr>
<td>
<label for="venue_location">Venue Address and Location: <span style="color: red;">*</span></label>
</td>
<td>
<textarea id="venue_location" name="venue_location" cols="38" rows="5"><?php print "$venue_location"; ?></textarea>
</td>
</tr>
<tr>
<td>
<label for="venue_url">Venue Web Address:</label>
</td>
<td><input id="venue_url" type="text" name="venue_url" size="50" value="<?php print "$venue_url"; ?>" />
</td>
</tr>
<tr>
<td>
<label for="start_date">Start Date: <span style="color: red;">*</span></label>
</td>
<td>
<input id="start_date" type="text" name="start_date" size="50" value="<?php print "$start_date"; ?>" />
</td>
</tr>
<tr>
<td><label for="end_date">End Date:</label>
</td>
<td>
<input id="end_date" type="text" name="end_date" size="50" value="<?php print "$end_date"; ?>" />
</td>
</tr>
<tr>
<td>
<label for="reception_date">Reception Date:</label>
</td>
<td>
<input id="reception_date" type="text" size="50" name="reception_date" value="<?php print "$reception_date"; ?>" />
</td>
</tr>
<tr>
<td><label for="reception_time">Reception Time:</label>
</td>
<td><input id="reception_time" type="text" name="reception_time" size="50" value="<?php print "$reception_time"; ?>" />
</td>
</tr>
<tr>
<td>
<label for="description">Event Description: <span style="color: red;">*</span></label>
</td>
<td>
<textarea id="description" name="description" cols="38" rows="5"><?php print "$description"; ?></textarea>
</td>
</tr>
<tr>
<td>
&nbsp;
</td>
<td>
<label for="fileatt">Attach an image of your work that will be in the event:</label><br /><input id="fileatt"
type="file" name="fileatt" size="50" />
<br />This image may be included in the listing and should be no larger than 1MB.
</td>
</tr>
<tr>
<td colspan="2"><input type="hidden" name="hidden" value="1" /></td>
</tr>
<tr>
<td>
<label for="submit"><span style="color: red;">*</span> Required fields.</label>
</td>
<td>
<input id="submit" type="submit" value="Send" />
</td>
</tr>
</table>
</form>



The second file is called email2.php



<?php

ob_start();

$to = 'your@email.com';

$keys = array('name', 'email', 'confirm_email', 'event_title', 'venue_name', 'venue_location', 'venue_url', 'start
date', 'end_date', 'reception_date', 'reception_time', 'description', 'hidden');
foreach(
$keys as $key)
{
$
$key = isset($_POST[$key]) ? $_POST[$key] : null ;
}

print (
'
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Email</title>
<script type="text/javascript">

function progress(){
intWidth = parseInt(document.getElementById("container1").style.width) + 1;
if(intWidth <= 400){
document.getElementById("container1").style.width = intWidth+"px";
}else{
document.getElementById("container1").style.width = 0;
}
setTimeout("progress()",300);
}

</script>
</head>

<body>
');

//Make sure email and confirm email are the same
if (!empty ($hidden)) {
if (
$email == $confirm_email) {
}else{
$email = '';
$confirm_email = '';
}
}


//Do a reg_ex check on the email
if (!empty ($hidden)) {
$regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
if (
eregi($regexp, $email))
{
}else{
$email = '';
$confirm_email = '';
}
}
// End of email checking

if (empty ($hidden)) {

print (
'<div id="container2">
<h1 id="content_h1"><a name="text"> E-mail </a></h1> <p>Please use the following form to e-mail us:</p>

');

include (
"form.php");

print (
'</div>

<div id="container0" style="display: none;">

<p style="font-size: 15pt; font-family: sans-serif; color:#fd6700; background:#fff;">
Loading...
</p>

<div id="container1" style="width:0px; height:5px; background-color:#fd6700; margin-top:0px; text-align: left;"></div>

<p>Please be patient while your data is processed. This may take a few moments especially if you are uploading a
file.</p>

</div>
');

}

// Check to see if all required fields are filled in correctly.

if (!empty ($hidden)) {

if (
$_FILES['fileatt']['error'] == 1){
print (
'<h2 id="content_h1"><a name="text">There\'s a problem with the imamge that you\'re trying to upload</a>

<p>The maximum file size that can be uploaded using this form is 2 megabytes.
</p></h2>');
}elseif ( (empty (
$name))) {
print (
'<h2 id="content_h1"><a name="text">You must fill in the "Member\'s Full Name" field.</a><p>All required fields
must be filled in before you can submit this form.</p> </h2>'
);

}elseif ( (empty (
$event_title))) {
print (
'<h2 id="content_h1"><a name="text">You must fill in the "Event Title" field.</a><p>All required fields must be
filled in before you can submit this form. </p> </h2>
');
}elseif ( (empty (
$venue_name))) {
print (
'<h2 id="content_h1"><a name="text">You must fill in the "Venue Name" field.</a><p>All required fields must be
filled in before you can submit this form. </p> </h2>
');}elseif ( (empty ($venue_location))) {
print (
'<h2 id="content_h1"><a name="text">You must fill in the "Venue Address" field.</a><p>All required fields must
be filled in before you can submit this form. </p> </h2>
');
}elseif ( (empty (
$start_date))) {
print (
'<h2 id="content_h1"><a name="text">You must fill in the "Start Date" field.</a><p>All required fields must be
filled in before you can submit this form. </p></h2>
');
}elseif ( (empty (
$description))) {
print (
'<h2 id="content_h1"><a name="text">You must fill in the "Event Description" field.</a></h1><p>All required
fields must be filled in before you can submit this form. </p></h2>
');
}elseif ( (!empty (
$name)) && (!empty ($email)) && (!empty ($venue_name))&& (!empty ($start_date))&& (!empty
(
$description))&& (!empty ($event_title))) {

// Get html message content
$form_data = "<p>This email is from <span class=\"bold\">$name</span> \n\n ";
$form_data .= "<p><b>Email Address:</b>$email</p><p><b>Event Title:</b>$event_title</p><p><b>Venue
Name:</b>$venue_name</p><p><b>Venue Location:</b>$venue_location</p><p><b>Venue URL:</b>$venue_url</p><p><b>Event Start
Date</b>$start_date</p><p><b>Event End Date</b>$end_date</p><p><b>Reception Date</b>$reception_date</p><p><b>Reception
Time</b>$reception_time</p><p><b>Event Description</b>$description</p> ";

$message = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \n" .
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> \n" .
"<html xmlns=\"http://www.w3.org/1999/xhtml\"> \n" .
"<head> \n" .
" <meta http-equiv=\"content-type\" content= \n" .
" \"text/html; charset=iso-8859-1\" /> \n" .
"<style type=\"text/css\"> \n" .
"body { font-size: 9pt; font-family: verdana, sans-serif; color: #000; background:#fff; } \n" .
".bold { font-weight: bold; } \n" .
"</style> \n" .
"</head> \n" .
"<body>$form_data \n" .
"</body> \n" .
"</html> \n\n";

// Obtain file upload vars
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];


$headers = "From: $from";

if (
is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

// Add a multipart boundary above the html message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";


// Base64 encode the file data
$data = chunk_split(base64_encode($data));

//We now have everything we need to write the portion of the message that contains the file attachment. Here's the code:

// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}else{
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

// Add a multipart boundary above the html message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
}


//That completes the modifications necessary to accommodate a file attachment. We can now send the message with a quick
call to mail:

// Send the message
mail($to, $subject, $message, $headers);

$body = "Dear $name, \n\nThank you for your event submission. We'll post the information as soon as
possible.\n\nCommunications Committee" ;

mail ($email, "Re: $event_title", $body, 'From:your@email.com');

print (
'<h1 id="content_h1"><a name="text"> Thank you </a></h1> <p>Thanks for submitting your
event information. Don\'t forget to send us information about any new events that you\'re in. We\'ll post as many as we
can.</p><p>Communications Committee</p>');

}else{
print (
'<h2 id="content_h1"><a name="text">Please check your e-mail addresses to make sure they match and then
resubmit the form.</a> <p>Thank you.</p></h2>');
include (
"form.php");
}
}

// This is the end of the insert

print ('

</div>
</body>
</html>
');

ob_end_flush();

?>




The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service