Monday, August 11, 2008

File uploading and send into email using PHP

/*
Program : File Uploading and Mail Sending Process
*/

/*
1. Initially upload the file into "mail_upload" folder
2. So, we must changed the folder permission of "mail_upload" into 777 (write permission)
3. Sending mail with the File Attachment

Note : Change the Below listed variable values.. for your SETTINGS resp.

Line No: 31 ( $whereto )
Line No: 63 ( $mailto )
Line No: 64 ( $from_mail )
Line No: 65 ( $from_name )
Line No: 66 ( $replyto )
Line No: 67 ( $subject )
Line No: 68 ( $message )

*/

$error = ""; // Set a variable that will be used for errors

if(isset($_POST['upload']) && $_POST['upload'] == 'Send into Mail') // Form is submitted
{
$whereto = "/home2/nasc/dev/ingrams/mail_upload"; // Gets post value from select menu

$whatfile = $_FILES['uploadedfile']['name']; // Gets file value from file upload input

if(empty($whereto)) // Checks to see if $whereto is empty, if so echo error
$error = "You need to choose a directory.
";

if($whatfile == NULL) // Checks to see if file input field is empty, if so throw an error
$error .= "You need to choose a file.";

if(!empty($whereto) && $whatfile != NULL) //if no errors so far then continue uploading
{
$target_path = "$whereto/"; // The directory the file will be placed

/* Add the original filename to our target path. Result is "uploads/filename.extension" */
$target_path = $target_path . basename($whatfile);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
echo "
Uploaded Success : ".basename($whatfile);

$file_name = basename($whatfile);
$file_path = $whereto."/";
mail_attachment ($file_name, $file_path);
}
else /* if there was a problem then throw an error */
$error .= "There was an error uploading the file, please try again!";
}
}

function mail_attachment($filename, $path) {
global $error;

$mailto = "mano_ilayans@yahoo.com";
$from_mail = "manoilayans@gmail.com";
$from_name = "Manokaran";
$replyto = "manoilayans@gmail.com";
$subject = "File Uploading and Mailing Process";
$message = "Message of File Uploading and Mailing Process";

$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"; // use diff. tyoes here
$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 "
2. Mail send ...

OK

"; // or use booleans here
} else {
$error .= "
Mail send ...

ERROR!";
}
}
?>

No comments:

Popular Posts