Class: Mailerooby::EmailSender

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/mailerooby/email_sender.rb

Class Method Summary collapse

Class Method Details

.send_email(from:, to:, subject:, body:, cc: nil, bcc: nil, reply_to: nil, attachments: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mailerooby/email_sender.rb', line 13

def self.send_email(from:, to:, subject:, body:, cc: nil, bcc: nil, reply_to: nil, attachments: nil)
    validate_parameters(from: from, to: to, subject: subject, body: body)
    headers = {
        'X-API-Key' => Mailerooby.sending_api_key
    }
    body = { from: from, to: to, subject: subject, body: body, cc: cc, bcc: bcc, reply_to: reply_to, attachments: attachments }
    response = post(base_uri, headers: headers, body: body, multipart: true)
    case response.code
    when 400
      raise BadRequestError, "Bad request: #{response.body}"
    when 401
      raise UnauthorizedError, "Unauthorized: #{response.body}"
    when 200
      JSON.parse(response.body)
    else
      raise GeneralAPIError, "API Error: #{response.body}"
    end
end