Class: Sendgrid::Mailer

Inherits:
Object
  • Object
show all
Includes:
SendGrid
Defined in:
lib/sendgrid/mailer.rb,
lib/sendgrid/mailer/version.rb

Constant Summary collapse

VERSION =
"0.1.7"

Instance Method Summary collapse

Constructor Details

#initialize(api_key, from, bcc, sandbox_mode: false, dev_catch_all:) ⇒ Mailer

Returns a new instance of Mailer.



10
11
12
13
14
15
16
# File 'lib/sendgrid/mailer.rb', line 10

def initialize(api_key, from, bcc, sandbox_mode: false, dev_catch_all:)
  @api_key = api_key
  @from = from
  @bcc = bcc
  @sandbox_mode = sandbox_mode
  @dev_catch_all = dev_catch_all
end

Instance Method Details

#build_mail_json(template_id:, to: nil, from: nil, bcc: nil, substitutions: {}, options: {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sendgrid/mailer.rb', line 18

def build_mail_json(template_id:, to: nil, from: nil, bcc: nil, substitutions: {}, options: {})
  options = {
    force_send: false,
  }.merge(options)

  if @dev_catch_all
    to = @dev_catch_all
  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) ⇒ Object



41
42
43
44
# File 'lib/sendgrid/mailer.rb', line 41

def send_mail(options)
  options = build_mail_json(options)
  send_grid.client.mail._('send').post(request_body: options)
end