开发者

Problem sending mail with message and attachements after upgrading to Rails 3

开发者 https://www.devze.com 2023-02-14 15:56 出处:网络
I used to have this code for sending mails: class MailTimerMailer < ActionMailer::Base def mail_schedule(from, to, cc, bcc, subject, message, files=[], sent_at = Time.now)

I used to have this code for sending mails:

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

It no longer works. I do not have a view for my mails, as the complete message comes from outside my method. I have tried to modify my code to Rails 3 like this:

cl开发者_高级运维ass 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

This code sends a mail with the attachements, but there are no actual message in the mail. It also gives me a deprecation warning "Giving a hash to body is deprecated, please use instance variables instead". I have tried with "body :message => message" but no luck.

How can I get this working again?

Thank you


This is how:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消