So I have my html form, and it calls to my php page, I want it to send an email with a file attached, however, it is sending the email and form information but not attaching the file, not quite sure why this is happening. I have been messing with this for sometime with no luck on getting it to attach the file. Any thoughts or direction? Thanks in advance!
html -
<form id="form1" name="form1" method="post" action="apply.php">
<p>
<label>First Name :
<input type="text" name="fname" id="fname" />
</label>
</p>
<p>
<label>Last Name :
<input type="text" name="lname" id="lname" />
</label>
</p>
<p><span id="sprytextfield1">
<label>E-Mail :
<input type="text" name="email" id="email" />
</label>
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></p>
<p>
<label>Upload Resume :
<input type="file" name="fileatt"/>
</label>
</p>
<p>
<label>Additional Information :<br />
<textarea name="addition" id="addition" cols="45" rows="5"></textarea>
</label>
</p>
<p>
<label>
<input type="submit" value="Submit Resume" />
</label>
</p>
<p> </p>
<p> </p>
</form>
the PHP -
<?php
// Read POST request params into global vars
$to = "email@email.com";
$replyTo = $_POST['email'];
$from = $_POST['fname'] . " " . $_POST['lname'];
$subject = "Test Subject Line";
$message = stripslashes("Name: " . $from . "\r\n\r\n" . "Email: " . $_POST['email'] . "\r\n\r\n" . "Descripton: " . $_POST['addition']);
// 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\r\n" .
"Content-Type: multipart/mixed;\r\n" .
"boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\r\n\r\n" .
"--{$mime_boundary}\r\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" .
"Content-Transfer-Encoding: 7bit\r\n\r\n" .
$message . "\r\n\r\n";
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
// Add file attachment to the message
$message .= "--{$mime_boundary}\r\n" .
"Content-Type: {$fileatt_type};\r\n" .
" name=\"{$fileatt_name}\"\r\n" .
"Content-Disposition: attachment;\r\n" .
"filename=\"{$fileatt_name}\"\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n" .
$data . "\n\n" .
"--{$mime_boundary}--\r\n";
}
// Send the message
$ok = @mail($to, $subject, $message, $headers);
$theResults = <<<EOD
<html>
<head>
<title>The test Page </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body {
background-color: #f1f1f1;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
font-style: normal;
line-height: normal;
font-weight: normal;
color: #666666;
text-decoration: none;
}
-->
</style>
</head>
<div>
<div align="left">Thank you for your interest! Your email will be answered very soon!</div>
</div>
</body>
</html>
EOD;
echo "$theResults";
?>