# ActionMailer with non-constant template

**URL:** <https://www.ruby-forum.com/t/actionmailer-with-non-constant-template/113476>\
**Category:** Rails\
**Created:** [August 31, 2007, 2:03pm UTC](https://www.ruby-forum.com/t/actionmailer-with-non-constant-template/113476 "2007-08-31T14:03:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [August 31, 2007, 2:03pm UTC](https://www.ruby-forum.com/t/actionmailer-with-non-constant-template/113476/1 "2007-08-31T14:03:30Z")

</div>

Hello,

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

\_\_  
[http://www.rubyrailways.com](http://www.rubyrailways.com)  
[http://scrubyt.org](http://scrubyt.org)

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [August 31, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/actionmailer-with-non-constant-template/113476/2 "2007-08-31T19:46:23Z")

</div>

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 = ::ERB.new(email\_template.gsub("[{%", “\<%”).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 K.

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [September 4, 2007, 5:58pm UTC](https://www.ruby-forum.com/t/actionmailer-with-non-constant-template/113476/3 "2007-09-04T17:58:22Z")

</div>

Peter,

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.

[http://blog.morwood.com/dynamic-actionmailer-content](http://blog.morwood.com/dynamic-actionmailer-content)

Cheers,  
Kevin

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [September 4, 2007, 6:24pm UTC](https://www.ruby-forum.com/t/actionmailer-with-non-constant-template/113476/4 "2007-09-04T18:24:29Z")

</div>

Peter,

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

[http://blog.morwood.com/2007/09/04/dynamic-actionmailer-content](http://blog.morwood.com/2007/09/04/dynamic-actionmailer-content)

Cheers,  
Kevin
