Hi there,
can anyone see why this might not work on my machine? The form displays and fills in correctly, but when I hit send the form clears and no mail arrives...
<?php
Now we'll make the form. When making forms that allow
file uploads, it is important to remember to set the
"enctype" attribute in the <form> tag to "multipart/form-data".
echo "<form action='$PHP_SELF' enctype='multipart/form-data' method='post'>\n";
echo "<input type='text' name='from'><br>\n";
echo "<input type='text' name='to'><br>\n";
echo "<input type='text' name='subject'><br>\n";
echo "<input type='file' name='attachment'><br>\n";
echo "<textarea name='body'></textarea><br>\n";
echo "<input type='submit' name='send' value='Send'>\n";
echo "</form>\n";
If the user has pushed the "Send" button:
if ($send) {
Defining the boundary:
$boundary = uniqid( "");
# Making the headers:
$headers = "From: $from
Content-type: multipart/mixed; boundary=\"$boundary\"";
# Determining the MIME type of the uploaded file:
if ($attachment_type) $mimeType = $attachment_type;
# If the browser did not specify the MIME type of the uploaded
# file, we'll just set the MIME type to "application/unknown".
else $mimeType = "application/unknown";
# Determining the file name:
$fileName = $attachment_name;
# Opening the file:
$fp = fopen($attachment, "r");
# Reading the entire file into a variable:
$read = fread($fp, filesize($attachment));
# OK, now we have a variable named $read
# that contains the entire file as a text chunk.
# Now we need to convert that text chunk into
# something that the mail clients can read.
# This means we'll have to encode it with base64.
$read = base64_encode($read);
# Now we've got a long text base64 encoded
# text string. The next thing we need to do
# is to convert that long string to
# small pieces consisting of 76 characters
# per line.
$read = chunk_split($read);
# Now we can create the mail body:
$body = "--$boundary
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 8bit
$body
--$boundary
Content-type: $mimeType; name=$fileName
Content-disposition: attachment; filename=$fileName
Content-transfer-encoding: base64
$read
--$boundary--";
# Mailing the whole thing:
mail($to, $subject, $body, $headers);
}
?>
thanks - dunstan