代码之家  ›  专栏  ›  技术社区  ›  Fábio Batista Raza Ahmed

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

  •  5
  • Fábio Batista Raza Ahmed  · 技术社区  · 14 年前

    我的应用程序存储了很多XML文件。后台作业定期将其中一些XML文件发送到特定邮箱。mailer代码非常简单:

    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
    

    但是,由于某种原因,这些XML在传输时被破坏:第一个换行符被添加到第987列,下面的被添加到第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 
    

    我打过电话 File.read 我自己上 rails console ,工作正常,不添加换行符。所以我认为错误在于 ActionMailer . 有什么建议吗?

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

    1 回复  |  直到 14 年前
        1
  •  3
  •   Fábio Batista Raza Ahmed    14 年前

    回答给我“拇指草”徽章的问题:)

    最后我自己给文件编码,现在一切正常:

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