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.

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.



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

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.



17
18
19
# File 'lib/subtrigger/email.rb', line 17

def body
  @body
end

#developmentObject

Returns the value of attribute development.



17
18
19
# File 'lib/subtrigger/email.rb', line 17

def development
  @development
end

#fromObject

Returns the value of attribute from.



17
18
19
# File 'lib/subtrigger/email.rb', line 17

def from
  @from
end

#sendmailObject (readonly)

Returns the value of attribute sendmail.



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

def sendmail
  @sendmail
end

#subjectObject

Returns the value of attribute subject.



17
18
19
# File 'lib/subtrigger/email.rb', line 17

def subject
  @subject
end

#toObject

Returns the value of attribute to.



17
18
19
# File 'lib/subtrigger/email.rb', line 17

def to
  @to
end

Instance Method Details

#sendObject

Tries to use sendmail to send the message. The message sent is returned for inspecting purposes.



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

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