Class: Aspera::Cli::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/cli/mailer.rb

Overview

Email service injected into Context as :mailer so that any component (TransferAgent, plugins, ...) can send emails via SMTP and ERB templates. Provides :smtp, :notify_to, :notify_template options via the Options manager.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, main_folder) ⇒ Mailer

Returns a new instance of Mailer.

Parameters:

  • options (Parser) —

    CLI options manager (provides :smtp, :notify_to, :notify_template)

  • main_folder (String) —

    application main folder (unused directly but kept for symmetry)



20
21
22
23
# File 'lib/aspera/cli/mailer.rb', line 20

def initialize(options, main_folder)
  @options     = options
  @main_folder = main_folder
end

Instance Attribute Details

#options ⇒ Parser (readonly)

Returns:



26
27
28
# File 'lib/aspera/cli/mailer.rb', line 26

def options
  @options
end

Instance Method Details

#email_settings ⇒ Hash

Returns email server settings with defaults applied.

Returns:

  • (Hash) —

    email server settings with defaults applied



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/aspera/cli/mailer.rb', line 29

def email_settings
  # Keys and types of values: see schema `SmtpOptions`
  smtp = options.get_option(:smtp, mandatory: true, complete: true)
  # Change keys from string into symbol, a `nil` value is a removed key
  smtp = smtp.symbolize_keys.compact
  # smtp[:ssl] = nil (false)
  smtp[:tls] = !smtp[:ssl] unless smtp.key?(:tls)
  smtp[:port] ||= if smtp[:tls]
    587
  elsif smtp[:ssl]
    465
  else
    25
  end
  smtp[:from_email] ||= smtp[:username] if smtp.key?(:username)
  smtp[:from_name] ||= smtp[:from_email].sub(/@.*$/, '').gsub(/[^a-zA-Z]/, ' ').capitalize if smtp.key?(:username)
  smtp[:domain] ||= smtp[:from_email].sub(/^.*@/, '') if smtp.key?(:from_email)
  Aspera.assert(smtp.key?(:domain), type: BadArgument) { 'Option smtp: missing domain: set domain, from_email or username' }
  Log.dump(:smtp, smtp)
  return smtp
end

#send_email_template(email_template_default: nil, values: {}) ⇒ Object

Send email using ERB template

Parameters:

  • email_template_default (String) (defaults to: nil) —

    default template, can be overridden by option

  • values (Hash) (defaults to: {}) —

    values to be used in template, keys with default: to, from_name, from_email



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
# File 'lib/aspera/cli/mailer.rb', line 54

def send_email_template(email_template_default: nil, values: {})
  values[:to] ||= options.get_option(:notify_to, mandatory: true)
  notify_template = options.get_option(:notify_template, mandatory: email_template_default.nil?) || email_template_default
  mail_conf = email_settings
  values[:from_name] ||= mail_conf[:from_name]
  values[:from_email] ||= mail_conf[:from_email]
  %i[to from_email].each do |n|
    Aspera.assert_type(values[n], String, type: Cli::BadArgument) { "Missing email parameter: #{n} in config" }
  end
  start_options = [mail_conf[:domain]]
  start_options.push(mail_conf[:username], mail_conf[:password], :login) if mail_conf.key?(:username) && mail_conf.key?(:password)
  template_binding = Environment.empty_binding
  values.each do |k, v|
    Aspera.assert_type(k, Symbol)
    template_binding.local_variable_set(k, v)
  end
  msg_with_headers = ERB.new(notify_template).result(template_binding)
  Log.dump(:msg_with_headers, msg_with_headers)
  require 'net/smtp'
  smtp = Net::SMTP.new(mail_conf[:server], mail_conf[:port])
  smtp.enable_starttls if mail_conf[:tls]
  smtp.enable_tls if mail_conf[:ssl]
  smtp.start(*start_options) do |smtp_session|
    smtp_session.send_message(msg_with_headers, values[:from_email], values[:to])
  end
  nil
end