Class: Aspera::Cli::Mailer
- Inherits:
-
Object
- Object
- Aspera::Cli::Mailer
- 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
- #options ⇒ Parser readonly
Instance Method Summary collapse
-
#email_settings ⇒ Hash
Email server settings with defaults applied.
-
#initialize(options, main_folder) ⇒ Mailer
constructor
A new instance of Mailer.
-
#send_email_template(email_template_default: nil, values: {}) ⇒ Object
Send email using ERB template.
Constructor Details
#initialize(options, main_folder) ⇒ Mailer
Returns a new instance of Mailer.
20 21 22 23 |
# File 'lib/aspera/cli/mailer.rb', line 20 def initialize(, main_folder) @options = @main_folder = main_folder end |
Instance Attribute Details
Instance Method Details
#email_settings ⇒ Hash
Returns 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 = .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
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] ||= .get_option(:notify_to, mandatory: true) notify_template = .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 = [mail_conf[:domain]] .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(*) do |smtp_session| smtp_session.(msg_with_headers, values[:from_email], values[:to]) end nil end |