Class: Sinatra::Mailer::Email

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(o = {}) ⇒ Email

Parameters

o<Hash=> Object>

Configuration commands to send to MailFactory.



141
142
143
144
145
146
147
# File 'lib/mailer.rb', line 141

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.



85
86
87
# File 'lib/mailer.rb', line 85

def config
  @config
end

#mailObject

Returns the value of attribute mail.



85
86
87
# File 'lib/mailer.rb', line 85

def mail
  @mail
end

Class Method Details

.deliveriesObject



89
90
91
# File 'lib/mailer.rb', line 89

def self.deliveries
  @deliveries ||= [ ]
end

.deliveries=(value) ⇒ Object



86
87
88
# File 'lib/mailer.rb', line 86

def self.deliveries=(value)
  @deliveries = value
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.



129
130
131
132
133
134
135
136
137
# File 'lib/mailer.rb', line 129

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.



114
115
116
# File 'lib/mailer.rb', line 114

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

#net_smtpObject

Sends the mail using SMTP.



101
102
103
104
105
106
# File 'lib/mailer.rb', line 101

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.



94
95
96
97
98
# File 'lib/mailer.rb', line 94

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

#test_sendObject



108
109
110
# File 'lib/mailer.rb', line 108

def test_send
  self.class.deliveries << @mail
end