Reputation: 169
I am sending email through my application. Since I have to send different type of mails I use database to save the message and then fetch the message for the email body.
This is how I fetch the notification.
def send_share_company(address, cid, from, council_id)
co = Council.find_by(id: council_id)
c = Company.find_by(id: cid)
@a = address.split(',')
@f = from
n = Notification.find_by(id: 'ad4e3718-0689-413b-8a52-96887e0d2aba')
message =n["body"]
from = Email.new(email: '[email protected]', name: "my Network")
subject = + cl["title"] + ' - Event of the Week: ' + c["name"]
to = Email.new(email: a.to_s)
content = Content.new(type: 'text/html', value: message)
mail = Mail.new(from, subject, to, content)
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
pp sg.client.mail._("send").post(request_body: mail.to_json) unless Rails.env.staging?
end
I save notification body in database as below.
Hello,
#{@f} thought you should take a look at the company, #{c["name"]}.
I want to replace #{@f} and #{c["name"]} with the values. But it is not happen. Email send without replacing values. How can I solve this.
Upvotes: 0
Views: 116
Reputation: 102001
String interpolation only occurs in string literals. Not in strings that are loaded from a file, the database or user input - just imagine how dangerous that would be.
In order to for the interpolation to be expanded you have to quote the content and eval it. Which is pretty far from ideal. A better idea would be to use a template engine like ERB.
Hello,
<%= #{@f} %> thought you should take a look at the company, <%= #{c["name"]} %>.
require 'erb'
template = ERB.new(n.body)
rendered = template.render(binding)
Upvotes: 1