Sunday, February 21, 2010

PHP - Mail With Attachment

//Form's file field name is 'file'



{
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

//uncomment these if wants to save the file in the server

// $save_path="cv\\";
$file=$_FILES['file'];
//if($file !="" && isset($_POST["file"]))
//{
//if(isset($save_path) && $save_path!="")
//{
// $name = split('/',$file['name']);
// $filenaemNew1 = date("y").date("m").date("j").date("G").date("i").$file['name'];
// move_uploaded_file($file['tmp_name'], $save_path . $filenaemNew1);
//}
//}
//else
//{
//$filenaemNew1 = "";
//}


$tmp_name = $_FILES['file']['tmp_name'];
$type = $_FILES['file']['type'];
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$from = "hr@jobsjobs.lk";
$message =
"Testing email";

echo "charitha";
if (file_exists($tmp_name))
{
if(is_uploaded_file($tmp_name))
{
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
}

$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/form-data;\r\n" .
" boundary=\"{$mime_boundary}\"";

$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" pet_photo=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

$to = "charithar@eurekasl.com";
$subject = "Testing Mail";


$mail_sent = @mail( $to, $subject, $message, $headers );

echo "ok";



}
}

?>

No comments:

Post a Comment