I'm quite new to coding in PHP and have been having some problems over the last few days. I have set up a form that allows the user to attach a file and enter some basic details.
I have then set up send.php to process this information and send it and the attached file through to me. It all seems to be working fine, but my email client says there is an attached file right up until the point that I open the email. Then it disappears.
The form code that I using is as follows;
<form action="./send.php" method="post" enctype="multipart/form-data">
<p>
<table cellspacing="10">
<tr>
<td><label for="name"><font size="2">Name</font></label></td>
<td>
<input type="text" name="name" value="" id="name" maxlength="100" />
</td>
</tr>
<tr>
<td><label for="email"><font size="2">Email Address</font></label></td>
<td>
<input type="text" name="email" id="email" maxlength="150" />
</td>
</tr>
<tr>
<td><label for="phone"><font size="2">Phone Number</font></label></td>
<td>
<input type="text" name="phone" id="phone" maxlength="20" />
</td>
</tr>
<tr>
<td><label for="type"><font size="2">Job Type</font></label></td>
<td>
<input type="checkbox" name="type1" id="type1" value="permanent" /> Permanent
<input type="checkbox" name="type2" id="type2" value="temporary_contact" /> Temporary & Contract
</td>
</tr>
<tr>
<td><label for="file"><font size="2">Upload CV</font></label></td>
<td> <input type="file" name="userfile" id="file"></td>
</tr>
<tr>
<td>
<button>Submit</button>
</td>
</tr>
</table>
And the send.php code is as follows;
<?php
$name = $_POST['name'] ;
$phone = $_POST['phone'] ;
$email = $_POST['email'] ;
$Perm = $_POST['type1'] ;
$Temp = $_POST['type2'] ;
//Obtain File variables
$fileatt = $_FILES['userfile']['tmp_name'];
$fileatt_type = $_FILES['userfile']['type'];
$fileatt_name = $_FILES['userfile']['name'];
$headers = "From: $email";
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" .
$name . "\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("MyEmailAddress", "CV Submission from $name", "CV Submitted by $name from $email", $headers);
if ($ok) {
echo "<p>Mail sent! Yay PHP!</p>";
} else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
?>
I realise it's all a bit untidy, but I have been ready through troubleshooting manuals and tutorials and rereading my code for about 48 hours solid now and I'm fresh out of ideas. If any of you has any advice, I'd been indebted to you. If not, thanks for having a look.
Graham