Class: Cyclid::API::Plugins::Email

Inherits:
Action show all
Defined in:
app/cyclid/plugins/action/email.rb

Overview

Email notification plugin

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Action

human_name, #prepare

Methods inherited from Base

get_config, human_name, register_plugin, set_config

Constructor Details

#initialize(args = {}) ⇒ Email

Returns a new instance of Email.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/cyclid/plugins/action/email.rb', line 28

def initialize(args = {})
  args.symbolize_keys!

  raise 'an email action requires a message' unless args.include? :message
  @message = args[:message]

  raise 'an email action requires a recipient' unless args.include? :to
  @to = args[:to]

  @subject = args[:subject] || 'Cyclid notification'
  @color = args[:color] || 'dodgerblue'
end

Class Method Details

.config?Boolean

This plugin has configuration data

Returns:

  • (Boolean)


161
162
163
# File 'app/cyclid/plugins/action/email.rb', line 161

def config?
  true
end

.config_schemaObject

Config schema for the email plugin



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/cyclid/plugins/action/email.rb', line 183

def config_schema
  schema = []
  schema << { name: 'server',
              type: 'string',
              description: 'SMTP server for outgoing emails',
              default: nil }
  schema << { name: 'port',
              type: 'integer',
              description: 'SMTP server port to connect to',
              default: nil }
  schema << { name: 'from',
              type: 'string',
              description: 'Sender email address',
              default: nil }
  schema << { name: 'username',
              type: 'string',
              description: 'SMTP server username',
              default: nil }
  schema << { name: 'password',
              type: 'password',
              description: 'SMTP server password',
              default: nil }

  return schema
end

.default_configObject

Default configuration for the email plugin



171
172
173
174
175
176
177
178
179
180
# File 'app/cyclid/plugins/action/email.rb', line 171

def default_config
  config = {}
  config['server'] = nil
  config['port'] = nil
  config['from'] = nil
  config['username'] = nil
  config['password'] = nil

  return config
end

.update_config(current, new) ⇒ Object

Update the plugin configuration



166
167
168
# File 'app/cyclid/plugins/action/email.rb', line 166

def update_config(current, new)
  current.merge! new
end

Instance Method Details

#perform(log) ⇒ Object

Send an email to the configured recipient; the message is rendered into text & HTML via. an ERB template which inserts additional information from the context



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/cyclid/plugins/action/email.rb', line 44

def perform(log)
  begin
    # Retrieve the server-wide email configuration
    config = Cyclid.config.plugins
    email_config = load_email_config(config)

    Cyclid.logger.debug "sending via. #{email_config[:server]}:#{email_config[:port]} " \
                        "as #{email_config[:from]}"

    # Add the job context
    to = @to ** @ctx
    subject = @subject ** @ctx
    message = @message ** @ctx

    # Create a binding for the text & HTML ERB templates
    info = { color: @color, title: subject }

    bind = binding
    bind.local_variable_set(:info, info)
    bind.local_variable_set(:ctx, @ctx)
    bind.local_variable_set(:message, message)

    # Generate text email from a template
    template_path = File.expand_path(File.join(__FILE__, '..', 'email', 'text.erb'))
    template = ERB.new(File.read(template_path), nil, '%<>-')

    text_body = template.result(bind)

    # Generate the HTML email from a template
    template_path = File.expand_path(File.join(__FILE__, '..', 'email', 'html.erb'))
    template = ERB.new(File.read(template_path), nil, '%<>-')

    html = template.result(bind)

    # Run the HTML through Premailer to inline the styles
    premailer = Premailer.new(html,
                              with_html_string: true,
                              warn_level: Premailer::Warnings::SAFE)
    html_body = premailer.to_inline_css

    # Create the email
    mail = Mail.new
    mail.from = email_config[:from]
    mail.to = to
    mail.subject = subject
    mail.text_part do
      body text_body
    end
    mail.html_part do
      content_type 'text/html; charset=UTF8'
      body html_body
    end

    # Deliver the email via. the configured server, using
    # authentication if a username & password were provided.
    log.write("sending email to #{@to}")

    mail.delivery_method :smtp, address: email_config[:server],
                                port: email_config[:port],
                                user_name: email_config[:username],
                                password: email_config[:password]
    mail.deliver

    success = true
  rescue StandardError => ex
    log.write "#{ex.message}\n"
    success = false
  end

  [success, 0]
end