Could somebody please take a look at this extremely simple php file upload form and tell me what I am doing wrong, it all works fine on the website, even recieving the confirmation message. However no email is being recieved at my email address. This is my first attempt at a PHP form.
<?php
if(isset($_POST['submit']))
{
//The form has been submitted, prep a nice thank you message
$output = '<h1>Your Application Has Been Received</h1>';
//Set the form flag to no display (cheap way!)
$flags = 'style="display:none;"';
//Deal with the email
$to = 'myemail.com';
$subject = 'Application';
$message = strip_tags($_POST['message']);
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
$filename = $_FILES['file']['name'];
$boundary =md5(date('r', time()));
$headers = "From: myemail.com
Reply-To: myemail.com";
$headers .= "
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$message="This is a multi-part message in MIME format.
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
$message
--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
mail($to, $subject, $name, $email, $headers);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>MailFile</title>
</head>
<body>
<?php echo $output; ?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
<label for="name">Name</label><input type="name" name="name" id="name"></p>
<p>
<label for="phone">Telephone</label> <input type="phone" name="phone" id="phone"></p>
<p>
<label for="email">Email</label> <input type="email" name="email" id="email"></p>
<p>
<label for="message">Cover Note</label> <textarea name="message" id="message" cols="20" rows="8"> </textarea></p>
<p><label for="file">File</label> <input type="file" name="file" id="file"></p>
<p><input type="submit" name="submit" id="submit" value="send"></p>
</form>
</body>
</html>
UPDATE
The form is working but only the Cover Note is being recieved, why are the other fields not sending by email?
There are a few problems with your code:
You're passing the wrong arguments to mail()
. This line:
mail($to, $subject, $name, $email, $headers);
should be:
mail($to, $subject, $message, $headers);
You're not storing all the form fields:
You need to retrieve all the form fields from $_POST
and incorporate them into your message. Perhaps something along these lines:
$message = "Name: " . $_POST['name'] . "
";
$message .= "Phone: " . $_POST['phone'] . "
";
$message .= "Email: " . $_POST['email'] . "
";
$message .= "Message:
" . strip_tags($_POST['message']);
Personally I would check the file type against a white-list, and only use application/octet-stream
for unknown types. For example:
$type = $_FILES['file']['type'];
if (!in_array ($type, array ('text/plain', 'image/png', 'audio/wav'))) {
$type = 'application/octet-stream';
}
And then set Content-Type: $type; name="$filename"
in the message body.