Action Mailer: How do I render dynamic data in an email body that is stored in the database?

Posted by Brandon Toone on Stack Overflow See other posts from Stack Overflow or by Brandon Toone
Published on 2010-05-03T18:49:37Z Indexed on 2010/05/03 19:08 UTC
Read the original article Hit count: 295

Filed under:
|

I have Action Mailer setup to render an email using the body attribute of my Email model (in the database). I want to be able to use erb in the body but I can't figure out how to get it to render in the sent email message.

I'm able to get the body as a string with this code

# models/user_mailer.rb
def custom_email(user, email_id)
  email = Email.find(email_id)

  recipients    user.email
  from          "Mail It Example <[email protected]>"
  subject       "Hello From Mail It"
  sent_on       Time.now

  # pulls the email body and passes a string to the template views/user_mailer/customer_email.text.html.erb
  body          :msg => email.body
end

I came across this article http://rails-nutshell.labs.oreilly.com/ch05.html which says I can use render but I'm only able to get render :text to work and not render :inline

# models/user_mailer.rb
def custom_email(user, email_id)
  email = Email.find(email_id)

  recipients    user.email
  from          "Mail It Example <[email protected]>"
  subject       "Hello From Mail It"
  sent_on       Time.now

  # body          :msg => email.body
  body          :msg => (render :text => "Thanks for your order")  # renders text and passes as a variable to the template
  # body          :msg => (render :inline => "We shipped <%= Time.now %>")  # throws a NoMethodError

end

© Stack Overflow or respective owner

Related posts about actionmailer

Related posts about ruby-on-rails