Class: Subtrigger::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/subtrigger/email.rb

Overview

E-mail notifications

Sometimes you want to send notification e-mails after the hook has fired to inform developers or yourself of some event. This class is a simple wrapper around the standard sendmail program.

Usage example

Email.new(:to => '[email protected]',
          :from => '[email protected]',
          :subject => 'Fired',
          :body => 'Your post-commit hook has just fired').send

If sendmail can not be found on your system an exception will be raised. – TODO: Use a hash of options rather than plain arguments.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Email

Sets up a new message and tries to find sendmail on your system.



23
24
25
26
27
28
29
30
31
# File 'lib/subtrigger/email.rb', line 23

def initialize(options = {})
  @to          = options[:to]
  @from        = options[:from]
  @subject     = options[:subject]
  @body        = options[:body]
  @development = options[:development] || false
  @sendmail    = Subtrigger.sendmail || `which sendmail`.strip
  raise 'Could not find sendmail; aborting.' if @sendmail.nil?
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



19
20
21
# File 'lib/subtrigger/email.rb', line 19

def body
  @body
end

#developmentObject

Returns the value of attribute development.



19
20
21
# File 'lib/subtrigger/email.rb', line 19

def development
  @development
end

#fromObject

Returns the value of attribute from.



19
20
21
# File 'lib/subtrigger/email.rb', line 19

def from
  @from
end

#sendmailObject (readonly)

Returns the value of attribute sendmail.



20
21
22
# File 'lib/subtrigger/email.rb', line 20

def sendmail
  @sendmail
end

#subjectObject

Returns the value of attribute subject.



19
20
21
# File 'lib/subtrigger/email.rb', line 19

def subject
  @subject
end

#toObject

Returns the value of attribute to.



19
20
21
# File 'lib/subtrigger/email.rb', line 19

def to
  @to
end

Instance Method Details

#sendObject

Tries to use sendmail to send the message.



34
35
36
37
38
39
40
41
42
# File 'lib/subtrigger/email.rb', line 34

def send
  message = header + "\n" + body
  unless development
    fd = open("|#{sendmail} #{to}", "w")
    fd.print(message)
    fd.close
  end
  message
end