Web form with attachment not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
seriousdamage
Forum Commoner
Posts: 30
Joined: Sat Nov 27, 2004 10:18 am

Web form with attachment not working

Post by seriousdamage »

Hi,
I am trying to build a form that allows me to add an attachment and send it when submittin gthe form.
Below you will find 2 scripts, one of the html form and the other for the php script. The problem is that I don't receive the attachment, but I get mail no problem.
Can someone help?
Thanks

HTML FORM:
<html>
<head>
<title>Untitled</title>
</head>

<body>
<form ENCTYPE="multipart/form-data" action="ez_formmail.php" method="post">
Name: <input type="text" name="realname"><BR>
E-mail address: <input type="text" name="email"><br>
Text: <textarea name="fixed text" cols="30" rows="4"></textarea><br>
Attach File:<input type="file" name="file"><br><br>

<input type="submit" name="">
</form>

</body>
</html>

PHP SCRIPT

<?
// Recipient of message (This can be changed via the form itself)
$recipient = 'mail@domain.com';

// Subject of message (This can be changed via the form itself)
$subject = 'WWW Form Submission';

// This is a list of domains that can run EZ FormMail. Do not include
// www, just the actual domain/ip address!
$referers = array('domain1.com', 'domain2.com', 'domain3.com');

// This is the page that users will be redirected to after the form is
// processed successfully.
$success_url = 'thanks.html';

// Your site URL
$siteurl = 'http://www.domain.com';

$path_to_file = 'http://www.gdomain.com';

###########################################################
# DO NOT EDIT BELOW THIS LINE #
###########################################################

//function Print_Footer() {
//echo '<p><center>Powered by EZ FormMail. Get it <b>free</b> from <a href="http://www.sensationdesigns.com">http://www.sensationdesigns.com</a>!</center>';
//}

function Check_Referer() {
global $referers;
$temp = explode('/', $_SERVER['HTTP_REFERER']);
$referer = $temp[2];
$found = false;
foreach ($referers as $domain) {
if (stristr($referer, $domain)) { $found = true; }
}
return $found;
}

function mail_it($content, $subject, $email, $recipient) {
global $attachment_chunk, $attachment_name, $attachment_type, $attachment_sent, $bcc;
}

if ($_POST) {
if (Check_Referer() == false) {
echo '<font size="+1" color="#FF0000">Error: Invalid Referer</font><BR>';
echo 'You are accessing this script from an unauthorized domain!';
Print_Footer();
die();
}
$ctr = 0;

$isrealname = 0;
$isemail = 0;

foreach ($_POST as $key => $val) {
if ($key == 'realname') { $isrealname = 1; }
if ($key == 'email') { $isemail = 1; }
if (substr($key, 0, 4) == 'req_' || $key == 'realname' || $key == 'email') {
if ($val == '') {
if ($ctr == 0) {
echo '<font size="+1" color="#FF0000">Error: Missing Field(s)</font><BR>';
echo 'The following <i>required</i> field(s) were not filled out:<BR>';
}
echo '<BR>- <b>'.substr($key, 4).'</b>';
$ctr++;
}
}
}
if ($ctr > 0) {
echo '<p>Click <a href="javascript:history.go(-1)">here</a> to go back';
Print_Footer();
die();
}
else {
if ($isrealname == 0) {
echo '<font size="+1" color="#FF0000">Error: Missing Field</font><BR>';
echo 'No "realname" field found.<p><a href="'.$siteurl.'">here</a> to return to the home page.';
Print_Footer();
die();
}
elseif ($isemail == 0) {
echo '<font size="+1" color="#FF0000">Error: Missing Field</font><BR>';
echo 'No "email" field found.<p><a href="'.$siteurl.'">here</a> to return to the home page.';
//Print_Footer();
die();
}
}

if (!(preg_match("/^.{2,}?@.{2,}\./", $_POST['email']))) {
echo '<font size="+1" color="#FF0000">Error: Invalid E-mail</font><BR>';
echo 'The e-mail address you entered (<i>'.$_POST['email'].'</i>) is invalid.';
//Print_Footer();
die();
}

$body = "Below is the result of your feedback form. It was submitted on:\n".date('l, F jS, Y').' at '.date('g:ia').".\n";

foreach ($_POST as $key => $val) {
if ($key == 'recipient') { $recipient = $val; }
elseif ($key == 'subject') { $subject = $val; }
else {
if ($key != 'realname' && $key != 'email') {
$body .= "\n".str_replace('req_', '', $key).": $val";
}
}
}
//$body .= "\n\n-------- Submission Details --------\n";
//$body .= "Remote Address: ".getenv('REMOTE_ADDR')."\n";
//$body .= "HTTP User Agent: ".getenv('HTTP_USER_AGENT')."\n\n";
//$body .= "--------------------------------------------------\n";
//$body .= "Powered by EZ FormMail. Available at http://www.sensationdesigns.com!";

$mailheaders = "From: ".$_POST['realname']." <".$_POST['email'].">\n";
$mailheaders .= "Reply-To: ".$_POST['email'];

mail($recipient, $subject, $body, $mailheaders);
header("Location: $success_url");
}
else {
echo '<center>You have access this page from an invalid location. Please click <a href="'.$siteurl.'">here</a> to go to '.$siteurl.'.</center>';
}

//Print_Footer();

// check for an attachment if there is a file upload it
if ($attachment_name) {
if ($attachment_size > 0) {
if (!$attachment_type) $attachment_type = "application/unknown";
$content .= "Attached File: ".$attachment_name."\n";
$fp = fopen($attachment, "r");
$attachment_chunk = fread($fp, filesize($attachment));
$attachment_chunk = base64_encode($attachment_chunk);
$attachment_chunk = chunk_split($attachment_chunk);
}
}

// check for a file if there is a file upload it
if ($file_name) {
if ($file_size > 0) {
if (!ereg("/$", $path_to_file))
$path_to_file = $path_to_file."/";
$location = $path_to_file.$file_name;
if (file_exists($path_to_file.$file_name))
$location = $path_to_file.rand(1000,3000)."form".$file_name;
copy($file,$location);
unlink($file);
$content .= "Uploaded File: ".$location."\n";
}
}

// This function takes the sorts, excludes certain keys and
// makes a pretty content string.
function parse_form($array, $sort = "") {
// build reserved keyword array
$reserved_keys[] = "MAX_FILE_SIZE";
$reserved_keys[] = "required";
$reserved_keys[] = "redirect";
$reserved_keys[] = "require";
$reserved_keys[] = "path_to_file";
$reserved_keys[] = "recipient";
$reserved_keys[] = "subject";
$reserved_keys[] = "sort";
$reserved_keys[] = "style_sheet";
$reserved_keys[] = "bgcolor";
$reserved_keys[] = "text_color";
$reserved_keys[] = "link_color";
$reserved_keys[] = "vlink_color";
$reserved_keys[] = "alink_color";
$reserved_keys[] = "title";
$reserved_keys[] = "missing_fields_redirect";
$reserved_keys[] = "env_report";
$reserved_keys[] = "submit";
if (count($array)) {
if (is_array($sort)) {
foreach ($sort as $field) {
$reserved_violation = 0;
for ($ri=0; $ri<count($reserved_keys); $ri++)
if ($array[$field] == $reserved_keys[$ri]) $reserved_violation = 1;

if ($reserved_violation != 1) {
if (is_array($array[$field])) {
for ($z=0;$z<count($array[$field]);$z++)
$content .= $field.SEPARATOR.$array[$field][$z].NEWLINE;
} else
$content .= $field.SEPARATOR.$array[$field].NEWLINE;
}
}
}
while (list($key, $val) = each($array)) {
$reserved_violation = 0;
for ($ri=0; $ri<count($reserved_keys); $ri++)
if ($key == $reserved_keys[$ri]) $reserved_violation = 1;

for ($ri=0; $ri<count($sort); $ri++)
if ($key == $sort[$ri]) $reserved_violation = 1;

// prepare content
if ($reserved_violation != 1) {
if (is_array($val)) {
for ($z=0;$z<count($val);$z++)
$content .= $key.SEPARATOR.$val[$z].NEWLINE;
} else
$content .= $key.SEPARATOR.$val.NEWLINE;
}
}
}
return $content;
}

?>
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Bump.

Please Use and tags!!![/b]
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Checkout this section of the php manual:

http://de3.php.net/manual/en/features.file-upload.php.

If you follow this you should be able to work it out. If not please post the relevent piece of code where you process the $_FILES array and I will try to assist you some more.[/b]
Post Reply