Dynamic content in email templates
Asked Answered
P

2

5

I have rails background and i'm working to implement something like as shown in the imageenter image description here

My email template will have such tags. While sending the email to the client, his information will get filled up in the template. I found this link here but there's no answer. I looked up and found thisgem, however i cant use it as it uses liquid templates and i was unable to implement it because of time limitation.

I know i can do the whole thing of finding {{first_name}} and replacing the content whenever a tag appears but i doubt if its an efficient way to implement it.

Please share your ideas and guidance. Thanks in Advance :)

Pitt answered 25/4, 2017 at 11:59 Comment(1)
@BLiu, I did not use any plugin for same. It was more like a text area where {{ and }} would enclose the dynamic tags. Hope this helps!Pitt
P
7

I was a noob in rails when i posted this question and soon forgot it and an upvote reminded me about it. I got successful in making it work and sharing my solution if anyone's looking for it:

class DynamicTemplateModel < ActiveRecord::Base

  def self.parse_template(template, attrs={})
    result = template
    attrs.each { |field, value| result.gsub!("{{#{field}}}", value) }
    # remove anything that resembles a field but did not match a known field name
    result.gsub!(/\{\{\.w+\}\}/, '')
    return result
  end

end

Usage: DynamicTemplate.parse_template(body, details)

where

details = {first_name: user.first_name, last_name: user.last_name, company: user&.company&.name, email_address: user.email}

and

body = "Hi {{first_name}} {{last_name}}, Your company {{company}} is registered with us successfully"

With the parse_template method, the new body will be as expected.

My main concern with diofeher solution about replacing the string was security. So managed with this.

Hope this helps someone. Leave any suggestions to make it better. Thanks

Pitt answered 26/9, 2017 at 5:9 Comment(0)
I
0

As you said before, you can use Liquid to replace it or you can try to use (http://www.kuwata-lab.com/erubis/).

Another option is to replace the tags like that:

ERB.new(your_template.gsub("{{", "<%=").gsub("}}", "%>")).result

You would change the default print of variable to use {{ }} syntax.

Iridissa answered 25/4, 2017 at 12:10 Comment(3)
I'm afraid i can't do this. This is a very very un-secure way and prone to SQL Injection. Image that i have to replace first_name with value and the value of first name of some user is User.delete_all. If that gets replaced here, my string will be like <%= User.delete_all %>. I don't think i need to say anything else here. My database will collapse with all the data gone.Pitt
Oh, so this is going to be used by users? I thought it was going to be used only by your team.Iridissa
this is required for the end users to prepare an template in which the tags will be replaced by data of users to whom the email is supposed to be sent. I've shared my solution. Suggestions on it are welcomed. ThanksPitt

© 2022 - 2024 — McMap. All rights reserved.