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.
Constant Summary collapse
- SMTP_CONF_PARAMS =
i[server tls ssl port domain username password from_name from_email].freeze
- SMTP_BOOL_PARAMS =
i[tls ssl].freeze
- SMTP_INT_PARAMS =
i[port].freeze
- SMTP_STR_PARAMS =
i[server domain username password from_email from_name].freeze
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.
21 22 23 24 |
# File 'lib/aspera/cli/mailer.rb', line 21 def initialize(, main_folder) = @main_folder = main_folder end |
Instance Attribute Details
Instance Method Details
#email_settings ⇒ Hash
Returns email server settings with defaults applied.
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 |
# File 'lib/aspera/cli/mailer.rb', line 30 def email_settings smtp = .get_option(:smtp, mandatory: true) # Change keys from string into symbol smtp = smtp.symbolize_keys unsupported = smtp.keys - SMTP_CONF_PARAMS Aspera.assert(unsupported.empty?, type: Cli::Error){"Unsupported SMTP parameter: #{unsupported.join(', ')}, use: #{SMTP_CONF_PARAMS.join(', ')}"} # Boolean fields must be actual booleans, not strings like "false" SMTP_BOOL_PARAMS.each do |k| Aspera.assert_values(smtp[k], [true, false], type: Cli::Error){"smtp.#{k}"} if smtp.key?(k) end SMTP_INT_PARAMS.each do |k| Aspera.assert_type(smtp[k], Integer, type: Cli::Error){"smtp.#{k}"} if smtp.key?(k) end SMTP_STR_PARAMS.each do |k| Aspera.assert_type(smtp[k], String, type: Cli::Error){"smtp.#{k}"} if smtp.key?(k) end # 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) i[server port domain].each do |n| Aspera.assert(smtp.key?(n)){"Missing mandatory smtp parameter: #{n}"} end Log.log.debug{"smtp=#{smtp}"} return smtp end |
#send_email_template(email_template_default: nil, values: {}) ⇒ Object
Send email using ERB template
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 |
# File 'lib/aspera/cli/mailer.rb', line 68 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){"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 |