使用ActionMailer发送XML附件时出现意外的换行符

Fáb*_*sta 5 ruby-on-rails ruby-on-rails-3

我的应用程序存储了很多XML文件.后台作业会定期将某些XML文件发送到特定邮箱.邮件代码很简单:

class MailSender < ActionMailer::Base
    default :from => AppConfig.mail_from

    smtp_settings :address => AppConfig.smtp_host,
                  :username => AppConfig.smtp_user,
                  :password => AppConfig.smtp_pass

    def send_xml(record)
        f = record.filename.gsub("\\", "/") # converts \ to /
        f_short = arq.gsub(/.*\//, "") # extracts only the filename
        f_phys = "#{AppConfig.xml_root}#{arq}" # builds the physical filename

        headers["Return-Receipt-To"] = AppConfig.return_receipt

        attachments[f_short] = File.read(f_phys) if File.exists?(f_phys)

        mail :subject => "...",
             :to => AppConfig.mail_to
    end
end
Run Code Online (Sandbox Code Playgroud)

但是,由于某些原因,这些XML在传输时会被破坏:第945行会添加第一个换行符,第990行会添加以下内容.每次中断后,都会插入一个空格.我认为图片说明了一切:

col 1                                       col 990
|.................................................|
<?xml version="1.0"  ...  </IE><IM>321505493301<
 /IM><CNAE>4744001<  ...  00</pCOFINS><vCOFINS>0.00
 </vCOFINS></COFINS  ...  /prod><imposto><ICMS><ICM
 S40><orig>0</orig>  ...  <infAdic><infCpl>Permite 
Run Code Online (Sandbox Code Playgroud)

我试着打电话给File.read自己rails console,它工作正常,没有添加换行符.所以我认为错误应该在于ActionMailer.有小费吗?

编辑以供澄清:大多数XML文档都位于一条大的单行上.我无法更改它,因为XML是经过数字签名的 - 任何更改(包括添加换行符和缩进)都会破坏数字签名.

Fáb*_*sta 3

回答给我“Thumbleweed”徽章的问题:)

我最终自己对文件进行了编码,现在工作正常:

attachments[f_short] = {
  :encoding => 'base64',
  :content => Base64.encode64( File.read(f_phys) ).chomp
} if File.exists?(f_phys)
Run Code Online (Sandbox Code Playgroud)