Sending mail with attachment using php
HTML Design:
<form id="attach" name="attach" method="post" action="attach.php" enctype="multipart/form-data"><table>
<tr>
<td>To</td><td>:</td><td><input type="text" name="to" id="to"></td>
</tr>
<tr>
<td>Subject</td><td>:</td><td><input type="text" name="subject" id="subject"></td>
</tr>
<tr>
<td>Message</td><td>:</td><td><input type="text" name="msg" id="msg"></td>
</tr>
<tr>
<td>Attachment<span class="imp">*</span></td><td>:</td><td><input type="file" name="upload" id="upload"></td>
</tr>
<tr>
<td></td><td></td><td><input type="submit" value="Submit" id="send" name="send"></td>
</tr>
</table>
</form>
PHP Script File (attach.php):
<?phpif(isset ($_POST["send"]))
{
$upload_name=$_FILES["upload"]["name"];
$upload_type=$_FILES["upload"]["type"];
$upload_size=$_FILES["upload"]["size"];
$upload_temp=$_FILES["upload"]["tmp_name"];
$message=$_POST["msg"];
$subject = $_POST["subject"];
$to=$_POST["to"];
if($message==""||$subject==""||$to=="")
{
echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#F3363F; font-weight:bold">Please fill all fields</font>';
}
else
{
$fp = fopen($upload_temp, "rb");
$file = fread($fp, $upload_size);
$file = chunk_split(base64_encode($file));
$num = md5(time());
//Normal headers
$headers = "From: Info Mail<serma2089@gmail.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".gettimeofday()." TheSystem@".$_SERVER['http://www.webizyme.com'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
// With message
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n";
// Attachment headers
$headers = "Content-Type:".$upload_type." ";
$headers .= "name=\"".$upload_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$upload_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";
// SEND MAIL
$check = mail($to, $subject, $message, $headers);
echo $check;
fclose($fp);
if($check){
echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#333333; font-weight:bold">Mail sent please check inbox and spam both <br /></font>';
}
}
}
?>
I hope this will be helpful for all web developers.
Thank You
No comments:
Post a Comment