class Backup::Notifier::Mail

Attributes

address[RW]

SMTP Server Address

authentication[RW]

Authentication type

Acceptable values: :plain, :login, :cram_md5

bcc[RW]

BCC receiver Email Address

cc[RW]

CC receiver Email Address

delivery_method[RW]

Mail delivery method to be used by the Mail gem.

Supported methods:

:smtp - ::Mail::SMTP (default)

Settings used by this method: {#address}, {#port}, {#domain}, {#user_name}, {#password}, {#authentication}, {#encryption}, {#openssl_verify_mode}

:sendmail - ::Mail::Sendmail

Settings used by this method: {#sendmail_args}

:exim - ::Mail::Exim

Settings used by this method: {#exim_args}

:file - ::Mail::FileDelivery

Settings used by this method: {#mail_folder}

domain[RW]

Your domain (if applicable)

encryption[RW]

Set the method of encryption to be used for the SMTP connection.

:starttls (default)

Use STARTTLS to upgrade the connection to a SSL/TLS connection.

:tls or :ssl

Use a SSL/TLS connection.

:none

No encryption will be used.

exim_args[RW]

Optional arguments to pass to ‘exim`

Note that this will override the defaults set by the Mail gem (currently: ‘-i -t’) So, if set here, be sure to set all the arguments you require.

Example: ‘-i -t -X/tmp/traffic.log’

from[RW]

Sender Email Address

mail_folder[RW]

Folder where mail will be kept when using the ‘:file` `delivery_method`.

Default location is ‘$HOME/Backup/emails’

openssl_verify_mode[RW]

OpenSSL Verify Mode

Valid modes: :none, :peer, :client_once, :fail_if_no_peer_cert See OpenSSL::SSL for details.

Use :none for a self-signed and/or wildcard certificate

password[RW]

SMTP Server Password (sender email’s credentials)

port[RW]

SMTP Server Port

reply_to[RW]

Set reply to email address

send_log_on[RW]

Array of statuses for which the log file should be attached.

Available statuses are: ‘:success`, `:warning` and `:failure`. Default: [:warning, :failure]

sendmail_args[RW]

Optional arguments to pass to ‘sendmail`

Note that this will override the defaults set by the Mail gem (currently: ‘-i’). So, if set here, be sure to set all the arguments you require.

Example: ‘-i -X/tmp/traffic.log’

to[RW]

Receiver Email Address

user_name[RW]

SMTP Server Username (sender email’s credentials)

Public Class Methods

new(model, &block) click to toggle source
Calls superclass method Backup::Notifier::Base::new
# File lib/backup/notifier/mail.rb, line 131
def initialize(model, &block)
  super
  instance_eval(&block) if block_given?

  @send_log_on ||= [:warning, :failure]
  @encryption  ||= :starttls
end

Private Instance Methods

new_email() click to toggle source

Configures the Mail gem by setting the defaults. Creates and returns a new email, based on the @delivery_method used.

# File lib/backup/notifier/mail.rb, line 182
def new_email
  method = %w(smtp sendmail exim file test)
    .index(@delivery_method.to_s) ? @delivery_method.to_s : "smtp"

  options =
    case method
    when "smtp"
      opts = {
        address: @address,
        port: @port,
        user_name: @user_name,
        password: @password,
        authentication: @authentication,
        enable_starttls_auto: @encryption == :starttls,
        openssl_verify_mode: @openssl_verify_mode,
        ssl: @encryption == :ssl,
        tls: @encryption == :tls
      }

      # Don't override default domain setting if domain not applicable.
      # ref https://github.com/mikel/mail/blob/2.6.3/lib/mail/network/delivery_methods/smtp.rb#L82
      opts[:domain] = @domain if @domain
      opts
    when "sendmail"
      opts = {}
      opts[:location] = utility(:sendmail)
      opts[:arguments] = @sendmail_args if @sendmail_args
      opts
    when "exim"
      opts = {}
      opts[:location] = utility(:exim)
      opts[:arguments] = @exim_args if @exim_args
      opts
    when "file"
      @mail_folder ||= File.join(Config.root_path, "emails")
      { location: File.expand_path(@mail_folder) }
    when "test" then {}
    end

  email = ::Mail.new
  email.delivery_method method.to_sym, options
  email.to       = to
  email.from     = from
  email.cc       = cc
  email.bcc      = bcc
  email.reply_to = reply_to
  email
end
notify!(status) click to toggle source

Notify the user of the backup operation results.

‘status` indicates one of the following:

‘:success` : The backup completed successfully. : Notification will be sent if `on_success` is `true`.

‘:warning` : The backup completed successfully, but warnings were logged. : Notification will be sent, including a copy of the current : backup log, if `on_warning` or `on_success` is `true`.

‘:failure` : The backup operation failed. : Notification will be sent, including a copy of the current : backup log, if `on_failure` is `true`.

# File lib/backup/notifier/mail.rb, line 160
def notify!(status)
  email = new_email
  email.subject = message.call(model, status: status_data_for(status))

  send_log = send_log_on.include?(status)
  template = Backup::Template.new(model: model, send_log: send_log)
  email.body = template.result(sprintf("notifier/mail/%s.erb", status.to_s))

  if send_log
    email.convert_to_multipart
    email.attachments["#{model.time}.#{model.trigger}.log"] = {
      mime_type: "text/plain;",
      content: Logger.messages.map(&:formatted_lines).flatten.join("\n")
    }
  end

  email.deliver! # raise error if unsuccessful
end