Class: Minimail::Mail

Inherits:
Object
  • Object
show all
Defined in:
lib/minimail/mail.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Mail

Returns a new instance of Mail.



5
6
7
8
9
10
11
12
13
14
# File 'lib/minimail/mail.rb', line 5

def initialize(params = {})
  mail_program?

  @recipients = params[:recipients] || []
  @subject = params[:subject] || ""
  @body = params[:body] || ""
  @attachments = if encode_program? && params[:attachments]
    process_attachments( [params[:attachments]].flatten )
  end
end

Instance Attribute Details

#attachmentsObject

Returns the value of attribute attachments.



3
4
5
# File 'lib/minimail/mail.rb', line 3

def attachments
  @attachments
end

#body(body = '') ⇒ Object

Returns the value of attribute body.



3
4
5
# File 'lib/minimail/mail.rb', line 3

def body
  @body
end

#recipients(recipients = nil) ⇒ Object

Returns the value of attribute recipients.



3
4
5
# File 'lib/minimail/mail.rb', line 3

def recipients
  @recipients
end

#subject(subject = nil) ⇒ Object

Returns the value of attribute subject.



3
4
5
# File 'lib/minimail/mail.rb', line 3

def subject
  @subject
end

Instance Method Details

#deliverObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/minimail/mail.rb', line 28

def deliver
  return "Invalid mail contents" unless valid?
  return "Invalid mail command" unless mail_command
  if @attachments
    IO.popen("(#{@attachments}) | #{mail_command} -s '#{@subject}' #{@recipients}", "w") do |io|
      io.write @body
      io.close_write
      io
    end
  else
    IO.popen("#{mail_command} -s '#{@subject}' #{@recipients}", "w") do |io|
      io.write @body
      io.close_write
      io
    end
  end
end

#draft(&block) ⇒ Object



55
56
57
# File 'lib/minimail/mail.rb', line 55

def draft(&block)
  instance_eval(&block)
end

#mail_commandObject



46
47
48
49
# File 'lib/minimail/mail.rb', line 46

def mail_command
  return '/usr/bin/mail' if command?('/usr/bin/mail')
  return '/bin/mail' if command?('/bin/mail')
end

#valid?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/minimail/mail.rb', line 51

def valid?
  @recipients.empty? ? false : true
end