Class | TMail::UNIXMbox |
In: |
lib/tmail/mailbox.rb
|
Parent: | Object |
new | -> | newobj |
make _From line
# File lib/tmail/mailbox.rb, line 271 def UNIXMbox.create_from_line( port ) sprintf 'From %s %s', fromaddr(), TextUtils.time2str(File.mtime(port.filename)) end
# File lib/tmail/mailbox.rb, line 276 def UNIXMbox.fromaddr(port) h = HeaderField.new_from_port(port, 'Return-Path') || HeaderField.new_from_port(port, 'From') || HeaderField.new_from_port(port, 'EnvelopeSender') or return 'nobody' a = h.addrs[0] or return 'nobody' a.spec end
# File lib/tmail/mailbox.rb, line 222 def UNIXMbox.lock( fname ) begin f = File.open(fname, 'r+') f.flock File::LOCK_EX yield f ensure f.flock File::LOCK_UN f.close if f and not f.closed? end end
# File lib/tmail/mailbox.rb, line 248 def UNIXMbox.mkfinal( mh, mboxfile, writeback_p, cleanup_p ) lambda { if writeback_p lock(mboxfile) {|f| mh.each_port do |port| f.puts create_from_line(port) port.ropen {|r| f.puts r.read } end } end if cleanup_p Dir.foreach(mh.dirname) do |fname| next if /\A\.\.?\z/ === fname File.unlink "#{mh.dirname}/#{fname}" end Dir.rmdir mh.dirname end } end
Creates a new mailbox object that you can iterate through to collect the emails from with "each_port".
You need to pass it a filename of a unix mailbox format file, the format of this file can be researched at this page at wikipedia
filename: The filename of the mailbox you want to open
tmpdir: Can be set to override TMail using the system environment‘s temp dir. TMail will first use the temp dir specified by you (if any) or then the temp dir specified in the Environment‘s TEMP value then the value in the Environment‘s TMP value or failing all of the above, ’/tmp‘
readonly: If set to false, each email you take from the mail box will be removed from the mailbox. default is false - ie, it WILL truncate your mailbox file to ZERO once it has read the emails out.
None
# First show using readonly true: require 'ftools' File.size("../test/fixtures/mailbox") #=> 20426 mailbox = TMail::UNIXMbox.new("../test/fixtures/mailbox", nil, true) #=> #<TMail::UNIXMbox:0x14a2aa8 @readonly=true.....> mailbox.each_port do |port| mail = TMail::Mail.new(port) puts mail.subject end #Testing mailbox 1 #Testing mailbox 2 #Testing mailbox 3 #Testing mailbox 4 require 'ftools' File.size?("../test/fixtures/mailbox") #=> 20426 # Now show with readonly set to the default false mailbox = TMail::UNIXMbox.new("../test/fixtures/mailbox") #=> #<TMail::UNIXMbox:0x14a2aa8 @readonly=false.....> mailbox.each_port do |port| mail = TMail::Mail.new(port) puts mail.subject end #Testing mailbox 1 #Testing mailbox 2 #Testing mailbox 3 #Testing mailbox 4 File.size?("../test/fixtures/mailbox") #=> nil
# File lib/tmail/mailbox.rb, line 217 def UNIXMbox.new( filename, tmpdir = nil, readonly = false ) tmpdir = ENV['TEMP'] || ENV['TMP'] || '/tmp' newobj(filename, "#{tmpdir}/ruby_tmail_#{$$}_#{rand()}", readonly, false) end
# File lib/tmail/mailbox.rb, line 237 def initialize( fname, mhdir, readonly, static ) @filename = fname @readonly = readonly @closed = false Dir.mkdir mhdir @real = MhMailbox.new(mhdir) @finalizer = UNIXMbox.mkfinal(@real, @filename, !@readonly, !static) ObjectSpace.define_finalizer self, @finalizer end
# File lib/tmail/mailbox.rb, line 233 def UNIXMbox.static_new( fname, dir, readonly = false ) newobj(fname, dir, readonly, true) end
# File lib/tmail/mailbox.rb, line 284 def close return if @closed ObjectSpace.undefine_finalizer self @finalizer.call @finalizer = nil @real = nil @closed = true @updated = nil end
old #each_mail returns Port
def each_mail( &block )
each_port do |port| yield Mail.new(port) end
end
# File lib/tmail/mailbox.rb, line 318 def each_new_port( mtime = nil ) close_check update @real.each_new_port(mtime) {|p| yield p } end
# File lib/tmail/mailbox.rb, line 295 def each_port( &block ) close_check update @real.each_port(&block) end