ActionMailer-从原始RFC822邮件中创建带有附件的新邮件

时间:2018-06-27 15:59:48

标签: ruby-on-rails ruby email actionmailer

我使用电子邮件传递服务发送电子邮件(Sparkpost),每次回复其中一封电子邮件时,我都会收到一个JSON,其中包含:回复邮件正文为HTML(body_html),回复邮件正文为文本(body_text) )和用于回复消息的原始RFC822(email_rfc822)。

收到此JSON后,我需要将此电子邮件转发给另一个收件人。目前,我使用以下邮件程序来实现这一目标:

class ReplyMailer < ApplicationMailer
  def reply(body_html, body_text, options = {})
    mail(to: options[:to], from: options[:from], reply_to: options[:reply_to], subject: options[:subject], skip_premailer: true) do |format|
      format.html { render html: body_html.html_safe } if body_html.present?
      format.text { render plain: body_text } if body_text.present?
    end
  end
end 

这种方法的问题在于它不转发原始邮件的附件。

如何更改此邮件程序以转发原始邮件中的所有附件(包括html正文上引用的内嵌图像)?

2 个答案:

答案 0 :(得分:1)

以下代码段可能会对您有所帮助

options[:attachments].each do |attachment| 
   attachments[attachment.original_filename] = File.read(attachment.tempfile)
end

答案 1 :(得分:0)

您可以使用类似的方法来处理内联附件

 context "Inline Attachments" do
it "accepts an inline attachment" do
  test_email = Mailer.test_email inline_attachments: 1
  @delivery_method.deliver!(test_email)

  images = @delivery_method.data[:content][:inline_images]
  expect(images.length).to eq(1)
  image = images[0]
  expect(image).to include(:name, :type=>"image/png; filename=image_0.png", :data=>"iVBORw0KGgoAAAANSUhEUgAAAfIAAACCCAMAAACKGrqXAAAAY1BMVEX////6ZCNVVVr6ZCP6ZCNVVVr6ZCNVVVr6ZCP6ZCNVVVpVVVr6ZCNVVVpVVVr6ZCNVVVpVVVr6ZCNVVVpVVVpVVVpVVVr6ZCP6ZCNVVVr6ZCP6ZCP6ZCP6ZCNVVVpVVVr6ZCO+CfRXAAAAH3RSTlMAgMBAEPBgoDAg4NBwkIDAUBDwQLBgMOCgIFCQ0LBwPgQxPgAADDFJREFUeF7s2kGLnEAYhOFqbERhSROHZFzXWer//8rAJo172jklhK73OXsrKItP9Q/h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