Skip to content

Instantly share code, notes, and snippets.

@e1senh0rn
Created February 15, 2012 16:31
Show Gist options
  • Select an option

  • Save e1senh0rn/1837115 to your computer and use it in GitHub Desktop.

Select an option

Save e1senh0rn/1837115 to your computer and use it in GitHub Desktop.
Simple web mail viewer
require 'base64'
class WebMailViewer < Sinatra::Base
configure(:integration, :development, :test) do
enable :logging
end
before do
if ActionMailer::Base.delivery_method != :file
halt "Sorry, but this feature works only for delivery_method == :file"
end
halt "Sorry, no local emails found" unless File.exists?(Rails.root.join('tmp/mails'))
end
helpers do
def list_files
Dir.glob(Rails.root.join('tmp/mails/*')).map do |file|
{
:updated_at => File.mtime(file),
:name => File.basename(file),
:file => file
}
end.sort_by{|i| i[:updated_at]}.reverse
end
def encode_address(str)
Base64.encode64 str
end
def decode_address(str)
Base64.decode64 str
end
end
get "/" do
mails = list_files
return "Sorry, no local emails found" if mails.empty?
mails.map do |file|
"#{file[:updated_at].to_s(:db)} -- <a href='#{url('/view/' + encode_address(file[:name]))}'>#{file[:name]}</a>"
end.join('<br />')
end
get "/view/:address" do
address = decode_address(params[:address])
mail = list_files.find {|file| file[:name] == address}
if mail
content_type 'text/plain', :charset => 'utf-8'
File.open(mail[:file], "rb").read
else
"No such file!"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment