Click to See Complete Forum and Search --> : Email Form with File Attachemnt


Redhead
12-22-2004, 07:46 PM
Hi There i'm trying to allow my user to submit a form to send to me with a file attachemt. Everthing works great except the attachemt of the file.

Can anyone help me? here is my form code:

<form name="form" enctype="multipart/form-data" method="post" action="send-coments1.php">
<?php
if ($error == 1)
{
echo "<p>Sorry, we were unable to process your registration.<br>
<b>Some fields were not filled in. Please Try Again</b>.</p>";
}
?>
Name:
<input name="Name" type="text" size="40" value=<? echo $Name; ?>>
Email Address:
<input name="Email" type="text" size="40" value=<? echo $Email; ?>>
Add Image:
<input name="fileatt" type="file" id="fileatt" size="40">
Commentsan:
<textarea name="Comments" rows="5" cols="50" value=<? echo $Comments; ?>></textarea>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Clear Form">
</form>


Here is my email script:

<?php
$error = 0;

if (($Name == "") ||($Email == "" )|| ($Comments == ""))
{
$error = 1;
}
if ($error == 1)
{
$l = "Location: http://www.websiteaddress.com/contact1.php?error=1";
$l .="&Name=$Name&Email=$Email&Comments=$Comments";
header($l);
exit;
}

//Define some variables
foreach($_FILES as $value) {
foreach($value as $k => $v) {
echo $k.' => '.$v.'<br>';
}
}

if($_FILES['fileatt']['name'] == '') {
echo 'You did not select a photo to upload';
}
elseif($_FILES['fileatt']['size'] == 0) {
echo 'There appears to be a problem with the photo your are uploading';
}
elseif($_FILES['fileatt']['size'] > $MAX_FILE_SIZE) {
echo 'The photo you selected is too large';
}
elseif(!getimagesize($_FILES['fileatt']['tmp_name'
])) {
echo 'The photo you selected is not a valid image file';
}
else {
$uploaddir = 'uploads/'; // remember the trailing slash!
$uploadfile = $uploaddir . $_FILES["fileatt"]['name'];
if(move_uploaded_file($_FILES['fileatt']['tmp_name
'], $uploadfile)) {
echo 'Upload file success!';
}
else {
echo 'There was a problem uploading your file.<br>';
print_r($_FILES);
}
}

$youremail="myemail@something.com";

$emailsubject="Contact!";

$from_who="<myemail@something.com>";

$pagetitle="Thank You!";

if (getenv(HTTP_CLIENT_IP)){
$user_ip=getenv(HTTP_CLIENT_IP);
}
else {
$user_ip=getenv(REMOTE_ADDR);
}

$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Comments = $_POST['Comments'];
$datafile = $_POST['datafile'];

$mailbody="Senders Name:\n=================\n$Name\n\n";
$mailbody.="Senders Email:\n=================\n$Email\n\n";
$mailbody.="Senders Comments:\n=================\n$Comments";

mail("$youremail", "$emailsubject", "$mailbody", "From: $from_who"); // Send the email.

$comments = nl2br($comments);

$k = "Location: http://www.websiteaddress.com/contact_thanks1.php?";
header($k);

?>



Please Help Me i'm going crazy
LOL Thanks

Redhead
12-24-2004, 12:43 PM
Hi everyone ok, i'm getting no help with this on.. So i search through some old threds and found something that would be helpful. but was not...

here is my new php code. like i sayid before the email part works it's just NOT attaching any files.... HELP ME PLEASE PLEASE

<?php
$error = 0;

if (($Name == "") ||($Email == "" )|| ($Comments == ""))
{
$error = 1;
}
if ($error == 1)
{
$l = "Location: http://www.mywebsite.com/contact1.php?error=1";
$l .="&Name=$Name&Email=$Email&Comments=$Comments";
header($l);
exit;
}

$youremail="myemail address";
// This is the address that the information submitted will be sent to.

$emailsubject="Contact form info!"; // This will the email's subject

$from_who="$Email";

$pagetitle="Thank You!"; // Title to be displayed on the sent info page.

if (getenv(HTTP_CLIENT_IP)){
$user_ip=getenv(HTTP_CLIENT_IP);
}
else {
$user_ip=getenv(REMOTE_ADDR);
}

// Read POST request params into global vars
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Comments = $_POST['Comments'];
$fileatt = $_POST['fileatt'];

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

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 plain message
$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";

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

// 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";
}

$mailbody="Senders Name:\n=================\n$Name\n\n";
$mailbody.="Senders Email:\n=================\n$Email\n\n";
$mailbody.="Senders Comments:\n=================\n$Comments";

mail("$youremail", "$emailsubject", "$mailbody", "From: $from_who"); // Send the email.

$comments = nl2br($comments);

?>