ActionMailer with non-constant template


The situation is that opposed to the classic setting (i.e. having an .rhtml file as the email template) the e-mail template I am using to send e-mail should be editable - i.e. there is a first version of the template (in the DB, or as an .rhtml_, it does not matter), which the user can edit from the Rails app, then the edited version should be saved and used to send the email.

While I don't think so this problem was not solved before, I was unable to find anything after heaps of googling - it seems everyone is just using the standard, one-html-for-all method.

So what's the Rails way to do this?

Cheers, Peter

Hi Peter, I would bet that this situation comes up a lot. The best way I’ve found to handle this is to have your template in the DB. You can use ERB to process it like so: email_body =“[{%”, “<%”).gsub(“%}]”,"

%>"), nil, “”).result(binding) The gsubs are used because I recommend using the string [{% instead of <%. This allows you to use a WYSIWYG editor with minimum headache. Given that your making an interface for changing the email templates, one assumes, WYSIWYG is on the agenda too.

You still end up with a .rhtml file, but all in contains is <%= email_body %>

I’m sure others out there are doing something similar and would love to hear what they do.

Good Luck, Rob Kaufman


I just finished putting up a blog post, my first, on how I did this. Give it a read and let me know if it helped you at all.

Cheers, Kevin


URL correction...because Mephisto is not perfect (yet).

Cheers, Kevin