在rails上的ruby上使用mail-gpg gem加密电子邮件正文

时间:2017-11-16 19:33:37

标签: ruby-on-rails ruby email encryption pgp

我试图在rails上使用mail-gpg ruby​​ gem来了解pgp加密,虽然我使它工作并且加密和解密都工作,但他们并不能完全满足我的解密pgp消息显示我不想发送的信息也正在加密。

代码:

  class EncryptMailer < ApplicationMailer
    def encrypted_mail user
        mail to: "#{user.email}", subject: 'Hopefully this is encrypted', content_type: 'text/plain', gpg: { encrypt: true, keys: { "#{user.email}" => user.pgp_key}}
    end
  end

邮件视图:

Welcome to example.com
You have successfully signed up to example.com.
To login to the site, just follow this link: example.com
Thanks for joining and have a great day!

解密后的输出

Date: Thu, 09 Nov 2017 15:44:26 -0500
From: from@example.com
To: to@example.com
Message-ID: <5a04be2ad575f_18982ab82f69def06728c@Personal_computer.mail>
Subject: Hopefully this is encrypted
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

    Welcome to example.com
    You have successfully signed up to example.com.
    To login to the site, just follow this link: example.com
    Thanks for joining and have a great day!

可见,加密的阻止消息最终会有一堆我不想要的额外信息。有没有办法只加密电子邮件的正文?或者我最好只加密邮件程序本身以外的邮件程序视图?

更新

在更好地了解gem的源代码之后,似乎这是作者的慎重选择,但是实现一个选项以使加密仅占用主体是很容易的。我已经提交了请求获取拉取请求权限的问题,但万一我从未批准过这样做。

在gem的源代码中(你可以找到here)转到encrypted_pa​​rt.rb文件并在加密变量分配

clean = options[:clean]
clean ? cleartext_mail = cleartext_mail.body : cleartext_mail

那就是它!您现在可以像这样调用gpg调用中的 clean 选项。

def encrypted_mail user
    mail to: "#{user.email}", subject: 'Hopefully this is encrypted', content_type: 'text/plain',  gpg: { encrypt: true, clean: true keys: { "#{user.email}" => user.pgp_key}
end

0 个答案:

没有答案