如何让rails和mail-gpg实际使用actionmailer加密消息?

时间:2015-08-13 17:41:55

标签: ruby-on-rails email encryption gnupg

我现在已经尝试了几天让Rails和mail-gpg实际加密电子邮件。我显然缺少一些东西。任何想法都将不胜感激。

这是我的代码......

class ExampleMailer < ActionMailer::Base
  default from: "applerocks@gmail.com"

  def sample_email(to_email_address)

    applerocks_gmail_com_public_key = <<-END
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1

mQENBFXI/dEBCAC93lu3MnTTJx1mTy67t11...
=bSQdF5z+byVKbSb90ogG9XfPYA==...
... Removed for security

-----END PGP PUBLIC KEY BLOCK-----
END

    Mail.new do
      to 'applerocks@gmail.com'
      gpg encrypt: true, keys: { 'applerocks@gmail.com' => applerocks_gmail_com_public_key }
    end

    @to_email_address = to_email_address
    mail to: @to_email_address, subject: 'Sample Email', gpg: { encrypt: true, sign_as: 'applerocks@gmail.com', password: 'applerocks' }

  end
end

谢谢,

克里斯

1 个答案:

答案 0 :(得分:0)

首先,您必须在applerocks_gmail_com_public_key中包含您的私钥,只需将其堆叠在公钥之后。

之后你需要接收者证书,你可以这样做:

hkp = Hkp.new
to_email_address_publ_key = hkp.search(to_email_address)
to_email_address_cert = hkp.fetch(to_email_address_publ_key[0][0])

删除此部分:

Mail.new do
  to 'applerocks@gmail.com'
  gpg encrypt: true, keys: { 'applerocks@gmail.com' => applerocks_gmail_com_public_key }
end

然后将结束部分更改为:

mail to: to_email_address, subject: 'Sample Email', gpg: { encrypt: true, sign_as: 'applerocks@gmail.com', password: 'applerocks',keys: { 'applerocks@ gmail.com' => applerocks_gmail_com_public_key, to_email_address => to_email_address_cert}}

我希望它有所帮助。