Class: Sinatra::Mailer::Email

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(o = {}) ⇒ Email

Parameters

o<Hash=> Object>

Configuration commands to send to MailFactory.



96
97
98
99
100
101
102
# File 'lib/diddies/mailer.rb', line 96

def initialize(o={})
  self.config = Mailer.config || {:sendmail_path => '/usr/sbin/sendmail'}
  o[:rawhtml] = o.delete(:html)
  m = MailFactory.new()
  o.each { |k,v| m.send "#{k}=", v }
  @mail = m
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



50
51
52
# File 'lib/diddies/mailer.rb', line 50

def config
  @config
end

#mailObject

Returns the value of attribute mail.



50
51
52
# File 'lib/diddies/mailer.rb', line 50

def mail
  @mail
end

Instance Method Details

#attach(file_or_files, filename = file_or_files.is_a?(File) ? File.basename(file_or_files.path) : nil, type = nil, headers = nil) ⇒ Object

Parameters

file_or_files<File, Array>

File(s) to attach.

filename<String>
type<~to_s>

The attachment MIME type. If left out, it will be determined from file_or_files.

headers<String, Array>

Additional attachment headers.

Raises

ArgumentError

file_or_files was not a File or an Array of File instances.



84
85
86
87
88
89
90
91
92
# File 'lib/diddies/mailer.rb', line 84

def attach(file_or_files, filename = file_or_files.is_a?(File) ? File.basename(file_or_files.path) : nil, 
  type = nil, headers = nil)
  if file_or_files.is_a?(Array)
    file_or_files.each {|k,v| @mail.add_attachment_as k, *v}
  else
    raise ArgumentError, "You did not pass in a file. Instead, you sent a #{file_or_files.class}" if !file_or_files.is_a?(File)
    @mail.add_attachment_as(file_or_files, filename, type, headers)
  end
end

#deliver!Object

Delivers the mail with the specified delivery method, defaulting to net_smtp.



69
70
71
# File 'lib/diddies/mailer.rb', line 69

def deliver!
  send(Mailer.delivery_method || :net_smtp)
end

#net_smtpObject

Sends the mail using SMTP.



60
61
62
63
64
65
# File 'lib/diddies/mailer.rb', line 60

def net_smtp
  Net::SMTP.start(config[:host], config[:port].to_i, config[:domain], 
                  config[:user], config[:pass], config[:auth]) { |smtp|
    smtp.send_message(@mail.to_s, @mail.from.first, @mail.to.to_s.split(/[,;]/))
  }
end

#sendmailObject

Sends the mail using sendmail.



53
54
55
56
57
# File 'lib/diddies/mailer.rb', line 53

def sendmail
  sendmail = IO.popen("#{config[:sendmail_path]} #{@mail.to}", 'w+') 
  sendmail.puts @mail.to_s
  sendmail.close
end