class Sendgrid::Mailer
Constants
- VERSION
Public Class Methods
new(api_key, from, bcc, sandbox_mode: false, dev_catch_all: false)
click to toggle source
# File lib/sendgrid/mailer.rb, line 10 def initialize(api_key, from, bcc, sandbox_mode: false, dev_catch_all: false) @api_key = api_key @from = from @bcc = bcc @sandbox_mode = sandbox_mode @dev_catch_all = dev_catch_all end
Public Instance Methods
build_mail_json(template_id:, to: nil, from: nil, bcc: nil, substitutions: {}, options: {})
click to toggle source
# File lib/sendgrid/mailer.rb, line 18 def build_mail_json(template_id:, to: nil, from: nil, bcc: nil, substitutions: {}, options: {}) options = { force_send: @sandbox_mode, }.merge(options) if @dev_catch_all to = @dev_catch_all bcc = nil end SendGrid::Mail.new.tap do |m| m.from = build_from(from) m.template_id = template_id if template_id m.personalizations = build_personalization(to, bcc, substitutions) if !options[:force_send] && !Rails.env.production? m.mail_settings = SendGrid::MailSettings.new.tap do |s| s.sandbox_mode = SendGrid::SandBoxMode.new(enable: true) end end end.to_json end
send_mail(options)
click to toggle source
# File lib/sendgrid/mailer.rb, line 42 def send_mail(options) options = build_mail_json(options) send_grid.client.mail._('send').post(request_body: options) end
Private Instance Methods
build_field(field_name, object, values, defaults = nil)
click to toggle source
# File lib/sendgrid/mailer.rb, line 49 def build_field(field_name, object, values, defaults = nil) if values && values.is_a?(Array) values.each do |v| object.send("#{field_name}=", parse_email(v)) end else object.send("#{field_name}=", parse_email(values || defaults)) end object end
build_from(from)
click to toggle source
# File lib/sendgrid/mailer.rb, line 61 def build_from(from) parse_email(from || @from) end
build_personalization(to, bcc, substitutions)
click to toggle source
# File lib/sendgrid/mailer.rb, line 65 def build_personalization(to, bcc, substitutions) SendGrid::Personalization.new.tap do |p| p = build_field(:to, p, to) p = build_field(:bcc, p, bcc, @bcc) if substitutions substitutions.each do |k, v| p.substitutions = SendGrid::Substitution.new( key: "{{#{k}}}", value: v ) end end end end
parse_email(value)
click to toggle source
# File lib/sendgrid/mailer.rb, line 82 def parse_email(value) output = if value.is_a? Hash raise "No key :email found" unless value[:email] value elsif split = value.match(/(.+?)<(.+)>/) { name: split[1].strip, email: split[2].strip, } else { email: value.strip } end SendGrid::Email.new(output) end
send_grid()
click to toggle source
# File lib/sendgrid/mailer.rb, line 99 def send_grid @sendgrid ||= SendGrid::API.new(api_key: @api_key) end