Rails 3.2电子邮件附件在正文中显示为文本

时间:2015-01-20 10:26:49

标签: ruby-on-rails email ruby-on-rails-3.2 actionmailer email-attachments

我的Rails 3.2应用程序中有一个电子邮件客户端,用于在电子邮件中存储电子邮件。使用tinyMCE保存电子邮件的内容,并使用Paperclip保存附件。这些电子邮件是使用我创建的邮件程序发送的,如下所示:

class MessageMailer < ActionMailer::Base   
  def messaging_message(msg)
    begin
      Message.transaction do
        @msg = msg

        msg.attached_files.each do |attached_file|
          attachments[attached_file.file_file_name] = File.read("#{Rails.root.to_s}/public/attachments/messaging/messages/#{msg.id}/#{attached_file.id}/#{attached_file.file_file_name}")
        end

        mail_msg = mail(
          to:           Message.convert_to_mail_addresses(msg.to_recipients), # This converts the list of Recipient objects into "emai@address.com; email2@address.com" format
          cc:           Message.convert_to_mail_addresses(msg.cc_recipients),
          bcc:          Message.convert_to_mail_addresses(msg.bcc_recipients),
          from:         msg.access_department.email,
          subject:      msg.subject,
          body:         msg.body,
          content_type: "text/html"
        )

        # Sets the user_name, password and domain based on the FROM address the user selected in the app
        mail_msg.delivery_method.settings.merge!(msg.access_department.mail_settings)
      end
    rescue Exception => e
      Rails.logger.error "MODEL: '#{class_name}' - METHOD: '#{method_name}'"
      Rails.logger.error " -> ERROR: #{e}"
      Rails.logger.error "BACKTRACE:"
      Rails.logger.error "#{e.backtrace.join("\n ")}"
      return false
    else
      return true
    end
  end
end

然后我使用以下代码运行此操作:

msg = Message.find(20)
MessageMailer.messaging_message(m).deliver

没有附件,这就像一个魅力。但是,当我有附件时,它们会在电子邮件正文中显示为一长串字符,即mime类型的文本版本。这是一个例子:

  

---- == _ mimepart_54be256d7d18_13ad78fe74390bc Content-Type:text / plain; charset = UTF-8 Content-Transfer-Encoding:7bit

     

Lorem ipsum dolor sit amet,per quot antiopam elaboraret cu,ei epicuri   perfecto有。 Ut utinam discere legimus vis,quidam habemus   menandri nec。 Eam dolores在他的身上肆意挥霍。   Qui et augue endingemque,ut sea doctus impetus inermis。简历   semper molestiae id mea,est corpora prodesset referrentur ex。在梅尔   diceret expetendis。 Mea te推动vivendum interesset,erroribus   referrentur mea ex。

     

Possit ornatus labores teos。 Per id unum mucius insolens,ne quo   elitr ludus nusquam。 Amet possit persius eam cu。亲没有nostro   nominati。 Ut zril persecuti eum,eu ius graece tempor,agam   mediocrem disputationi est。

     

我的个人签名

     

---- == _ mimepart_54be256d7d18_13ad78fe74390bc Content-Type:image / jpeg;字符集= UTF-8;文件名= Side_rolling_bulk_vessels_Klein.jpg   Content-Transfer-Encoding:base64 Content-Disposition:attachment;   filename = Side_rolling_bulk_vessels_Klein.jpg内容ID:   &LT; 54be256c7da70_13ad78fe74389af@Theo-Ubuntu-Laptop.mail>   / 9J / 4AAQSkZJRgABAgEASABIAAD / 4RZ1RXhpZgAATU0AKgAAAAgABwESAAMA   AAABAAEAAAEaAAUAAAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAIAAAEx   AAIAAAAbAAAAcgEyAAIAAAAUAAAAjYdpAAQAAAABAAAApAAAANAAAABIAAAA   AQAAAEgAAAABQWRvYmUgUGhvdG9zaG9wIENTIFdpbmRvd3MAMjAwOTowMzoy   NCAxMDo0NDo1OAAAAAAAA6ABAAMAAAAB // 8AAKACAAQAAAABAAABVKADAAQA   AAABAAAB4wAAAAAAAAAGAQMAAwAAAAEABgAAARoABQAAAAEAAAEeARsABQAA   AAEAAAEmASgAAwAAAAEAAgAAAgEABAAAAAEAAAEuAgIABAAAAAEAABU / AAAA   AAAAAEgAAAABAAAASAAAAAH / 2P / gABBKRklGAAECAQBIAEgAAP / tAAxBZG9i   ZV9DTQAC / + 4ADkFkb2JlAGSAAAAAAf / bAIQADAgICAkIDAkJDBELCgsRFQ8M   DA8VGBMTFRMTGBEMDAwMDAwRDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM   DAENCwsNDg0QDg4QFA4ODhQUDg4ODh ........

我尝试重新排序代码,在here gem中以Mail显示的格式重写代码,以及使用mime类型显式添加附件并对自己进行编码,但我仍然得到同样的结果。

任何人都可以解释我出错的地方吗?

2 个答案:

答案 0 :(得分:2)

我遇到了与上述相同的问题。

只需从代码中删除行content_type: "text/html"即可。

Rails会自动发送包含附件的多部分电子邮件,正确嵌套顶级为multipart/mixed,第一部分为multipart/alternative,其中包含纯文本和HTML电子邮件。

有关详细信息,请参阅Rails - Sending Emails with Attachments

答案 1 :(得分:1)

经过数小时的搜索和试用后,错误,我设法找到我的代码出了什么问题。

问题不在于我添加附件的方式,而在于我添加电子邮件的正文的方式。电子邮件的结构如下(这非常简单):

  • multipart/mixed
    • multipart/alternative
      • text/html
      • text/plain
    • image/png
    • application/pdf
    • ....

我不确定原因,但我猜测通过直接添加电子邮件的content_type,我在创建此电子邮件时覆盖了一些默认设置。此外,msg.contents返回了HTML。

由于我感觉我的问题出在content_type,我尝试为我的内容创建一个视图。所以我现在有了以下内容:

寄件人/ message_mailer.rb

class MessageMailer < ActionMailer::Base   
  def messaging_message(msg)
    begin
      Message.transaction do
        @msg = msg

        msg.attached_files.each do |attached_file|
          attachments[attached_file.file_file_name] = File.read("#{Rails.root.to_s}/public/attachments/messaging/messages/#{msg.id}/#{attached_file.id}/#{attached_file.file_file_name}")
        end

        mail_msg = mail(
          to:       Message.convert_to_mail_addresses(msg.to_recipients), # This converts the list of Recipient objects into "emai@address.com; email2@address.com" format
          cc:       Message.convert_to_mail_addresses(msg.cc_recipients),
          bcc:      Message.convert_to_mail_addresses(msg.bcc_recipients),
          from:     msg.access_department.email,
          subject:  msg.subject
        )

        # Sets the user_name, password and domain based on the FROM address the user selected in the app
        mail_msg.delivery_method.settings.merge!(msg.access_department.mail_settings)
      end
    rescue Exception => e
      Rails.logger.error "MODEL: '#{class_name}' - METHOD: '#{method_name}'"
      Rails.logger.error " -> ERROR: #{e}"
      Rails.logger.error "BACKTRACE:"
      Rails.logger.error "#{e.backtrace.join("\n ")}"
      return false
    else
      return true
    end
  end
end


应用程序/争夺/ message_mailer / messaging_message.html.haml

!!!
%html
  %head
    %meta{content: "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}
  %body
    = @msg.contents.html_safe


现在,当我运行以下内容时:

msg = Message.find(20)
MessageMailer.messaging_message(m).deliver

一切正常,我收到了一封包含可下载附件的电子邮件!

我猜测的是,现在我让Mail gem处理我的电子邮件的创建,而不是自己设置内容类型。

希望这有助于遇到同样问题的其他人,因为我发现这个问题的帮助很少。