Reputation: 51
Working on a Rails app that connects with eCommerce sites, occasionally sending emails from our app to customers of the eCommerce sites we work with.
Right now we have a generic email template saved in the app as a html.erb
file, but we're trying to add functionality to allow these eCommerce sites (our users) to submit their own custom email templates to use for their respective customers.
My first instinct was to allow users to submit their template in a textarea form and save it in our MySQL database in a templates table. However, I'm concerned about a few things:
<% User.destroy_all %>
or some other damaging Ruby code inside.Our app uses Rails 4.1.7 And Ruby 2.1.4p265.
Upvotes: 1
Views: 1386
Reputation: 4516
As per your needs I would go with a simple solution
I'll leave the implementation up to you, but here is the example input:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>[[title]]</title>
</head>
<body>
Hello [[username]], thank you for subscribing.
You can get started by visiting this <a href="[[link]]">link</a>.
</body>
</html>
the parser converts [[username]]
to <%= @username %>
, and the result will be something like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><%= @title %></title>
</head>
<body>
Hello <%= @username %>, thank you for subscribing.
You can get started by visiting this <a href="<%= @link %>">link</a>.
</body>
</html>
To answer your concerns:
1.This is already answered above.
2.That's very easy, use .html_safe or the raw() helper function.
3.This should not be a concern because you will not run any code that's submitted by the user, since you will only allow user to give set variables that you know of. You just do a regex match not a eval().
4.This is the easiest solution I could think of.
Upvotes: 1
Reputation: 122
One way you might approach this is to allow users to save an mjml template to the database.
Theres a mjml-rails gem that might help out.
there is a mjml-include
tag that will allow you to include thigns before they are rendered. I'd imagine you can give your users a list of allowable includes, which link to the proper partial.
Upvotes: 0