Manish Shrivastava
Manish Shrivastava

Reputation: 32040

dynamic email subject and body from database

In my admin site, I want editable

Dynamic subject in database text.

mailer = MailTemplate.find_by_template_name('friend_request')
# mailer.subject = "#{@user.username} wants to be friend on site.com"

I have to use code like :

mail(:to => friend.email, :subject => mailer.subject) do |format|
  format.text { render :inline => nl2br(mailer.body) }
  format.html { render :inline => nl2br(mailer.body) }
end

Now, I found body is working properly with dynamic code. But SUBJECT is not working with mailer.subject for value like "#{@user.username} wants to be friend on SportsPundit.com"

Please suggest. I tried with eval(mailer.subject) as well.

Upvotes: 0

Views: 323

Answers (1)

000
000

Reputation: 27247

Consider using a different templating language so you're not exposing ruby to the user. Handlebars may be a good choice.

In your MailTemplate object, subject would be

"{{ user/username }} wants to be friend on site.com"

Then you would render it using handlebars-rails or some other lib, passing @user as user in the handlebars template. This is better for the average user to figure out, so they aren't looking at ruby.

Upvotes: 1

Related Questions