Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic content in email templates

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 :)

like image 557
Aakanksha Avatar asked Apr 25 '17 11:04

Aakanksha


People also ask

What is a dynamic email template?

3. What is a dynamic email template? Dynamic email template designs are automatically personalized in real-time to build one-to-one customer relationships. A single template can be personalized and sent to a few million.

What is dynamic content in an email?

Gmail supports dynamic email, messages with interactive content. Dynamic email is sometimes called AMP for email. You can interact with dynamic email like you would a website. For example, you can reply to an event invitation, or respond to comments in a Google Doc, without leaving Gmail.

How dynamic content can be added in the email notification?

Adding dynamic content to the subject lineSelect Insert > Dynamic Content. Search for the dynamic content and then double-click the item you want to add. Add any additional text to the subject line. After you save your email, you can preview the subject line using different contacts.


1 Answers

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

like image 183
Aakanksha Avatar answered Oct 30 '22 22:10

Aakanksha