Method 1
<form action="" enctype="multipart/form-data" method="post"><input type="file" name="attc">
<input type="submit" name="sub" value="send" />
</form>
<?php
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$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";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
echo "mail send ... Successfully";
} else {
echo "ERROR!";
}
}
if(isset($_POST['sub']))
{
$filename = $_FILES['attc']['name'];
move_uploaded_file($_FILES['attc']['tmp_name'],"file_attached/".$filename);
$path = "file_attached/";
$mailto = "your-email@gmail.com";
$from_mail = "from@gmail.com";
$from_name = "from_name";
$replyto = "reply@gmail.com";
$subject = "new testing";
$message = "Please find attached file";
echo mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message);
}
?>
Method 2
<?php
$from_name = filter_var($_REQUEST["from_name"], FILTER_SANITIZE_STRING);
$from_email = filter_var($_REQUEST["from_email"], FILTER_SANITIZE_STRING);
$recipient_email = filter_var($_REQUEST["to_email"], FILTER_SANITIZE_STRING);
$subject = filter_var($_REQUEST["subject"], FILTER_SANITIZE_STRING);
$body = $_REQUEST["body"];
$attachments = $_FILES['my_files'];
//$file_count = count($attachments['name']); //count total files attached
$boundary = md5("https://www.gstforumindia.in");
//construct a message body to be sent to recipient
$message_body = "Message from $from_name\n";
$message_body .= "------------------------------\n";
$message_body .= "$body\n";
$message_body .= "------------------------------\n";
$message_body .= "$from_name\n";
$message_body .= "$from_email\n";
if(!empty($attachments))
{ //if attachment exists
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Reply-To: ".$from_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//message text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/html; charset=UTF-8\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message_body));
//attachments
if(!empty($attachments['name'])){
if($attachments['error']>0) //exit script and output error if we encounter any
{
$mymsg = array(
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder" );
print $mymsg[$attachments['error']];
exit;
}
//get file info
$file_name = $attachments['name'];
$file_size = $attachments['size'];
$file_type = $attachments['type'];
//read file
$handle = fopen($attachments['tmp_name'], "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
}
}else{ //send plain email otherwise
$headers = "From:".$from_email."\r\n".
"Reply-To: ".$from_email. "\n" .
"X-Mailer: PHP/" . phpversion();
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$body = $message_body;
}
if(mail($recipient_email, $subject, $body, $headers))
{
$json['status'] = 'TRUE';
$json['message'] = 'Thanks for contact us.';
}else
{
$json['status'] = 'FALSE';
$json['message'] = 'Somthing wrong Please retry.';
}
echo json_encode($json,JSON_UNESCAPED_SLASHES);
?>
No comments:
Post a Comment