【问题标题】:Rails 4/5 Sending Dynamic ActionMailer::Base.mail email With Attachment labeled NonameRails 4/5 发送动态 ActionMailer::Base.mail 电子邮件,附件标记为 Noname
【发布时间】:2017-02-22 15:52:15
【问题描述】:

我看过类似的帖子,这些帖子主要通过创建视图和控制器来处理发送附件,例如:

PDF attachment in email is called 'Noname'

但我有一个在后台动态生成文件的进程,需要使用 ActionMailer::Base.mail 将其附加到收件人列表中。下面是代码:

def send_email(connection)
    email = ActionMailer::Base.mail(to: connection['to'], from: connection['from'], subject: 'Sample File', body: "<p>Hello,</p><p>Your data is ready</p>", content_type: 'multipart/mixed')
    email.cc = connection['cc'] if connection['cc'].present?
    email.bcc = connection['bcc'] if connection['bcc'].present?
    @files.each do |file|
      report_file_name = "#{@start_time.strftime('%Y%M%dT%I%m%s')}_#{file[0]}.xlsx"
      file_location = "#{Rails.root}/tmp/#{report_file_name}"
      email.attachments[report_file_name] = File.open(file_location, 'rb'){|f| f.read}
    end
    email.deliver if email
  end

我可以在日志中看到它与内容一起发送,但假设它是作为 Noname 发送的,因为它找不到视图。有什么方法可以让它成功运行?

下面是示例输出:

发送邮件至 sample@sample.com (383.9ms) 日期: 2016 年 10 月 13 日星期四 08:47:30 -0400 来自:样品至: 收件人消息 ID: 主题:示例文件 Mime 版本:1.0 内容类型:多部分/混合;字符集=UTF-8 内容传输编码:7bit

-- 内容类型:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; 文件名=20161012T08101476259208_Data.xlsx 内容传输编码:base64 内容处置:附件; 文件名=20161012T08101476259208_Data.xlsx 内容 ID:

UEsDBBQAAAAIAO.. ... ...ADUFQAAAAA=

更新 - 我注意到如果我使用 email.content_type = 'text/plain' - 附件会成功通过。对我来说,这很有效,但我很高兴以后能够使用 HTML 设置我的电子邮件的样式

我认为这是可行的,因为它会阻止 Rails 进行通常的收集/自动解释过程。不过,我当然希望看到多部分/混合或 html 兼容版本在这里工作。

更新 2 这只是人为地修复了 rails_email_preview gem 中的问题,它将电子邮件呈现到开发中的新选项卡。在生产中,这可以简单易懂地打印详细信息和可能是 base64 编码的文件,因此问题仍然悬而未决。

【问题讨论】:

  • 我现在也有同样的问题?有什么关于如何修复它的消息吗?

标签: ruby-on-rails excel email ruby-on-rails-4


【解决方案1】:

我也遇到过这个问题,经过一番调查,似乎在Rails 4中,调用ma​​il方法后不能再调用attachments方法,否则content_type的邮件消息对象将没有边界信息,因此无法在收到的电子邮件中正确解析附件部分。

我认为深入研究 actionmailer 源代码,您应该能够找到解决方案,方法是覆盖默认的 ma​​il 方法或手动设置正确的边界信息。

但是为了快速解决这个问题,我想出了一个使用元编程的不优雅的解决方法:定义一个继承 ActionMailer::Base 的委托类。

class AnyMailer < ActionMailer::Base
  # a delegation mailer class used to eval dynamic mail action
end

然后通过定义一个任意方法来执行电子邮件发送来评估这个类。

def send_email(connection, files)
  AnyMailer.class_eval do
    def any_mailer(connection, files)
      files.each do |file|
        report_file_name = :foo
        file_location = :bar
        attachments[report_file_name] = File.open(file_location, 'rb'){|f| f.read}
      end
      mail(to: connection['to'], from: connection['from'], subject: 'Sample File', body: "<p>Hello,</p><p>Your data is ready</p>")
    end
  end

  AnyMailer.any_mailer(connection, files).deliver_now
end

注意,不需要将 content_type 指定为 'multipart/mixed',ActionMailer 会正确处理。我试图明确地指定它,但是却弄乱了电子邮件内容。

【讨论】:

    【解决方案2】:

    这让我发疯了。 如果您使用邮件程序视图,请确保您有一个格式正确的 .html 模板和一个 .text 模板。

    其中任何一个中的最小错误都会将整个电子邮件呈现为无名附件。

    【讨论】:

      猜你喜欢
      • 2014-12-31
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多