title: PHP email encryption
body: <p>I have a website with an SSL.</p>
  
  <p>I would like to encrypt outgoing emails from my server. I've been digging around at this and I really don't know where to begin.</p>
  
  <p>Here is my PHP email script so you have an idea of what I'm using:</p>
  
  <pre><code>public function email($to, $title, $message){
      $from = "angela@mysite.com";
      $headers = "From: {$from}\r\n";
      $headers .= "X-Confirm-Reading-To: {$from}\r\n";
      $headers .= "Reply-To: {$from}\r\n";
      $headers .= "Organization: InfiniSys, inc.\r\n";
      $headers .= "MIME-Version: 1.0\r\n";
      $headers .= "Content-type: text/html; charset=ISO-8859-1\r\n";
      $headers .= "X-Priority: 3\r\n";
      $headers .= "X-Mailer: PHP". phpversion() ."\r\n";
  
      $subject = $title;
      mail($to, $subject, $message, $headers);
  }
  </code></pre>
  
  <p>Ubuntu 14.04</p>
  
  <p>I'm not sure if this is a server setting or programming config.</p>
  
  <p>Very interesting post: (can't remember where I got it)</p>
  
  <pre><code>&lt;?php
  // Setup mail headers.
  $headers = array("From" =&gt; "someone@example.com",
      "To" =&gt; "someone-else@example.com",
      "Cc" =&gt; "spam@somewhere.org",
      "Subject" =&gt; "Encrypted mail readable with most clients",
      "X-Mailer" =&gt; "PHP/".phpversion()
  );
  
  // Get the public key certificate.
  $pubkey = file_get_contents("C:\test.cer");
  
  // Remove some double headers for mail()
  $headers_msg = $headers;
  unset($headers_msg['To'], $headers_msg['Subject']);
  $data = &lt;&lt;
  
  This email is Encrypted!
  
  You must have my certificate to view this email!
  
  Me
  
  EOD;
  
  //write msg to disk
  $fp = fopen("C:\msg.txt", "w");
  fwrite($fp, $data);
  fclose($fp);
  
  // Encrypt message
  openssl_pkcs7_encrypt("C:\msg.txt","C:\enc.txt",$pubkey,$headers_msg,PKCS7_TEXT,1);
  
  // Seperate headers and body for mail()
  $data = file_get_contents("C:\enc.txt");
  $parts = explode("\n\n", $data, 2);
  
  // Send mail
  mail($headers['To'], $headers['Subject'], $parts[1], $parts[0]);
  
  // Remove encrypted message (not fot debugging)
  //unlink("C:\msg.txt");
  //unlink("C:\enc.txt");
  ?&gt;
  </code></pre>