<commit_before># Mail::Gpg [![Build Status](https://travis-ci.org/jkraemer/mail-gpg.png?branch=master)](https://travis-ci.org/jkraemer/mail-gpg)

This gem adds GPG/MIME encryption capabilities to the [Ruby Mail
Library](https://github.com/mikel/mail)

## Installation

Add this line to your application's Gemfile:

    gem 'mail-gpg'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install mail-gpg


## Usage

### Encrypting / Signing
Construct your Mail object as usual and specify you want it to be encrypted
with the gpg method:

    Mail.new do
      to '<EMAIL>'
      from '<EMAIL>'
      subject 'gpg test'
      body "encrypt me!"
      add_file "some_attachment.zip"

      # encrypt message, no signing
      gpg encrypt: true

      # encrypt and sign message with sender's private key, using the given
      # passphrase to decrypt the key
      gpg encrypt: true, sign: true, password: '<PASSWORD>'

      # encrypt and sign message using a different key
      gpg encrypt: true, sign_as: '<EMAIL>', password: '<PASSWORD>'


      # encrypt and sign message and use a callback function to provide the
      # passphrase.
      gpg encrypt: true, sign_as: '<EMAIL>',
          passphrase_callback: ->(obj, uid_hint, passphrase_info, prev_was_bad, fd){puts "Enter passphrase for #{passphrase_info}: "; (IO.for_fd(fd, 'w') << readline.chomp).flush }
    end.deliver


Make sure all recipients' public keys are present in your local gpg keychain.
You will get errors in case encryption is not possible due to missing keys.
If you collect public key data from your users, you can specify the ascii
armored key data for recipients using the `:keys` option like this:

    johns_key = <<-END
    -----BEGIN PGP PUBLIC KEY BLOCK-----
    Version: GnuPG v1.4.12 (GNU/Linux)

    mQGiBEk39msRBADw1ExmrLD1OUMdfvA7cnVVYTC7CyqfNvHUVuuBDhV7azs
    ....
    END

    Mail.new do
      to '<EMAIL>'
      gpg encrypt: true, keys: { '<EMAIL>' => johns_key }
    end

The key will then be imported before actually trying to encrypt/send the mail.
In theory you only need to specify the key once like that, however doing it
every time does not hurt as gpg is clever enough to recognize known keys, only
updating it's db when necessary.

You may also want to have a look at the [GPGME](https://github.com/ueno/ruby-gpgme) docs and code base for more info on the various options, especially regarding the `passphrase_callback` arguments.


### Signing only

This is not implemented yet


## Rails / ActionMailer integration

    class MyMailer < ActionMailer::Base<commit_msg>Added "signing only" paragraph to readme<commit_after># Mail::Gpg [![Build Status](https://travis-ci.org/jkraemer/mail-gpg.png?branch=master)](https://travis-ci.org/jkraemer/mail-gpg)

This gem adds GPG/MIME encryption capabilities to the [Ruby Mail
Library](https://github.com/mikel/mail)

## Installation

Add this line to your application's Gemfile:

    gem 'mail-gpg'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install mail-gpg


## Usage

### Encrypting / Signing
Construct your Mail object as usual and specify you want it to be encrypted
with the gpg method:

    Mail.new do
      to '<EMAIL>'
      from '<EMAIL>'
      subject 'gpg test'
      body "encrypt me!"
      add_file "some_attachment.zip"

      # encrypt message, no signing
      gpg encrypt: true

      # encrypt and sign message with sender's private key, using the given
      # passphrase to decrypt the key
      gpg encrypt: true, sign: true, password: '<PASSWORD>'

      # encrypt and sign message using a different key
      gpg encrypt: true, sign_as: '<EMAIL>', password: '<PASSWORD>'


      # encrypt and sign message and use a callback function to provide the
      # passphrase.
      gpg encrypt: true, sign_as: '<EMAIL>',
          passphrase_callback: ->(obj, uid_hint, passphrase_info, prev_was_bad, fd){puts "Enter passphrase for #{passphrase_info}: "; (IO.for_fd(fd, 'w') << readline.chomp).flush }
    end.deliver


Make sure all recipients' public keys are present in your local gpg keychain.
You will get errors in case encryption is not possible due to missing keys.
If you collect public key data from your users, you can specify the ascii
armored key data for recipients using the `:keys` option like this:

    johns_key = <<-END
    -----BEGIN PGP PUBLIC KEY BLOCK-----
    Version: GnuPG v1.4.12 (GNU/Linux)

    mQGiBEk39msRBADw1ExmrLD1OUMdfvA7cnVVYTC7CyqfNvHUVuuBDhV7azs
    ....
    END

    Mail.new do
      to '<EMAIL>'
      gpg encrypt: true, keys: { '<EMAIL>' => johns_key }
    end

The key will then be imported before actually trying to encrypt/send the mail.
In theory you only need to specify the key once like that, however doing it
every time does not hurt as gpg is clever enough to recognize known keys, only
updating it's db when necessary.

You may also want to have a look at the [GPGME](https://github.com/ueno/ruby-gpgme) docs and code base for more info on the various options, especially regarding the `passphrase_callback` arguments.


### Signing only

Just leave the the `:encrypt` option out or pass `encrypt: false`, i.e.

`
Mail.new do
	to '<EMAIL>'
	gpg sign: true
end.deliver 
`


## Rails / ActionMailer integration

    class MyMailer < ActionMailer::Base
{'fraction_non_alphanumeric': 0.09065262365995862, 'fraction_numerical': 0.004137671619334211, 'mean_word_length': 3.334148329258354, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 24, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6657242', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1603, 'n_words': 645}