Class: Resent::Emails

Inherits:
Object
  • Object
show all
Defined in:
lib/resent/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Emails

Returns a new instance of Emails.



91
92
93
# File 'lib/resent/client.rb', line 91

def initialize(client)
  @client = client
end

Instance Method Details

#send(from:, to:, subject:, html: nil, text: nil, cc: nil, bcc: nil, reply_to: nil) ⇒ Object

Send a transactional email.

Required: from, to, subject, and html and/or text.

Raises:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/resent/client.rb', line 98

def send(from:, to:, subject:, html: nil, text: nil, cc: nil, bcc: nil, reply_to: nil)
  to_list = Array(to).map { |v| v.to_s.strip }.reject(&:empty?)
  raise Error, "from is required" if from.to_s.strip.empty?
  raise Error, "to is required" if to_list.empty?
  raise Error, "subject is required" if subject.to_s.strip.empty?
  raise Error, "html or text body is required" if html.to_s.strip.empty? && text.to_s.strip.empty?

  payload = {
    "from" => from.to_s.strip,
    "to" => to_list,
    "subject" => subject.to_s
  }
  payload["html"] = html if html && !html.to_s.empty?
  payload["text"] = text if text && !text.to_s.empty?
  payload["cc"] = Array(cc) if cc
  payload["bcc"] = Array(bcc) if bcc
  payload["reply_to"] = reply_to if reply_to

  @client.request("POST", "email/send", body: payload)
end