Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit aac669686e2cf1da757757cb8adc3bbcc829cbe6 0 parents
Ryan Bates ryanb authored
5 .gitignore
@@ -0,0 +1,5 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
+tmp/*
2  .rspec
@@ -0,0 +1,2 @@
+--color
+--backtrace
1  .rvmrc
@@ -0,0 +1 @@
+rvm use 1.9.2@letter_opener --create
2  Gemfile
@@ -0,0 +1,2 @@
+source "http://rubygems.org"
+gemspec
9 Rakefile
@@ -0,0 +1,9 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+require 'rspec/core/rake_task'
+
+desc "Run RSpec"
+RSpec::Core::RakeTask.new do |t|
+ t.verbose = false
+end
+task :default => :spec
19 letter_opener.gemspec
@@ -0,0 +1,19 @@
+Gem::Specification.new do |s|
+ s.name = "letter_opener"
+ s.version = "0.0.1"
+ s.author = "Ryan Bates"
+ s.email = "ryan@railscasts.com"
+ s.homepage = "http://github.com/ryanb/letter_opener"
+ s.summary = "Preview mail in browser instead of sending."
+ s.description = "When mail is sent from your application, Open Mail will open a preview in the browser instead of sending."
+
+ s.files = Dir["{lib,spec}/**/*", "[A-Z]*"] - ["Gemfile.lock"]
+ s.require_path = "lib"
+
+ s.add_dependency 'launchy'
+ s.add_development_dependency 'rspec', '~> 2.6.0'
+ s.add_development_dependency 'mail', '~> 2.3.0'
+
+ s.rubyforge_project = s.name
+ s.required_rubygems_version = ">= 1.3.4"
+end
3  lib/letter_opener.rb
@@ -0,0 +1,3 @@
+require "fileutils"
+require "digest/sha1"
+require "letter_opener/delivery_method"
17 lib/letter_opener/delivery_method.rb
@@ -0,0 +1,17 @@
+module LetterOpener
+ class DeliveryMethod
+ def initialize(options = {})
+ @options = {:location => './letter_opener'}.merge!(options)
+ end
+
+ def deliver!(mail)
+ identity = Time.now.to_i.to_s + Digest::SHA1.hexdigest(mail.encoded)[0..6]
+ path = File.join(@options[:location], identity)
+ FileUtils.mkdir_p(path)
+ File.open(File.join(path, "index.html"), 'w') do |f|
+ template = File.expand_path("../views/index.html.erb", __FILE__)
+ f.write ERB.new(File.read(template)).result(binding)
+ end
+ end
+ end
+end
82 lib/letter_opener/views/index.html.erb
@@ -0,0 +1,82 @@
+<style type="text/css">
+ #message_headers {
+ position: absolute;
+ top: 0px;
+ left: 0;
+ width: 100%;
+ height: 85px;
+ padding: 10px 0 0 0;
+ margin: 0;
+ background: #fff;
+ font-size: 12px;
+ font-family: "Lucida Grande";
+ border-bottom: 1px solid #dedede;
+ overflow: hidden;
+ }
+
+ #message_headers dl {
+ margin: 0;
+ padding: 0;
+ }
+
+ #message_headers dt {
+ width: 60px;
+ padding: 1px;
+ float: left;
+ text-align: right;
+ font-weight: bold;
+ color: #7f7f7f;
+ }
+
+ #message_headers dd {
+ margin-left: 70px;
+ padding: 1px;
+ }
+
+ #message_headers p.alternate {
+ position: absolute;
+ top: 0;
+ right: 15px;
+ }
+
+ #message_headers p.alternate a {
+ color: #09c;
+ }
+
+ pre#message_body {
+ padding: 10px;
+ word-wrap: break-word;
+ }
+
+ body {
+ margin-top: 96px;
+ }
+</style>
+
+<div id="message_headers">
+ <dl>
+ <dt>From:</dt>
+ <dd><%= mail.from %></dd>
+
+ <dt>Subject:</dt>
+ <dd><strong><%= mail.subject %></strong></dd>
+
+ <dt>Date:</dt>
+ <dd><%= Time.now.strftime("%b %e, %Y %I:%M:%S %p %Z") %></dd>
+
+ <dt>To:</dt>
+ <dd><%= mail.to %></dd>
+ </dl>
+
+ <% if false && mail.multipart? %>
+ <p class="alternate">
+ <% if body_part.content_type && body_part.content_type.match(/text\/html/) %>
+ <a href="<%= name %>.txt">View plain text version</a>
+ <% else %>
+ <a href="<%= name %>.html">View HTML version</a>
+ <% end %>
+ </p>
+ <% end %>
+</div>
+
+<pre id="message_body"><%= mail.body %></pre>
24 spec/letter_opener/delivery_method_spec.rb
@@ -0,0 +1,24 @@
+require "spec_helper"
+
+describe LetterOpener::DeliveryMethod do
+ before(:each) do
+ location = File.expand_path('../../../tmp/letter_opener', __FILE__)
+ FileUtils.rm_rf(location)
+ Mail.defaults do
+ delivery_method LetterOpener::DeliveryMethod, :location => location
+ end
+ @location = location
+ end
+
+ it "saves text into html document" do
+ mail = Mail.deliver do
+ from 'foo@example.com'
+ to 'bar@example.com'
+ subject 'Hello'
+ body 'World!'
+ end
+ html = File.read(Dir["#{@location}/*/index.html"].first)
+ html.should include("Hello")
+ html.should include("World!")
+ end
+end
8 spec/spec_helper.rb
@@ -0,0 +1,8 @@
+require 'rubygems'
+require 'bundler/setup'
+Bundler.require(:default)
+
+require "mail"
+
+RSpec.configure do |config|
+end
Please sign in to comment.
Something went wrong with that request. Please try again.