Allow user to create custom email template for ActionMailer in Rails

二次信任 提交于 2019-12-06 02:55:31

There are many ways you can achieve it. Here is one way.

First of all, you need to store user-generated email templates in a 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 would do something like:

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

To make it really work, you need to do something like this in the mailer:

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

In the send_email.html.erb, do something like this:

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

You see that the template contains "@@name@@" and "@@value@@". Those are the markers for you to be able to replace with custom values.

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

I hope this helps.

Well easiest would be to store it into you rails database. So you should create a rails model called EmailTemplate which has a field body type text.

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

To use this particular template and send it as an email. Create a Rails mailer, and in the view load the corresponding EmailTemplate. So in the view it would be as follows.

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

The above method is the easiest way to do this. If you want to let the user access various variables in your system as well then I recommend you look into liquid markup language (https://github.com/Shopify/liquid/).

I would modify Yosep's idea to avoid using an actual erb template. Send the mail directly using a body arg as it's less performance overhead.

e.g.

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

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

To make variables accessible through UI, liquid templates can be used. liquid-rails provides a way to use liquid templates as Rails views. Now if you update handler column to .liquid, you can store the views in database as liquid templates.

Check this blog for more information.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!