Class: ChainMail::Providers::SendGrid

Inherits:
Base
  • Object
show all
Defined in:
lib/chain_mail/providers/send_grid.rb

Class Method Summary collapse

Methods inherited from Base

post_json

Class Method Details

.deliver(mail, creds) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/chain_mail/providers/send_grid.rb', line 9

def self.deliver(mail, creds)
  if creds[:api_key].nil? || creds[:api_key].to_s.strip.empty?
    raise KeyError,
          "Missing SendGrid API key"
  end

  payload = {
    personalizations: [{ to: mail.to.map { |t| { email: t } } }],
    from: { email: mail.from.first },
    subject: mail.subject,
    content: [{ type: "text/html", value: mail.body.decoded }]
  }
  headers = {
    "Authorization" => "Bearer #{creds[:api_key]}",
    "Content-Type" => "application/json"
  }
  post_json("https://api.sendgrid.com/v3/mail/send", headers, payload)
end