Class: Cloudflare::EmailService::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudflare/email_service/message.rb

Overview

Validates and serializes an outbound email into the JSON payload expected by the Cloudflare Email Service “send” endpoint.

Addresses may be given as:

* a String — "user@example.com" or "Display Name <user@example.com>"
* a Hash   — { email: "user@example.com", name: "Display Name" }
             (:address is accepted as an alias for :email)
* an Array of any of the above (for to / cc / bcc)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from:, to:, subject:, html: nil, text: nil, cc: nil, bcc: nil, reply_to: nil, attachments: nil, headers: nil) ⇒ Message

Returns a new instance of Message.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cloudflare/email_service/message.rb', line 17

def initialize(from:, to:, subject:, html: nil, text: nil, cc: nil,
               bcc: nil, reply_to: nil, attachments: nil, headers: nil)
  @from = from
  @to = to
  @cc = cc
  @bcc = bcc
  @reply_to = reply_to
  @subject = subject
  @html = html
  @text = text
  @attachments = attachments
  @headers = headers
end

Instance Attribute Details

#attachmentsObject (readonly)

Returns the value of attribute attachments.



14
15
16
# File 'lib/cloudflare/email_service/message.rb', line 14

def attachments
  @attachments
end

#bccObject (readonly)

Returns the value of attribute bcc.



14
15
16
# File 'lib/cloudflare/email_service/message.rb', line 14

def bcc
  @bcc
end

#ccObject (readonly)

Returns the value of attribute cc.



14
15
16
# File 'lib/cloudflare/email_service/message.rb', line 14

def cc
  @cc
end

#fromObject (readonly)

Returns the value of attribute from.



14
15
16
# File 'lib/cloudflare/email_service/message.rb', line 14

def from
  @from
end

#headersObject (readonly)

Returns the value of attribute headers.



14
15
16
# File 'lib/cloudflare/email_service/message.rb', line 14

def headers
  @headers
end

#htmlObject (readonly)

Returns the value of attribute html.



14
15
16
# File 'lib/cloudflare/email_service/message.rb', line 14

def html
  @html
end

#reply_toObject (readonly)

Returns the value of attribute reply_to.



14
15
16
# File 'lib/cloudflare/email_service/message.rb', line 14

def reply_to
  @reply_to
end

#subjectObject (readonly)

Returns the value of attribute subject.



14
15
16
# File 'lib/cloudflare/email_service/message.rb', line 14

def subject
  @subject
end

#textObject (readonly)

Returns the value of attribute text.



14
15
16
# File 'lib/cloudflare/email_service/message.rb', line 14

def text
  @text
end

#toObject (readonly)

Returns the value of attribute to.



14
15
16
# File 'lib/cloudflare/email_service/message.rb', line 14

def to
  @to
end

Instance Method Details

#to_hHash

Returns the request body, with nil/empty fields omitted.

Returns:

  • (Hash)

    the request body, with nil/empty fields omitted.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cloudflare/email_service/message.rb', line 45

def to_h
  {
    from: normalize_address(from),
    to: normalize_recipients(to),
    subject: subject,
    cc: optional_recipients(cc),
    bcc: optional_recipients(bcc),
    reply_to: optional_address(reply_to),
    html: presence(html),
    text: presence(text),
    headers: presence(headers),
    attachments: optional_attachments(attachments),
  }.compact
end

#validate!self

Returns:

  • (self)

Raises:



33
34
35
36
37
38
39
40
41
42
# File 'lib/cloudflare/email_service/message.rb', line 33

def validate!
  raise ValidationError, "from is required" if blank?(from)
  raise ValidationError, "to is required" if blank?(to)
  raise ValidationError, "subject is required" if blank?(subject)
  if blank?(html) && blank?(text)
    raise ValidationError, "provide html and/or text body content"
  end

  self
end