Class: Mail::FileDelivery

Inherits:
Object
  • Object
show all
Defined in:
lib/mail/network/delivery_methods/file_delivery.rb

Overview

FileDelivery class delivers emails into multiple files based on the destination address. Each file is appended to if it already exists.

So if you have an email going to fred@test, bob@test, joe@anothertest, and you set your location path to /path/to/mails then FileDelivery will create the directory if it does not exist, and put one copy of the email in three files, called “fred@test”, “bob@test” and “joe@anothertest”

Make sure the path you specify with :location is writable by the Ruby process running Mail.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ FileDelivery

Returns a new instance of FileDelivery.



21
22
23
# File 'lib/mail/network/delivery_methods/file_delivery.rb', line 21

def initialize(values)
  self.settings = { :location => './mails' }.merge!(values)
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.



25
26
27
# File 'lib/mail/network/delivery_methods/file_delivery.rb', line 25

def settings
  @settings
end

Instance Method Details

#deliver!(mail) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mail/network/delivery_methods/file_delivery.rb', line 27

def deliver!(mail)
  if ::File.respond_to?(:makedirs)
    ::File.makedirs settings[:location]
  else
    ::FileUtils.mkdir_p settings[:location]
  end

  mail.destinations.uniq.each do |to|
    ::File.open(::File.join(settings[:location], to), 'a') { |f| "#{f.write(mail.encoded)}\r\n\r\n" }
  end
end