views:

128

answers:

1

My software sends emails for users. I want to show them what the emails will look like before they get sent. However, with ActionMailer conventions, the entire template is in one file. This means the html,head,body tags, etc. Can anyone think of a good way to give my users a preview of what the emails I send out will look like?

Thanks!

+1  A: 

I had the same issue. I built out the display with the associated model I was sending rather than in the mailer. I was able to feed sample data or live data to display it to the user. when it came time to actually send it, I rendered the exact same thing within the mailer view

EDIT: I apologize for the crap variable names in advance. I am not sure I am allowed to explicitly talk about them :)

Lets say I have a BarMailer function called foo(status,bar) where status is a test email or a live email and bar is my associated model.

I called deliver_foo("test",bar)

deliver_foo sends out a multipart message so for each part I render_message and pass along variables I need. for example:

p.body = render_message('bar_html', :bar => bar, :other_data => bar.other_data)

so, that render_message is is saying to specifically use the bar_html view (I also have a bar_text for plain text).

this is the contents of my bar_html view:

<%=render :inline => @bar.some_parent.some_other_model.html, :locals => {:other_data => @other_data, :time => Time.now, :bar => @bar }%>

Its a little complicated, but it is based on a template system. By rendering inline everywhere, I am able to use the same code for a number of different functions including previewing and sending. I like this because it becomes a WYSIWIG. No extra code or functionality that could be buggy and muck with the potential output in an email. If it works in one area, it will work in the other. Plus keeping it DRY means I am not going to forget to modify a copy (which I would do frequently, hehe).

cgr
interesting...so in your actionmailer model you had something like @body[:everything] = AssociatedModel.generate_email, and then your actionmailer view was just @everything?
Tony
edited the original answer for you so I could use the code function :)
cgr