SendGrid ruby​​库:在事务性电子邮件中发送特定于上下文的值

时间:2015-11-30 04:56:21

标签: ruby sendgrid sendgrid-ruby

我正在使用gem https://github.com/sendgrid/sendgrid-ruby

在SendGrid中,我有一个类似于

的交易电子邮件模板
<%body%>
Hello :name!, what's up
your number is :number.
This is the transactional footer

如何传递变量:number:name ??

感谢,

1 个答案:

答案 0 :(得分:0)

查看section of the README relating to templates

假设您有一个要将电子邮件发送到的用户模型:

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