升级到Rails 3后发送带有邮件和附件的邮件时出现问题

时间:2011-03-02 19:52:31

标签: ruby-on-rails ruby-on-rails-3

我以前有这个代码用于发送邮件:

class MailTimerMailer < ActionMailer::Base
  def mail_schedule(from, to, cc, bcc, subject, message, files=[], sent_at = Time.now)
    @subject = subject
    @recipients = to
    @from = from
    @cc = cc
    @bcc = bcc
    @sent_on = sent_at
    @body["message"] = message
    @headers = {}

    # attache files
    files.each do |file|
      attachment file.mimetype do |a|
        a.body = file.binarydata
        a.filename = file.filename
      end
    end

  end
end

它不再有效。我没有查看我的邮件,因为完整的邮件来自我的方法之外。我试图将我的代码修改为Rails 3,如下所示:

class ScheduleMailer < ActionMailer::Base
  def mail_schedule(from, to, cc, bcc, subject, message, files=[], sent_at = Time.now)
    @subject = subject
    @recipients = to
    @from = from
    @cc = cc
    @bcc = bcc
    @sent_on = sent_at
    @body["message"] = message
    @headers = {}

    # attache files
    files.each do |file|
      attachments[file.filename] = File.read("public/data/" << file.id.to_s() << "." << file.extension)
    end

  end
end

此代码发送带附件的邮件,但邮件中没有实际邮件。它还给了我一个弃用警告“给主体散列哈希值,请改用实例变量”。我试过“body:message =&gt; message”,但没有运气。

我怎样才能让它再次运作?

谢谢

1 个答案:

答案 0 :(得分:3)

这是:

class MyMailer < ActionMailer::Base
  def mail_schedule(from, to, cc, bcc, subject, message, files=[], sent_at = Time.now)
    # attache files
    files.each do |file|
      attachments[file.filename] = File.read("public/data/" << file.id.to_s() << "." << file.extension)
    end

    mail(:from => from, :to => to, :cc => cc, :bcc => bcc, :subject => subject) do |format|
      format.text { render :text => message }
    end
  end
end