【问题标题】:difference between console email and rake email attachments控制台电子邮件和 rake 电子邮件附件之间的区别
【发布时间】:2013-04-29 21:00:13
【问题描述】:

我正在通过 wicket_pdf 生成发票 pdf,这很有效。此 pdf 将在后续处理步骤中用作电子邮件的附件。

# user.rb
def send_last_invoice_email!(invoice)
  UserMailer.send_actual_invoice_email(self, invoice).deliver
end

# user_mailer.rb
def send_actual_invoice_email(user, invoice)
  @user = user
  @invoice = invoice
  to = @user.email
  # THIS IS THE PROBLEMATIC LINE
  attachments["#{@invoice.file_name}"] = File.read(@invoice.file_name_path)
  mail(:to => to)
end

# console => works
user = User.where("step_id = 1 AND status = 0").last
invoice = user.invoices.last
user.send_last_invoice_email!(invoice)

# rake task => doesn't work
desc "send email for first user for step_id => 1 testing purpose"
task :send_mail => :environment do
  user = User.where("step_id = 1 AND status = 0").last
  invoice = user.invoices.last
  user.send_last_invoice_email!(invoice)
end

当我调用 RAKE TASK 时,pdf 为空。我已经调试过了,文件名存在,原始pdf文件没问题,但只有电子邮件中附加的pdf是空的。

我在开发模式下使用 letter_opener (1.0.0)。

有人知道,为什么控制台和通过 rake 任务执行相同的代码有区别吗?以及如何调试这种差异?

【问题讨论】:

    标签: ruby-on-rails-3 rake email-attachments rails-console


    【解决方案1】:

    发现了问题。这真的很奇怪。我在十六进制编辑器中比较两个 pdf 时找到了解决方案。

    在 rake 中,我必须将 ruby​​ 标准编码设置为 utf-8

    # users.rake 
    namespace :users do
      Encoding.default_external = Encoding::UTF_8
      Encoding.default_internal = Encoding::UTF_8
      ...
    

    我希望其他人可以从我 2 天的努力中受益:-)。 干杯

    【讨论】:

      猜你喜欢
      • 2021-12-24
      • 2016-11-30
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 2016-12-17
      • 1970-01-01
      • 2017-10-09
      相关资源
      最近更新 更多