letter_opener 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,32 @@
1
+ = Letter Opener
2
+
3
+ Preview mail in your browser instead of sending it. When using the included delivery method, any sent mail will open automatically into the browser.
4
+
5
+ == Rails Setup
6
+
7
+ First add the gem to your development environment and run the +bundle+ command to install it.
8
+
9
+ gem "letter_opener", :group => :development
10
+
11
+ Then set the delivery method in <tt>config/environments/development.rb</tt>
12
+
13
+ config.action_mailer.delivery_method = :letter_opener
14
+
15
+ Now any delivered mail will pop up in your browser. The messages are stored in <tt>tmp/letter_opener</tt>.
16
+
17
+
18
+ == Other Setup
19
+
20
+ This can be used with anything that uses the Mail gem. Simply set the delivery method when configuring Mail and specify a location.
21
+
22
+ require "letter_opener"
23
+ Mail.defaults do
24
+ delivery_method LetterOpener::DeliveryMethod, :location => "tmp/letter_opener"
25
+ end
26
+
27
+
28
+ == Development & Feedback
29
+
30
+ Questions or comments? Please use the {issue tracker}[https://github.com/ryanb/letter_opener/issues]. If you would like to contribute to this project, clone this repository and run +bundle+ and +rake+ to run the tests.
31
+
32
+ Special thanks to the {mail_view}[https://github.com/37signals/mail_view/] gem for inspiring this project and for their mail template.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc "Run RSpec"
6
+ RSpec::Core::RakeTask.new do |t|
7
+ t.verbose = false
8
+ end
9
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ require "fileutils"
2
+ require "digest/sha1"
3
+ require "cgi"
4
+
5
+ require "letter_opener/message"
6
+ require "letter_opener/delivery_method"
7
+ require "letter_opener/railtie" if defined? Rails
@@ -0,0 +1,15 @@
1
+ module LetterOpener
2
+ class DeliveryMethod
3
+ def initialize(options = {})
4
+ @options = {:location => './letter_opener'}.merge!(options)
5
+ end
6
+
7
+ def deliver!(mail)
8
+ location = File.join(@options[:location], "#{Time.now.to_i}_#{Digest::SHA1.hexdigest(mail.encoded)[0..6]}")
9
+ messages = mail.parts.map { |part| Message.new(location, mail, part) }
10
+ messages << Message.new(location, mail) if messages.empty?
11
+ messages.each { |message| message.render }
12
+ Launchy.open("file://#{messages.first.filepath}")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,86 @@
1
+ <style type="text/css">
2
+ #message_headers {
3
+ position: absolute;
4
+ top: 0px;
5
+ left: 0;
6
+ width: 100%;
7
+ height: 85px;
8
+ padding: 10px 0 0 0;
9
+ margin: 0;
10
+ background: #fff;
11
+ font-size: 12px;
12
+ font-family: "Lucida Grande";
13
+ border-bottom: 1px solid #dedede;
14
+ overflow: hidden;
15
+ }
16
+
17
+ #message_headers dl {
18
+ margin: 0;
19
+ padding: 0;
20
+ }
21
+
22
+ #message_headers dt {
23
+ width: 60px;
24
+ padding: 1px;
25
+ float: left;
26
+ text-align: right;
27
+ font-weight: bold;
28
+ color: #7f7f7f;
29
+ }
30
+
31
+ #message_headers dd {
32
+ margin-left: 70px;
33
+ padding: 1px;
34
+ }
35
+
36
+ #message_headers p.alternate {
37
+ position: absolute;
38
+ top: 0;
39
+ right: 15px;
40
+ }
41
+
42
+ #message_headers p.alternate a {
43
+ color: #09c;
44
+ }
45
+
46
+ pre#message_body {
47
+ padding: 10px;
48
+ word-wrap: break-word;
49
+ }
50
+
51
+ body {
52
+ margin-top: 96px;
53
+ }
54
+ </style>
55
+
56
+ <div id="message_headers">
57
+ <dl>
58
+ <dt>From:</dt>
59
+ <dd><%= mail.from.join(", ") %></dd>
60
+
61
+ <dt>Subject:</dt>
62
+ <dd><strong><%= mail.subject %></strong></dd>
63
+
64
+ <dt>Date:</dt>
65
+ <dd><%= Time.now.strftime("%b %e, %Y %I:%M:%S %p %Z") %></dd>
66
+
67
+ <dt>To:</dt>
68
+ <dd><%= mail.to.join(", ") %></dd>
69
+ </dl>
70
+
71
+ <% if mail.multipart? %>
72
+ <p class="alternate">
73
+ <% if type == "plain" %>
74
+ <a href="rich.html">View HTML version</a>
75
+ <% else %>
76
+ <a href="plain.html">View plain text version</a>
77
+ <% end %>
78
+ </p>
79
+ <% end %>
80
+ </div>
81
+
82
+ <% if type == "plain" %>
83
+ <pre id="message_body"><%= CGI.escapeHTML(body) %></pre>
84
+ <% else %>
85
+ <%= body %>
86
+ <% end %>
@@ -0,0 +1,38 @@
1
+ module LetterOpener
2
+ class Message
3
+ attr_reader :mail
4
+
5
+ def initialize(location, mail, part = nil)
6
+ @location = location
7
+ @mail = mail
8
+ @part = part
9
+ end
10
+
11
+ def render
12
+ FileUtils.mkdir_p(@location)
13
+ File.open(filepath, 'w') do |f|
14
+ f.write ERB.new(template).result(binding)
15
+ end
16
+ end
17
+
18
+ def template
19
+ File.read(File.expand_path("../message.html.erb", __FILE__))
20
+ end
21
+
22
+ def filepath
23
+ File.join(@location, "#{type}.html")
24
+ end
25
+
26
+ def content_type
27
+ @part && @part.content_type || @mail.content_type
28
+ end
29
+
30
+ def body
31
+ (@part && @part.body || @mail.body).to_s
32
+ end
33
+
34
+ def type
35
+ content_type =~ /html/ ? "rich" : "plain"
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,7 @@
1
+ module LetterOpener
2
+ class Railtie < Rails::Railtie
3
+ initializer "letter_opener.add_delivery_method" do
4
+ ActionMailer::Base.add_delivery_method :letter_opener, LetterOpener::DeliveryMethod, :location => Rails.root.join("tmp", "letter_opener")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,49 @@
1
+ require "spec_helper"
2
+
3
+ describe LetterOpener::DeliveryMethod do
4
+ before(:each) do
5
+ Launchy.stub(:open)
6
+ location = File.expand_path('../../../tmp/letter_opener', __FILE__)
7
+ FileUtils.rm_rf(location)
8
+ Mail.defaults do
9
+ delivery_method LetterOpener::DeliveryMethod, :location => location
10
+ end
11
+ @location = location
12
+ end
13
+
14
+ it "saves text into html document" do
15
+ Launchy.should_receive(:open)
16
+ mail = Mail.deliver do
17
+ from 'foo@example.com'
18
+ to 'bar@example.com'
19
+ subject 'Hello'
20
+ body 'World!'
21
+ end
22
+ text = File.read(Dir["#{@location}/*/plain.html"].first)
23
+ text.should include("foo@example.com")
24
+ text.should include("bar@example.com")
25
+ text.should include("Hello")
26
+ text.should include("World!")
27
+ end
28
+
29
+ it "saves multipart email into html document" do
30
+ mail = Mail.deliver do
31
+ from 'foo@example.com'
32
+ to 'bar@example.com'
33
+ subject 'Many parts'
34
+ text_part do
35
+ body 'This is <plain> text'
36
+ end
37
+ html_part do
38
+ content_type 'text/html; charset=UTF-8'
39
+ body '<h1>This is HTML</h1>'
40
+ end
41
+ end
42
+ text = File.read(Dir["#{@location}/*/plain.html"].first)
43
+ text.should include("View HTML version")
44
+ text.should include("This is &lt;plain&gt; text")
45
+ html = File.read(Dir["#{@location}/*/rich.html"].first)
46
+ html.should include("View plain text version")
47
+ html.should include("<h1>This is HTML</h1>")
48
+ end
49
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ Bundler.require(:default)
4
+
5
+ require "mail"
6
+ require "launchy"
7
+
8
+ RSpec.configure do |config|
9
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: letter_opener
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Bates
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-07 00:00:00.000000000 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: launchy
17
+ requirement: &2161172040 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *2161172040
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &2161171520 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.6.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *2161171520
37
+ - !ruby/object:Gem::Dependency
38
+ name: mail
39
+ requirement: &2161171020 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 2.3.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *2161171020
48
+ description: When mail is sent from your application, Letter Opener will open a preview
49
+ in the browser instead of sending.
50
+ email: ryan@railscasts.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - lib/letter_opener/delivery_method.rb
56
+ - lib/letter_opener/message.html.erb
57
+ - lib/letter_opener/message.rb
58
+ - lib/letter_opener/railtie.rb
59
+ - lib/letter_opener.rb
60
+ - spec/letter_opener/delivery_method_spec.rb
61
+ - spec/spec_helper.rb
62
+ - Gemfile
63
+ - Rakefile
64
+ - README.rdoc
65
+ has_rdoc: true
66
+ homepage: http://github.com/ryanb/letter_opener
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: 1.3.4
84
+ requirements: []
85
+ rubyforge_project: letter_opener
86
+ rubygems_version: 1.6.2
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Preview mail in browser instead of sending.
90
+ test_files: []