mailbuilder 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'mail', "~> 2.4.0"
4
+ gem "actionpack", ">= 3.0.0"
5
+ gem 'nokogiri', "~> 1.5.0"
6
+
7
+ # Add dependencies to develop your gem here.
8
+ # Include everything needed to run rake, tests, features, etc.
9
+ group :development do
10
+ gem "shoulda"
11
+ gem "bundler"
12
+ gem "jeweler", "~> 1.6.4"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,67 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ actionpack (3.2.2)
5
+ activemodel (= 3.2.2)
6
+ activesupport (= 3.2.2)
7
+ builder (~> 3.0.0)
8
+ erubis (~> 2.7.0)
9
+ journey (~> 1.0.1)
10
+ rack (~> 1.4.0)
11
+ rack-cache (~> 1.1)
12
+ rack-test (~> 0.6.1)
13
+ sprockets (~> 2.1.2)
14
+ activemodel (3.2.2)
15
+ activesupport (= 3.2.2)
16
+ builder (~> 3.0.0)
17
+ activesupport (3.2.2)
18
+ i18n (~> 0.6)
19
+ multi_json (~> 1.0)
20
+ builder (3.0.0)
21
+ erubis (2.7.0)
22
+ git (1.2.5)
23
+ hike (1.2.1)
24
+ i18n (0.6.0)
25
+ jeweler (1.6.4)
26
+ bundler (~> 1.0)
27
+ git (>= 1.2.5)
28
+ rake
29
+ journey (1.0.3)
30
+ mail (2.4.1)
31
+ i18n (>= 0.4.0)
32
+ mime-types (~> 1.16)
33
+ treetop (~> 1.4.8)
34
+ mime-types (1.17.2)
35
+ multi_json (1.1.0)
36
+ nokogiri (1.5.0)
37
+ polyglot (0.3.3)
38
+ rack (1.4.1)
39
+ rack-cache (1.2)
40
+ rack (>= 0.4)
41
+ rack-test (0.6.1)
42
+ rack (>= 1.0)
43
+ rake (0.9.2.2)
44
+ shoulda (3.0.1)
45
+ shoulda-context (~> 1.0.0)
46
+ shoulda-matchers (~> 1.0.0)
47
+ shoulda-context (1.0.0)
48
+ shoulda-matchers (1.0.0)
49
+ sprockets (2.1.2)
50
+ hike (~> 1.2)
51
+ rack (~> 1.0)
52
+ tilt (!= 1.3.0, ~> 1.1)
53
+ tilt (1.3.3)
54
+ treetop (1.4.10)
55
+ polyglot
56
+ polyglot (>= 0.3.1)
57
+
58
+ PLATFORMS
59
+ ruby
60
+
61
+ DEPENDENCIES
62
+ actionpack (>= 3.0.0)
63
+ bundler
64
+ jeweler (~> 1.6.4)
65
+ mail (~> 2.4.0)
66
+ nokogiri (~> 1.5.0)
67
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Darwin
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,21 @@
1
+ = mailbuilder
2
+
3
+ A simple ERB based email template thingy! The nice thing is that it inlines the images =).
4
+
5
+ Created a gem as it was getting boring copying this to new projects.
6
+
7
+ == Contributing to mailbuilder
8
+
9
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
10
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
11
+ * Fork the project
12
+ * Start a feature/bugfix branch
13
+ * Commit and push until you are happy with your contribution
14
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
15
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
16
+
17
+ == Copyright
18
+
19
+ Copyright (c) 2012 Darwin. See LICENSE.txt for
20
+ further details.
21
+
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "mailbuilder"
18
+ gem.homepage = "http://github.com/bjornblomqvist/mailbuilder"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A simple ERB template mail builder}
21
+ gem.description = %Q{A simple ERB template mail builder. It creates an email with inline images. Very nice =)}
22
+ gem.email = "darwin.git@marianna.se"
23
+ gem.authors = ["Darwin"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "mailbuilder #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ require "#{File.dirname(__FILE__)}/../lib/mail_builder.rb"
4
+
5
+ mail = MailBuilder.new("#{File.dirname(__FILE__)}/emails/test").build({:name => "Björn Blomqvist"})
6
+ mail.subject "Just another test"
7
+ mail.to "me@me.com"
8
+ mail.from "you@you.com"
9
+
10
+ File.open("/tmp/test.eml",'w') do |file|
11
+ file.write(mail.to_s)
12
+ end
13
+
14
+ puts "Wrote the email to: /tmp/test.eml"
Binary file
@@ -0,0 +1,16 @@
1
+ <body bgimage="darwin.png">
2
+ <h1>Just a simple test</h1>
3
+
4
+ <%= name %>
5
+
6
+ <img src="darwin.png" >
7
+
8
+ <p style="background: url(darwin.png)">
9
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
10
+ </p>
11
+
12
+ <p background="darwin.png">
13
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
14
+ </p>
15
+
16
+ </body>
@@ -0,0 +1,3 @@
1
+ Just a simple test
2
+
3
+ <%= name %>
@@ -0,0 +1,164 @@
1
+ # encoding: utf-8
2
+
3
+ require 'action_view'
4
+ require 'mail'
5
+ require 'nokogiri'
6
+
7
+ class MailBuilder
8
+
9
+ def initialize(identifier_or_path,with_images = true)
10
+ if identifier_or_path.is_a?(Symbol)
11
+ @path = "./app/emails/#{identifier_or_path.to_s}"
12
+ else
13
+ @path = identifier_or_path
14
+ end
15
+ @basename = File.basename(@path)
16
+ @with_images = with_images
17
+ end
18
+
19
+ def build(locals)
20
+ result = nil
21
+ builder = self
22
+ Dir.chdir @path do
23
+ result = Mail.new do
24
+
25
+ part :content_type => 'multipart/related' do |related|
26
+
27
+ related.part :content_type => 'multipart/alternative' do |alternative|
28
+ alternative.part :content_type => 'text/plain', :body => builder.read_file_or_template("#{@basename}.txt", locals)
29
+ alternative.part :content_type => 'text/html', :body => nil
30
+ end
31
+
32
+ builder.add_html_part(related,locals)
33
+
34
+ end
35
+ end
36
+
37
+ result
38
+ end
39
+ end
40
+
41
+ def add_inline_images doc,related
42
+
43
+ parts = Hash.new
44
+
45
+ # Add images for img elements
46
+ doc.css('img').each do |img|
47
+ unless parts[img['src']]
48
+ related.add_file img['src']
49
+ parts[img['src']] = related.parts[-1]
50
+ related.parts[-1].encoded
51
+ end
52
+ img['src'] = "cid:#{parts[img['src']].content_id[1...-1]}"
53
+ end
54
+
55
+ # Add images for bgimage attributes
56
+ doc.css('*[bgimage]').each do |element|
57
+ unless parts[element['bgimage']]
58
+ related.add_file element['bgimage']
59
+ parts[element['bgimage']] = related.parts[-1]
60
+ related.parts[-1].encoded
61
+ end
62
+ element['bgimage'] = "cid:#{parts[element['bgimage']].content_id[1...-1]}"
63
+ end
64
+
65
+ # Add images for background attribute
66
+ doc.css('*[background]').each do |element|
67
+ if(element['background'].match ".jpg|.png|.gif")
68
+ unless parts[element['background']]
69
+ related.add_file element['background']
70
+ parts[element['background']] = related.parts[-1]
71
+ related.parts[-1].encoded
72
+ end
73
+ element['background'] = "cid:#{parts[element['background']].content_id[1...-1]}"
74
+ end
75
+ end
76
+
77
+ # Add style attributes with urls to an image
78
+ doc.css('*[style]').each do |element|
79
+
80
+ if(element['style'].match(/url\(["']?(.*?\.(png|gif|jpg|jpeg))["']?\)/i))
81
+ file_name = element['style'].match(/url\(["']?(.*?\.(png|gif|jpg|jpeg))["']?\)/i)[1]
82
+ unless parts[file_name]
83
+ related.add_file file_name
84
+ parts[file_name] = related.parts[-1]
85
+ related.parts[-1].encoded
86
+ end
87
+
88
+ element['style'] = element['style'].gsub(file_name,"cid:#{parts[file_name].content_id[1...-1]}")
89
+ end
90
+ end
91
+ end
92
+
93
+ def add_html_part related,locals
94
+
95
+ doc = Nokogiri::HTML::fragment(read_file_or_template("#{@basename}.html", locals))
96
+
97
+ add_inline_images(doc,related) if @with_images
98
+
99
+ related.parts[0].parts[1].content_type ['text', 'html', { 'charset' => 'UTF-8' }]
100
+ related.parts[0].parts[1].body = doc.to_s
101
+
102
+ end
103
+
104
+ def get_demo_html
105
+ Dir.chdir @path do
106
+ return read_file_or_template("#{@basename}.html", eval(File.read("locals.rb") || "{}"))
107
+ end
108
+ end
109
+
110
+ def get_demo_text
111
+ Dir.chdir @path do
112
+ return read_file_or_template("#{@basename}.txt", eval(File.read("locals.rb") || "{}"))
113
+ end
114
+ end
115
+
116
+ def read_file_or_template(filename, locals = {})
117
+ if File.exists?(filename)
118
+ File.read(filename)
119
+ elsif File.exists?("#{filename}.erb")
120
+ template = ERB.new(File.read("#{filename}.erb"))
121
+ template.result(erb_binding(locals))
122
+ end
123
+ end
124
+
125
+ module SimpleRenderingHelper
126
+
127
+ include ActionView::Helpers::TextHelper
128
+ include ActionView::Helpers::TagHelper
129
+ include ActionView::Helpers::SanitizeHelper
130
+
131
+ def self.included(into)
132
+ into.class_eval do
133
+ extend ActionView::Helpers::SanitizeHelper::ClassMethods
134
+ end
135
+ end
136
+
137
+ end
138
+
139
+ class BindingHelper
140
+ include SimpleRenderingHelper
141
+ def binding_for
142
+ binding
143
+ end
144
+
145
+ def add_attribute key,value
146
+ self.class.__send__(:attr_accessor, "#{key}")
147
+ self.__send__("#{key}=", value)
148
+ end
149
+
150
+ def set_locals hash
151
+ hash.each do |key,value|
152
+ add_attribute(key,value)
153
+ end
154
+ end
155
+ end
156
+
157
+
158
+ def erb_binding(vars)
159
+ object = BindingHelper.new
160
+ object.set_locals(vars)
161
+ object.binding_for
162
+ end
163
+
164
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'mailbuilder'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestMailbuilder < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mailbuilder
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.2.0
6
+ platform: ruby
7
+ authors:
8
+ - Darwin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-03-05 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mail
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 2.4.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: actionpack
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: nokogiri
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.5.0
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: shoulda
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: bundler
61
+ requirement: &id005 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *id005
70
+ - !ruby/object:Gem::Dependency
71
+ name: jeweler
72
+ requirement: &id006 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.6.4
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *id006
81
+ description: A simple ERB template mail builder. It creates an email with inline images. Very nice =)
82
+ email: darwin.git@marianna.se
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.rdoc
90
+ files:
91
+ - .document
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE.txt
95
+ - README.rdoc
96
+ - Rakefile
97
+ - VERSION
98
+ - example/build_test_email.rb
99
+ - example/emails/test/darwin.png
100
+ - example/emails/test/test.html.erb
101
+ - example/emails/test/test.txt.erb
102
+ - lib/mail_builder.rb
103
+ - test/helper.rb
104
+ - test/test_mailbuilder.rb
105
+ homepage: http://github.com/bjornblomqvist/mailbuilder
106
+ licenses:
107
+ - MIT
108
+ post_install_message:
109
+ rdoc_options: []
110
+
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 586765740859545114
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: "0"
128
+ requirements: []
129
+
130
+ rubyforge_project:
131
+ rubygems_version: 1.8.15
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: A simple ERB template mail builder
135
+ test_files: []
136
+