Class: Code::Object::Smtp

Inherits:
Dictionary show all
Defined in:
lib/code/object/smtp.rb

Constant Summary

Constants inherited from Code::Object

NUMBER_CLASSES

Instance Attribute Summary

Attributes included from Concerns::Shared

#methods, #raw

Instance Method Summary collapse

Methods inherited from Dictionary

#<=>, #code_any?, #code_clear, #code_compact, #code_compact!, #code_deep_duplicate, #code_delete, #code_delete_if, #code_delete_unless, #code_dig, #code_each, #code_empty?, #code_except, #code_fetch, #code_fetch_values, #code_flatten, #code_get, #code_has_key?, #code_has_value?, #code_invert, #code_keep_if, #code_keep_unless, #code_key, #code_keys, #code_merge, #code_merge!, #code_select, #code_select!, #code_set, #code_size, #code_to_context, #code_to_query, #code_transform_values, #code_values, #initialize

Methods inherited from Code::Object

code_new, #code_new, #initialize, maybe, #name, repeat, |

Methods included from Concerns::Shared

#<=>, #==, #as_json, #code_and, #code_as_json, #code_compare, #code_deep_duplicate, #code_different, #code_duplicate, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_falsy?, code_fetch, #code_fetch, code_get, #code_get, #code_greater, #code_greater_or_equal, #code_inclusive_range, #code_inspect, #code_less, #code_less_or_equal, #code_methods, #code_name, #code_nothing?, #code_or, #code_self, #code_set, code_set, #code_something?, #code_strict_different, #code_strict_equal, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_string, #code_to_time, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #sig, #something?, #succ, #to_code, #to_i, #to_json, #to_s, #truthy?

Constructor Details

This class inherits a constructor from Code::Object::Dictionary

Instance Method Details

#call(**args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/code/object/smtp.rb', line 6

def call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code
  code_value = code_arguments.code_first

  case code_operator.to_s
  when "send"
    sig(args) do
      {
        from: String.maybe,
        to: String.maybe,
        subject: String.maybe,
        body: String.maybe,
        body_text: String.maybe,
        body_html: String.maybe
      }
    end

    code_send(
      from: code_value.code_get("from"),
      to: code_value.code_get("to"),
      subject: code_value.code_get("subject"),
      body: code_value.code_get("body"),
      body_text: code_value.code_get("body_text"),
      body_html: code_value.code_get("body_html")
    )
  else
    super
  end
end

#code_send(from: nil, to: nil, subject: nil, body: nil, body_text: nil, body_html: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/code/object/smtp.rb', line 37

def code_send(
  from: nil,
  to: nil,
  subject: nil,
  body: nil,
  body_text: nil,
  body_html: nil
)
  code_from = from.to_code
  code_to = to.to_code
  code_subject = subject.to_code
  code_body = body.to_code
  code_body_text = body_text.to_code
  code_body_html = body_html.to_code

  mail = Mail.new
  mail.from = code_from.to_s
  mail.to = code_to.to_s
  mail.subject = code_subject.to_s

  text_part = Mail::Part.new
  text_part.content_type = "text/plain; charset=UTF-8"
  text_part.body = code_body_text.to_s.presence || code_body.to_s

  html_part = Mail::Part.new
  html_part.content_type = "text/html; charset=UTF-8"
  html_part.body = code_body_html.to_s

  mail.content_type = "multipart/alternative"
  mail.add_part(text_part)
  mail.add_part(html_part) if code_body_html.to_s.present?

  mail.delivery_method(
    :smtp,
    {
      address: code_get("address").to_s,
      port: code_get("port").to_i,
      user_name: code_get("user_name").to_s,
      password: code_get("password").to_s,
      authentication: code_get("authentication").to_s,
      enable_starttls_auto: code_get("enable_starttls_auto").truthy?
    }
  )

  mail.deliver!

  Nothing.new
end