Allow user to create custom email template for ActionMailer in Rails

I want to allow the user of the rails app to create a custom email template via a form and use it as a default site email template form to be mailed to, but I can't find any tutorials, it has to contain a couple of dynamic variables as well.

Is there a specific way to do this?

(so the user enters the email text in the form referencing the variables to be evaluated before submitting)

+3


source to share


4 answers


There are many ways to accomplish this. Here's one way.

First of all, you need to save the user generated email templates in the database.

CustomEmailTemplate.create(user_id: params[:user_id], template_body: params[:template_body])

      

When it's time to send an email for the template, you should do something like:

custom_values = { name: "Whatever", value: "nice" }
template = CustomEmailTemplate.find(template_id)
CustomerEmailer.send_email(template, custom_values).deliver

      

For it to actually work, you need to do something like this in your mailbox:



def send_email(template, custom_values)
    @template = template
    @custom_values = custom_values
    mail subject: "Your subject goes here.."
end

      

In send_email.html.erb do the following:

<%= @template.template_text.gsub(/@@name@@/, @custom_values[:name]).gsub(/@@value@@/, @custom_values[:value]) %>

      

You can see that the template contains "@@ name @@" and "@@ value @@". These are markers that you can replace with custom values.

Of course, you can refactor this code to put the replacement logic in the model, etc., to make it "clean code".

Hope this helps.

+5


source


The easiest would be to store it in the rails database. Therefore, you have to create a named rail model EmailTemplate

that has a body

type field text

.

Then you create a form to update or create new templates.

To use this particular template and send it as email. Create a Rails mailer and in a view load the appropriate EmailTemplate. So in view it will be as follows.



<%= EmailTemplate.find(id).body.html_safe %>

      

The above method is the easiest way to do it. If you want the user to be able to access various variables on your system, then I recommend that you learn fluid markup language ( https://github.com/Shopify/liquid/ ).

+3


source


I would modify Yosep's idea to avoid using the real erb pattern. Send mail directly using an argument body

, as this degrades performance.

eg

def send_email(template, custom_values)
    body = template.template_text\
     .gsub(/@@name@@/, custom_values[:name])
     .gsub(/@@value@@/, custom_values[:value]
    mail subject: "Your subject goes here..", body: body
end

      

0


source


You can use panaromic gem to store views in the database. This will allow Rails to automatically search the database for related views based on the path

and column handler

.

Fluid templates can be used to make variables available through the user interface. liquid-rails provides a way to use liquid templates as Rails views. Now if you update the column handler

to .liquid, you can save the views to the database as fluid templates.

Check out this blog for more information.

0


source







All Articles