# Use a string as template for mail body

**URL:** <https://www.ruby-forum.com/t/use-a-string-as-template-for-mail-body/150920>\
**Category:** Rails\
**Created:** [October 14, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/use-a-string-as-template-for-mail-body/150920 "2008-10-14T22:46:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisjo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chrisjo](https://www.ruby-forum.com/u/chrisjo)\
**Post date:** [October 14, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/use-a-string-as-template-for-mail-body/150920/1 "2008-10-14T22:46:12Z")

</div>

I want to allow users to create mail templates through an administration  
page. I store the email body as text in the database, and now I want to  
take the string, and treat it like an email template and send as mail.

So for instance, if something like this is in the database:

`
email_templates
id | text
 1 | Hello #{@user.name}! This is an email
...
`

Then I’d like to do this:

`
template = EmailTemplate.find(1)
user = User.find(1)
MyMailer.deliver_some_mail(template.text, user)
`

…and

`
class MyMailer < ActionMailer::Base
  def some_mail(template, user)
    subject "..."
    recipient "removed_email_address@domain.invalid"
    # ...
```
body string_template(template, :user => user)

```
end
end
`

Is it possible? If so, how?

---

<div class="post-metadata">

**Author:** ![chrisjo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chrisjo](https://www.ruby-forum.com/u/chrisjo)\
**Post date:** [October 14, 2008, 11:24pm UTC](https://www.ruby-forum.com/t/use-a-string-as-template-for-mail-body/150920/2 "2008-10-14T23:24:33Z")

</div>

While this is pretty easy with the ERB library and its rendering, it’s  
also  
very dangerous. You’ll need to build a whitelist of what you’ll let them  
do.

“Hello #{User.delete\_all}”

Never let anyone arbitrarily monkey with your code or data.  
Instead, make your own parser or look at how some of the CMS tools like  
Radiant do things like this.

On Tue, Oct 14, 2008 at 3:46 PM, Christian J. \<

---

<div class="post-metadata">

**Author:** ![chrisjo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chrisjo](https://www.ruby-forum.com/u/chrisjo)\
**Post date:** [October 14, 2008, 11:29pm UTC](https://www.ruby-forum.com/t/use-a-string-as-template-for-mail-body/150920/3 "2008-10-14T23:29:30Z")

</div>

Brian H. wrote:

> While this is pretty easy with the ERB library and its rendering, it’s  
> also  
> very dangerous. You’ll need to build a whitelist of what you’ll let them  
> do.
> 
> “Hello #{User.delete\_all}”
> 
> Never let anyone arbitrarily monkey with your code or data.  
> Instead, make your own parser or look at how some of the CMS tools like  
> Radiant do things like this.
> 
> On Tue, Oct 14, 2008 at 3:46 PM, Christian J. \<

Yup, I’m very aware of the safety implications. Basically this will be  
available to people who have access to the code as well, but it makes  
this task a bit easier. I’ll look up simpler parsing that’ll just allow  
for looking up properties on a single object or something like that.  
Thanks!

---

<div class="post-metadata">

**Author:** ![chrisjo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chrisjo](https://www.ruby-forum.com/u/chrisjo)\
**Post date:** [October 14, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/use-a-string-as-template-for-mail-body/150920/4 "2008-10-14T23:41:40Z")

</div>

On 14 Oct 2008, at 22:29, Christian J. wrote:

> > 
> 
> this task a bit easier. I’ll look up simpler parsing that’ll just  
> allow  
> for looking up properties on a single object or something like that.  
> Thanks!

For what it’s worth, something like

@body = render :inline =\> some\_string, :body =\> {}

would do it.

Fred

---

<div class="post-metadata">

**Author:** ![chrisjo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chrisjo](https://www.ruby-forum.com/u/chrisjo)\
**Post date:** [October 14, 2008, 11:33pm UTC](https://www.ruby-forum.com/t/use-a-string-as-template-for-mail-body/150920/5 "2008-10-14T23:33:25Z")

</div>

On Tue, Oct 14, 2008 at 2:29 PM, Christian J.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yup, I’m very aware of the safety implications. Basically this will be  
> available to people who have access to the code as well, but it makes  
> this task a bit easier. I’ll look up simpler parsing that’ll just allow  
> for looking up properties on a single object or something like that.

> **[Liquid template language](https://shopify.github.io/liquid/)**
>
> Documentation for the Liquid template language, created by Shopify.

~ j.

---

<div class="post-metadata">

**Author:** ![chrisjo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chrisjo](https://www.ruby-forum.com/u/chrisjo)\
**Post date:** [October 15, 2008, 12:27am UTC](https://www.ruby-forum.com/t/use-a-string-as-template-for-mail-body/150920/6 "2008-10-15T00:27:00Z")

</div>

John B. wrote:

> On Tue, Oct 14, 2008 at 2:29 PM, Christian J.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Yup, I’m very aware of the safety implications. Basically this will be  
> > available to people who have access to the code as well, but it makes  
> > this task a bit easier. I’ll look up simpler parsing that’ll just allow  
> > for looking up properties on a single object or something like that.
> 
> [http://www.liquidmarkup.org](http://www.liquidmarkup.org)
> 
> ~ j.

Thanks, this looks very interesting!

---

<div class="post-metadata">

**Author:** ![chrisjo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chrisjo](https://www.ruby-forum.com/u/chrisjo)\
**Post date:** [October 15, 2008, 1:52pm UTC](https://www.ruby-forum.com/t/use-a-string-as-template-for-mail-body/150920/7 "2008-10-15T13:52:51Z")

</div>

Christian J. wrote:

> John B. wrote:
> 
> > On Tue, Oct 14, 2008 at 2:29 PM, Christian J.  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > Yup, I’m very aware of the safety implications. Basically this will be  
> > > available to people who have access to the code as well, but it makes  
> > > this task a bit easier. I’ll look up simpler parsing that’ll just allow  
> > > for looking up properties on a single object or something like that.
> > 
> > [http://www.liquidmarkup.org](http://www.liquidmarkup.org)
> > 
> > ~ j.
> 
> Thanks, this looks very interesting!

Played around with it a little bit, and man, this is perfect for what I  
needed. Very cool!
