Class: SendLayer::Emails

Inherits:
Object
  • Object
show all
Defined in:
lib/sendlayer/emails.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Emails

Returns a new instance of Emails.



16
17
18
# File 'lib/sendlayer/emails.rb', line 16

def initialize(client)
  @client = client
end

Instance Method Details

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sendlayer/emails.rb', line 20

def send(from:, to:, subject:, text: nil, html: nil, cc: nil, bcc: nil, reply_to: nil,
         attachments: nil, headers: nil, tags: nil)
  
  # Validate required parameters
  raise SendLayerValidationError.new("Either 'text' or 'html' content must be provided") if text.nil? && html.nil?
  
  # Prepare email data
  email_data = {
    From: normalize_recipient(from, 'sender'),
    To: normalize_recipients(to, 'recipient'),
    Subject: subject
  }

  if html
    email_data[:ContentType] = "HTML"
    email_data[:HTMLContent] = html
  else
    email_data[:ContentType] = "Text"
    email_data[:PlainContent] = text
  end
  email_data[:CC] = normalize_recipients(cc) if cc
  email_data[:BCC] = normalize_recipients(bcc) if bcc
  email_data[:ReplyTo] = normalize_recipients(reply_to, 'reply_to') if reply_to
  email_data[:Headers] = headers if headers

  if tags
    unless tags.is_a?(Array) && tags.all? { |t| t.is_a?(String) }
      raise SendLayerValidationError.new('Tags must be a list of strings')
    end
    email_data[:Tags] = tags
  end

  # Handle attachments
  if attachments && !attachments.empty?
    email_data[:Attachments] = process_attachments(attachments)
  end

  @client.make_request('POST', 'email', email_data)
end