Class: Argosnap::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/argosnap/notifications/mailer.rb

Overview

Send email notification to the user

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, logger) ⇒ Mailer

Returns a new instance of Mailer.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/argosnap/notifications/mailer.rb', line 12

def initialize(config, logger)
  @logger              = logger
  @smtpd_user          = config[:smtp][:smtpd_user]
  @smtpd_password      = config[:smtp][:smtpd_password]
  @smtpd_address       = config[:smtp][:smtpd_address]
  @smtpd_port          = config[:smtp][:smtpd_port]
  @smtpd_from          = config[:smtp][:smtpd_from]
  @smtpd_to            = config[:smtp][:smtpd_to]
  @method              = config[:smtp][:email_delivery_method]
  @format              = config[:smtp][:format]
  @balance             = Fetch.new.balance.to_s
end

Instance Attribute Details

#balanceObject (readonly)

Returns the value of attribute balance.



11
12
13
# File 'lib/argosnap/notifications/mailer.rb', line 11

def balance
  @balance
end

#formatObject (readonly)

Returns the value of attribute format.



11
12
13
# File 'lib/argosnap/notifications/mailer.rb', line 11

def format
  @format
end

#loggerObject (readonly)

Returns the value of attribute logger.



11
12
13
# File 'lib/argosnap/notifications/mailer.rb', line 11

def logger
  @logger
end

#methodObject (readonly)

Returns the value of attribute method.



11
12
13
# File 'lib/argosnap/notifications/mailer.rb', line 11

def method
  @method
end

#smtpd_addressObject (readonly)

Returns the value of attribute smtpd_address.



11
12
13
# File 'lib/argosnap/notifications/mailer.rb', line 11

def smtpd_address
  @smtpd_address
end

#smtpd_fromObject (readonly)

Returns the value of attribute smtpd_from.



11
12
13
# File 'lib/argosnap/notifications/mailer.rb', line 11

def smtpd_from
  @smtpd_from
end

#smtpd_passwordObject (readonly)

Returns the value of attribute smtpd_password.



11
12
13
# File 'lib/argosnap/notifications/mailer.rb', line 11

def smtpd_password
  @smtpd_password
end

#smtpd_portObject (readonly)

Returns the value of attribute smtpd_port.



11
12
13
# File 'lib/argosnap/notifications/mailer.rb', line 11

def smtpd_port
  @smtpd_port
end

#smtpd_toObject (readonly)

Returns the value of attribute smtpd_to.



11
12
13
# File 'lib/argosnap/notifications/mailer.rb', line 11

def smtpd_to
  @smtpd_to
end

#smtpd_userObject (readonly)

Returns the value of attribute smtpd_user.



11
12
13
# File 'lib/argosnap/notifications/mailer.rb', line 11

def smtpd_user
  @smtpd_user
end

Instance Method Details

#configObject

load configuration files



7
8
9
# File 'lib/argosnap/notifications/mailer.rb', line 7

def config 
  Configuration.new
end

#ensure_mail_configurationObject



25
26
27
28
29
30
31
# File 'lib/argosnap/notifications/mailer.rb', line 25

def ensure_mail_configuration
  if smtpd_address.empty?
    config.log_and_abort("There is no 'smtpd_address' in #{config}. Please check your configuration file.")
  elsif smtpd_to.empty?
    config.log_and_abort("There is no 'smtpd_to' in #{config}. Please check your configuration file.")
  end
end

#send_mailObject

send email. HTML format is the default.



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
# File 'lib/argosnap/notifications/mailer.rb', line 57

def send_mail
  # ensure compatibility first
  Install.new.ensure_compatibility
  ensure_mail_configuration

  # create the 'mail' object
  mail           = Mail.new

  # configure mail options
  mail[:from]    =  smtpd_from
  mail[:to]      =  smtpd_to
  mail[:subject] = "ARGOSNAP: your tarsnap balance."

  # configure mail format: Use HTML if the user wants it, otherwise default to 'txt'
  if format == 'html'
    if config.gem_available?('haml')
      require 'haml'
      mail['content-type'] = 'text/html; charset=UTF-8'
      # convert HAML to HTML
      Haml::Engine.new(File.read(File.expand_path('../../../../files/mail.body.haml', __FILE__))).def_method(balance, :render)
      mail[:body] = balance.render
    else
      log.error("Please install haml gem to receive HTML emails: '$ gem install haml'")
      mail[:body] = text_message(balance)
    end
  else
    mail[:body] =  text_message(balance)
  end

  # SMTPd configuration. Defaults to local 'sendmail'
  if method == 'smtp'
    if smtpd_port == 465
      # use smtps with some defaults
      mail.delivery_method :smtp, address: smtpd_address, port: smtpd_port, user_name: smtpd_user, password: smtpd_password, ssl: true, openssl_verify_mode: 0
    else
      mail.delivery_method :smtp, address: smtpd_address, port: smtpd_port
    end
  end

  # dispatch email
  mail.deliver
  logger.info("Argosnap sent an email to #{smtpd_to}!")
  logger.info("Current amount in picoUSD: #{balance}")
end

#text_message(amount) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/argosnap/notifications/mailer.rb', line 33

def text_message(amount)
  message = <<EOF

Hello

Your tarsnap account is running out of funds. Your current amount of picoUSD is #{amount}.

This automated email message was sent by argosnap. Please do not reply to this email.

Have a nice day!

argosnap
--

Argosnap: Receive notifications when your tarsnap account is running out of picoUSD!
URL:      https://github.com/atmosx/argosnap

EOF

  message
end