Steps on how PHP mail encryption system?

Asked By 0 points N/A Posted on -

Good day to all of you!

Recently, I am using PHP mail and now I was thinking if it's possible to encrypt a message or mail sent to me by using it.

I ask many people if they can help me. And unfortunately, none of them solve my problem.

So guys, can someone tell me what are the PHP mail encryption steps?

And comments would be appreciated.

Thank you and good day.

Hargrave Lawrence

Answered By 0 points N/A #170230

Steps on how PHP mail encryption system?


Hi Hargrave,

Yes it is possible to encrypt a message in PHP. There are two ways of doing this:

1) Using crypt () function

2) PGP based public key encryption

Crypt Function

The crypt function takes plaintext as input and then apply some inbuilt encryption algorithm and convert it into ciphertext .The crypt function performs one way encryption only that is once the plain text is encrypted .It cannot be decrypted again .This is widely used for password protection.


$hashed_password = crypt('mypassword');

if (crypt($user_input, $hashed_password) == $hashed_password) {

   echo "Password verified!";



PGP based public key encryption

PGP uses public key cryptography. PGP can perform both encryption and decryption. The user of public key has both public key and private key.Users share their public key to all other users to whom they want to communicate.If you want to send a message to user A then you need encrypt a message using user A public key. Once user A receives the message only he can decrypt it using his private key. There is no relation between private a public key.


            $from = "[email protected]";

            $to = "[email protected]";

            $messagebody = $_POST['msg'];

            $gpg_path = '/usr/local/bin/gpg';

            $home_dir = '/htdocs/www';

            $user_env = 'web';

            $cmd = "echo $message_body | HOME=$home_dir USER=$user_env $gpg_path" .

                        "–quiet –no-secmem-warning –encrypt –sign –armor " .

                        "–recipient $to –local-user $from";

                        $message_body = `$cmd`;

                        mail($to,'Message from Web Form', $message_body,"From:$fromrn");


PHP invokes /usr/local/bin/gpg (this location may vary on your server) to encrypt a message. Furthermore, setting the HOME and USER environment variables tells GPG where to look for the Keyrings on which these keys are stored. The other flags do the following:

–quiet and –no-secmem-warning suppress warnings from GPG.

–encrypt performs the encryption.

–sign adds a signature to verify the sender's identity.

Login/Register to Answer

Related Questions