Location: PHPKode > scripts > GPGsend > gpgsend/index.php
<?php
/*
GPGsend - v0.1.1 - Tony Awtrey (hide@address.com)
This script is licensed under the GNU Public License:
(http://www.gnu.org/copyleft/gpl.html)

GPGSend is a script to send a GnuPG encrypted email from a form. This script
requires the following:

Apache (with mod_action)
PHP running as a CGI
CGIwrap or SuExec
GnuPG

An .htaccess file needs to be created in the directory to specify the PHP
script is to be wrapped:

AddType application/x-httpd-wphp php
Action application/x-httpd-wphp /cgi-sys/php-cgiwrap/username/php.cgi

The next thing to look at is the $to variable as it needs to be pointed to the
right email address.

Lastly, the $commandline to run gpg needs to be modified. Change the 'username'
in the homedir path to match the username specified in the .htaccess file
CGIwrap action. Also specify the 'Full Username' that identifies the key to use
to encrypt the message body.

*/

////////////////////
// Specify debug mode or not
////////////////////
$debug = 0;

////////////////////
// Check if we are posting
////////////////////
if ($REQUEST_METHOD=='POST') {

  ////////////////////
  // Check if the form is complete
  ////////////////////
  if ($name && $email && $subject && $body) {

    ////////////////////
    // Prep the variables for the mail() function
    ////////////////////
    $to      = "hide@address.com";
    $from    = "From: $name <$email>";
    $subject = "$subject";

    ////////////////////
    // Clean the var we are passing to the command line
    ////////////////////
    $body = escapeshellcmd($body);

    ////////////////////
    // Encrypt it!
    ////////////////////
    $commandline = "echo $body | /usr/bin/gpg --homedir /home/username/.gnupg -a -r 'Full Username' -e -";
    if ($debug==1) {
      system($commandline);
    } else {
      exec($commandline,$encrypted);
    }

    ////////////////////
    // Make the result array a string
    ////////////////////
    if (is_array($encrypted)) {
      $encrypted = implode($encrypted,"\n");
    }

    ////////////////////
    // Either output the debug info or mail it
    ////////////////////
    if ($debug==1) {
      echo "<!-- $commandline -->\n";
      echo "<!-- To: $to -->\n";
      echo "<!-- $from -->\n";
      echo "<!-- Subject: $subject -->\n";
      echo "<!-- $encrypted -->\n";
    } else {
      if ($encrypted) {
        mail($to,$subject,$encrypted,$from);
      } else {
        $notenc = 1;
      }
    }

  } else {

    $notall = 1;

  }

}
?>
<HTML>
<HEAD>
<TITLE>GPGsend</TITLE>
</HEAD>
<BODY BGCOLOR="white">

<H1>GPGSend</H1>

<? if ($notall==1) { ?>
<P><FONT COLOR="red">Please fill out the form completely</FONT></P>
<? } ?>
<? if ($notenc==1) { ?>
<P><FONT COLOR="red">The encryption failed.</FONT></P>
<? } ?>

<? if ($encrypted) { ?>

<H1>Thank you!</H1>

<P>
Your information has been sent.
</P>

<? } else { ?>

<FORM METHOD="post" ACTION="gpgsend.php">
<PRE>
Name:    <INPUT NAME="name" VALUE="<? echo $name; ?>">
Email:   <INPUT NAME="email" VALUE="<? echo $email; ?>">
Subject: <INPUT NAME="subject" VALUE="<? echo $subject; ?>">
Message:
<TEXTAREA ROWS="8" COLS="60" NAME="body">
<? echo $body; ?>
</TEXTAREA>
<INPUT TYPE="submit">
</PRE>
</FORM>

<? } ?>

</BODY>
</HTML>
Return current item: GPGsend