<?php
// if there is post
if (isset($_POST)&& !empty($_POST)){
//if there is an attachment
if (!empty($_FILES['attachment']['name'])){
//store some variable
$file_name = $_FILES['attachment']['name'];
$temp_name = $_FILES['attachment']['tmp_name'];
$file_type = $_FILES['attachment']['type'];
//get the extension of the file
$base = basename($file_name);
$extension = substr(&$base, strlen($base)-4,strlen($base));
//only these file type will be allowed.
$allowed_extensions = array(".doc","docx",".pdf",".zip",".png",".txt");
//check that this file type is allowed
if (in_array($extension,$allowed_extensions)){
//mail essenstials
$from = $_POST['email'];
$to = "jbbhatti@yahoo.com"; //change to
$subject = "Chaking E-mail Attechment"; //change to
$message = "I am M junaid bhatti and Chaking e-mail att: function.";
//things you need
$file = $temp_name;
$content = chunk_split(base64_encode(file_get_contents($file)));
$uid = md5(uniqid(time()));
//standard mail headers
$header = "From: dj4maza.com@gmail.com \r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
//declaring we have multiple kind of email (i.e plain text and attachment.
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
/*
//plan text part
$header .= "--".$uid."\r\n";
$header .= "Content-type: text/plain; charset-iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
*/
//file attachment
$header .= "--".$uid."\r\n";
$header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n";
$header .= $content."\r\n\r\n";
//send the mail (message is not here, but in the header in a multi part
if(mail($to,$subject,"",$header)){
echo "Mail Have been Send, Thanking you mail us.";
}else{
echo "Mail Could Not b Send Us.";
}
exit();
}else{
echo"File Type Not Allowed";
}
}else {
echo" No File Posted";
}
}
?>
HTML Form
<html>
<body>
<form method="post" enctype="multipart/form-data">
<input type="text" name="email" value="form"/>
<br>
<input type="file" name="attachment"/>
<br>
<input type="submit" value="Send Mail"/>
</form>
</body>
</html>
No comments :
Post a Comment