0

I am using the gem https://github.com/sendgrid/sendgrid-ruby

In SendGrid I have a transactional email template that looks like

<%body%>
Hello :name!, what's up
your number is :number.
This is the transactional footer

how to pass the variables :number, :name??

thanks,

Shiva
  • 11,485
  • 2
  • 67
  • 84

1 Answers1

0

Check out the section of the README relating to templates.

Assuming that you have a user model that you want to sent the email to:

# assuming some users
recipients = []

users.each do |user|
  recipient = SendGrid::Recipient.new(user.email)
  recipient.add_substitution('name', 'some name')
  recipient.add_substitution('number', 1234)

  recipients << recipient
end

# then initialize a template
template = SendGrid::Template.new('MY_TEMPLATE_ID')

# create the client
client = SendGrid::Client.new(api_user: my_user, api_key: my_key)

# set some defaults
mail_defaults = {
  from: 'admin@email.com',
  html: '<h1>I like email</h1>',
  text: 'I like email',
  subject: 'Email is great',
}

mailer = SendGrid::TemplateMailer.new(client, template, recipients)

# then mail the whole thing at once
mailer.mail(mail_defaults)
yez
  • 2,368
  • 9
  • 15
  • Tried this and getting an error. NoMethodError (undefined method `to_h' for #): at the following line "mailer.mail(mail_defaults)" – jdog Mar 29 '16 at 02:38