Class: Mushy::Smtp

Inherits:
Flux
  • Object
show all
Defined in:
lib/mushy/fluxs/smtp.rb

Instance Attribute Summary

Attributes inherited from Flux

#config, #id, #masher, #parent_fluxs, #subscribed_to, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Flux

#convert_this_to_an_array, #convert_to_symbolized_hash, #execute, #execute_single_event, #group_these_results, #guard, inherited, #initialize, #join_these_results, #limit_these_results, #merge_these_results, #model_these_results, #outgoing_split_these_results, #shape_these, #sort_these_results, #standardize_these

Constructor Details

This class inherits a constructor from Mushy::Flux

Class Method Details

.detailsObject



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
36
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
# File 'lib/mushy/fluxs/smtp.rb', line 7

def self.details
  {
    name: 'Smtp',
    description: 'Send email through SMTP.',
    config: {
      from: {
              description: 'From whom the email will be sent.',
              type:        'text',
              shrink:      true,
              value:       '',
            },
      to: {
            description: 'To whom the email should be sent.',
            type:        'text',
            value:       '',
          },
      subject: {
                 description: 'The subject of the email.',
                 type:        'text',
                 value:       '',
               },
      body: {
              description: 'The text body of the email.',
              type:        'textarea',
              value:       '',
            },
      html_body: {
              description: 'The HTML body of the email.',
              type:        'textarea',
              value:       '',
            },
      attachment_file: {
                         description: 'The full path of a file to attach.',
                         type:        'text',
                         shrink:      true,
                         value:       '',
                       },
      address: {
                 description: 'The address of the SMTP server.',
                 type:        'text',
                 value:       'smtp.gmail.com',
               },
      port: {
              description: 'The SMTP server port.',
              type:        'integer',
              value:       '587',
            },
      domain: {
                description: 'The email domain.',
                type:        'text',
                value:       'gmail.com',
              },
      username: {
                  description: 'The username.',
                  type:        'text',
                  value:       '',
                },
      password: {
                  description: 'The password.',
                  type:        'text',
                  value:       '',
                },
    },
  }
end

Instance Method Details

#adjust(options) ⇒ Object



91
92
93
# File 'lib/mushy/fluxs/smtp.rb', line 91

def adjust options
  options.tap { |x| x[:via] = 'smtp' }
end

#cleanup(options) ⇒ Object



95
96
97
98
99
# File 'lib/mushy/fluxs/smtp.rb', line 95

def cleanup options
  options.tap do |hash|
    hash.delete_if { |_, v| v.to_s == '' }
  end
end

#get_via_options_from(config) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/mushy/fluxs/smtp.rb', line 101

def get_via_options_from config
  {
    address:              config[:address],
    port:                 config[:port].to_s,
    user_name:            config[:username],
    password:             config[:password],
    domain:               config[:domain],
    authentication:       :plain,
    enable_starttls_auto: true,
  }
end

#process(event, config) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mushy/fluxs/smtp.rb', line 73

def process event, config
  options = adjust(cleanup({
      from: config[:from],
      to: config[:to],
      subject: config[:subject],
      body: config[:body],
      html_body: config[:html_body],
      via_options: get_via_options_from(config)
  }))

  if (config[:attachment_file].to_s != '')
    options[:attachments] = { config[:attachment_file].split("\/")[-1] => File.read(config[:attachment_file]) }
  end

  result = Pony.mail options
  options.tap { |x| x.delete(:via_options) }
end