/ Published in: PHP
Simple script utilising the `gpg_encrypt.php` code (`http://business-php.com/opensource/gpg_encrypt/`) that will encrypt both the body and attachment of an email on upload.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<html><body> <form action="<?php echo($_SERVER[REQUEST_URI]);?>" method="post" enctype="multipart/form-data"> <textarea name="message" cols="30" rows="6"><?php echo($_POST[message]);?></textarea> <br /> <label for='email_file'>Select A File To Email:</label> <input type="file" name="email_file"> <br /> <input type="submit" value="Encrypt and send your message"> <hr> <?php $message = $_POST[message]; //Settings $max_allowed_file_size = 5120; // size in KB default 5meg 5120 $gpglocation = '/usr/bin/gpg'; $gnupgp = '/home/user/.gnupg'; $keyid = 'XXXXXXXXXXXXXXXXXXX'; // see: http://business-php.com/opensource/gpg_encrypt/ /** ----------- End of config --------------- **/ // pgp_encrypt.php see: http://business-php.com/opensource/gpg_encrypt/ require_once('gpg_encrypt.php'); //Get the email file information //get the file extension of the file $size_of_email_file = $_FILES["email_file"]["size"]/1024;//size in KBs //Validations if($size_of_email_file > $max_allowed_file_size ) { $errors .= "\n Size of file should be less than $max_allowed_file_size"; } //------ Validate the file extension ----- $allowed_ext = false; { { $allowed_ext = true; } } if(!$allowed_ext) { $errors .= "\n The email file is not supported file type. ". } // Obtain file upload vars $tmp_name = $_FILES['email_file']['tmp_name']; // Generate a boundary string $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // Add the headers for a file attachment $headers .= "MIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; // Add a multipart boundary above the plain message $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"; // Base64 encode the file data // Add file attachment to the message $message .= "--{$mime_boundary}\n" . "Content-Type: {$type_of_email_file};\n" . " name=\"{$name_of_email_file}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$name_of_email_file}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; } $gpg = gpg_encrypt("${message}", $gpglocation , $gnupgp, $keyid); if("$gpg[2]" == '0') { echo('Your message was encrypted and sent'); } else { echo("<pre>\n$gpg[1]</pre><br /><div>$errors</div>"); } } ?> </body> </html>