Hey everyone,
I doing a form in html that only needs to upload an attachments then shoots it’s to an email address. I can upload it but when i click it, the attachment does not attach it. Here is both html and php code
<form action="resume.php" method="post" name="form1" id="form1" onsubmit="MM_validateForm('resumeText','','R');return document.MM_returnValue">
<p>
<input type="file" name="attachment" size="50" />
<input type="button" name="add_attachment" value="Add attachment" onclick="addAttachment()">
</p>
<p>
<input type="submit" name="submitText" id="submitText" value="Submit" />
</p>
</form>
<script type="text/javascript" language="javascript">
var trow;
function init()
{
trow = 2;
}
function addAttachment()
{
var tbodyElem = document.getElementById("emailTBody");
var trElem, tdElem, txtNode, tmpString;
trElem = tbodyElem.insertRow(trow);
tdElem = trElem.insertCell(0);
tdElem.className = "gradient";
tdElem.setAttribute("style", "text-align: right;");
txtNode = document.createTextNode("Attachment " + (trow - 1) + ":");
tdElem.appendChild(txtNode);
tdElem = trElem.insertCell(1);
tdElem.className = "gradient";
tdElem.setAttribute("style", "text-align: left;");
tdElem.setAttribute("colspan", "3");
var input = document.createElement("INPUT");
input.setAttribute("type", "file");
input.setAttribute("name", "attachment" + (trow - 1));
input.setAttribute("size", "100");
input.setAttribute("value", "");
input.setAttribute("id", "attachment" + (trow - 1));
tdElem.appendChild(input);
trow++;
}
</script>
<?php
$emailPattern = '/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i';
//$to = "Info@hereandhome.org";
$to = "atlnycdude23@gmail.com";
$subject = "Submit Your Resume";
$from = "Here and Home";
$filecount = 0;
foreach($_FILES as $file => $value) {
$attachment[(int)$filecount] = $_FILES[$file]['tmp_name'];
$attachment_name[(int)$filecount] = $_FILES[$file]['name'];
if (is_uploaded_file($attachment[(int)$filecount])) { //Do we have a file uploaded?
$fp = fopen($attachment[(int)$filecount], "rb"); //Open it
$data[(int)$filecount] = fread($fp, filesize($attachment[(int)$filecount])); //Read it
$data[(int)$filecount] = chunk_split(base64_encode($data[(int)$filecount])); //Chunk it up and encode it as base64 so it can emailed
fclose($fp);
$filecount++;;
}
}
$headers = "From: ". $from . "<" . $to. ">
";
$message .= "Resume: " . $resumeText . "
";
for ($i = 0, $filecount = (int) count($data); $i < $filecount; $i++) {
$message .= "Content-Type: application/octet-stream;
name=\"" . $attachment_name[$i] . "\"
";
$message .= "Content-Transfer-Encoding: base64
";
$message .= "Content-Disposition: attachment;
filename=\"" . $attachment_name[$i] . "\"
";
$message .= $data[$i]; //The base64 encoded message
$message .= "
";
}
if (mail($to,$subject,$message,$headers))
{
echo "Data has been submitted to $to!";
} else {
echo "&Result=error";
}
function safe($string)
{
$pattern = "/\r|
|\%0a|\%0d|Content\-Type:|bcc:|to:|cc:/i";
return preg_replace($pattern, '', $string);
}
?>
What am i doing wrong? Please help. Thanks.