Warning: fopen(graphic_design/files/thread-785-1.txt) [function.fopen]: failed to open stream: Permission denied in /graphic_design/global.php on line 421
file NOT opened php form w/file attachment? -
PDA

View Full Version : php form w/file attachment?


dubtastic
05-07-2004, 02:50 PM
here is my code that i grabbed off the intarwebnet:


<?php


// Read POST request params into global vars
$redirectpage = "thanks_submit.php";
$errorpage = "error.php";
$to = "no@dubtastic.com";
$replyTo = $_POST['email'];
$from = $_POST['firstname'] . " " . $_POST['lastname'];
$subject = "Testing File Attachment";
$message = $_POST['message'];

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

$headers = "From: " . $from . "\r\nReply-To: " . $replyTo;

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

// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
header("Location: $redirectpage");
} else {
header("Location: $errorpage");
}
?>

what adjustments need to be made to the code so that the description and other information is included in the message? it is sending the file attachment, but it is not sending the persons website or description in the message. it is almost as if the file attachment overwrites the $message variable instead of appending to it.

any of you php gurus out there wanna tell me where i messed up?

dubtastic
05-08-2004, 05:44 AM
nevermind, figured it out... the form field with the description was named description instead of "message". also made some other changes. here is the code for those who care, and also for those that dont! :P


<?php


// Read POST request params into global vars
$redirectpage = "thanks_submit.php";
$errorpage = "error.php";
$to = "email here";
$replyTo = $_POST['email'];
$from = $_POST['firstname'] . " " . $_POST['lastname'];
$subject = "Test Subject Line";
$message = stripslashes("Name: " . $from . "\n\n" . "Email: " . $_POST['email'] . "\n\nWebsite: ". $_POST['url'] . "\n\n" . "Descripton: " . $_POST['description']);

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

$headers = "From: " . $from . "\r\nReply-To: " . $replyTo;

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

// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
header("Location: $redirectpage");
} else {
header("Location: $errorpage");
}
?>

Scotty
05-14-2004, 06:53 AM
Boy!....this php stuff has me so baffled.....I am the old fashioned html guy with a wee touch of the asp............

Arch Stanton
05-14-2004, 10:03 AM
here scotty, this will help you wityh php, once you start, you'll never turn back:

http://www.ucit.ca/serverside1.php

Scotty
05-14-2004, 10:11 AM
here scotty, this will help you wityh php, once you start, you'll never turn back:
http://www.ucit.ca/serverside1.php

Cheers Joel......I will add this site to my Fav's and try some php out- I suppose better late than never eh!