if($_POST["Submit"]=='Upload')
{

/* GET File Variables */
$tmpName = $_FILES['file']['tmp_name'];
$fileType = $_FILES['file']['type'];
$fileName = $_FILES['file']['name'];

$message .= "Dear name,";
$message .= "\r\n\n";
$message .= "Please find the attachment file.";
$message .= "\r\n\n";
$message .= "Thanks!";

if (file($tmpName) && !empty($fileName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);

/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";

/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";

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

/* Encoding file data */
$data = chunk_split(base64_encode($data));

/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";

$result = mail("dinesh@example.com",$subject,$message,$headers);

if($result){
drupal_set_message(t('Message sent successfully'), 'status');
}
else{
drupal_set_message(t('Mail could not sent due to some technical problem'), 'error');
}

}

}

Advertisements