gpg在上传时加密消息和文件,然后发送电子邮件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了gpg在上传时加密消息和文件,然后发送电子邮件相关的知识,希望对你有一定的参考价值。
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.
<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 .= " 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 .= " 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 " . "Content-Type: multipart/mixed; " . " boundary="{$mime_boundary}""; // Add a multipart boundary above the plain message $message = "This is a multi-part message in MIME format. " . "--{$mime_boundary} " . "Content-Type: text/plain; charset="iso-8859-1" " . "Content-Transfer-Encoding: 7bit " . $message . " "; // Base64 encode the file data // Add file attachment to the message $message .= "--{$mime_boundary} " . "Content-Type: {$type_of_email_file}; " . " name="{$name_of_email_file}" " . "Content-Disposition: attachment; " . " filename="{$name_of_email_file}" " . "Content-Transfer-Encoding: base64 " . $data . " " . "--{$mime_boundary}-- "; } $gpg = gpg_encrypt("${message}", $gpglocation , $gnupgp, $keyid); if("$gpg[2]" == '0') { echo('Your message was encrypted and sent'); } else { echo("<pre> $gpg[1]</pre><br /><div>$errors</div>"); } } ?> </body> </html>
以上是关于gpg在上传时加密消息和文件,然后发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章