betamatt-inline_attachment 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +72 -0
  2. data/lib/inline_attachment.rb +86 -0
  3. metadata +64 -0
data/README ADDED
@@ -0,0 +1,72 @@
1
+ = Inline Attachment
2
+
3
+ This package adds full support for embedding inline images into your HTML emails
4
+ through ActionMailer.
5
+
6
+ == Installation
7
+
8
+ To perform a system wide installation:
9
+
10
+ <tt>gem source -a http://gems.github.com</tt>
11
+ <tt>gem install betamatt-inline_attachment</tt>
12
+
13
+ To use inline_attachment in your project, add the following line to your project's
14
+ config/environment.rb:
15
+
16
+ <tt>config.gem 'inline_attachment'</tt>
17
+
18
+
19
+ == Usage
20
+
21
+ I've rewritten most of Edmond's great work in this version. I now override
22
+ path_to_image instead of image_tag because a big reason for all the Rails2
23
+ breakages was because image_tag was basically reproduced in previous versions,
24
+ so broke when that method changed.
25
+
26
+ Now we override the very simple path_to_image, and most importantly we really
27
+ just add our own stuff for ActionMailer templates, and resort to the existing
28
+ code for everything else.
29
+
30
+ I've also integrated in with the new implicit multipart stuff. So now, there is
31
+ so little code required!
32
+
33
+
34
+ class Notifier < ActionMailer::Base
35
+ def signup
36
+ recipients %q{"Testing IA" <testing@handle.it>}
37
+ from %q{"Mr Tester" <tester@handle.it>}
38
+ subject "Here's a funky test"
39
+ end
40
+ end
41
+
42
+ # Oh yeah baby! Read it and weep! So how's this work? Well, you'll need
43
+ # your templates named properly - see the `Multipart email` section of the
44
+ # ActionMailer::Base docs.
45
+
46
+ # signup.text.plain.erb
47
+ Your username is: <%= @username %>
48
+
49
+ # signup.text.html.erb
50
+ <html><head><title>Signup Notification</title></head><body>
51
+ <%= image_tag "logo.png" %>
52
+ <p>Your username is: <%=h @username %>
53
+ </body></html>
54
+
55
+
56
+ That's it! InlineAttachment will look for
57
+ "#{RAILS_ROOT}/public/images/logo.png" and will do the right thing and embed it
58
+ inline into the HTML version of the email. ActionMailer will do the right thing
59
+ and offer the recipient both the text/plain and text/html parts as alternatives.
60
+
61
+
62
+ Note, that you should still be able to use this in the 0.3.0 way if you have
63
+ code that uses that. But there were a lot of alternatives, and the examples in
64
+ here didn't show a crucial step of shuffling the parts around to be sure that
65
+ the image parts came after the html.
66
+
67
+ You can also do the old "manual" method if you want.
68
+
69
+ == Contributors
70
+
71
+ Jason King (JasonKing)
72
+ Matt Griffin (betamatt)
@@ -0,0 +1,86 @@
1
+ TMail::HeaderField::FNAME_TO_CLASS.delete('content-id')
2
+ CID_NAME = 'Content-Id'
3
+
4
+ module ActionMailer
5
+ module PartContainer
6
+ # Add an inline attachment to a multipart message.
7
+ def inline_attachment(params, &block)
8
+ params = { :content_type => params } if String === params
9
+ params = { :disposition => "inline",
10
+ :transfer_encoding => "base64" }.merge(params)
11
+ params[:headers] ||= {}
12
+ params[:headers][CID_NAME] = params[:cid]
13
+ part(params, &block)
14
+ end
15
+ end
16
+
17
+ class Base
18
+ private
19
+
20
+ def create_mail_with_inline_attachment
21
+ # sniff out inline html parts with inline images and wrap them up in a
22
+ # multipart/related mime part
23
+ got_inlines = false
24
+ related_parts = []
25
+ for_deletion = []
26
+ @parts.each_with_index do |p,i|
27
+ logger.debug "Considering #{i}"
28
+ if p.content_disposition == 'inline' && p.content_type =~ %r{^image/}i && p.headers[CID_NAME]
29
+ logger.debug "Just adding [#{i}] to the list\n#{p.filename}"
30
+ for_deletion << i
31
+ related_parts << p
32
+ got_inlines = true
33
+ end
34
+
35
+ if got_inlines && p.content_type =~ %r{text/html}i
36
+ @parts[i] = ActionMailer::Part.new( :content_type => "multipart/related" )
37
+ @parts[i].parts << p
38
+ related_parts.each do |rp|
39
+ @parts[i].parts << rp
40
+ end
41
+ got_inlines = false
42
+ related_parts = []
43
+ end
44
+ end
45
+ for_deletion.sort{|a,b|b<=>a}.each {|i|@parts.delete_at(i)}
46
+ create_mail_without_inline_attachment
47
+ end
48
+ alias_method_chain :create_mail, :inline_attachment
49
+ end
50
+ end
51
+
52
+ module ActionView
53
+ module Helpers #:nodoc:
54
+ module AssetTagHelper
55
+ def path_to_image_with_inline_attachment(source)
56
+ @part_container ||= @controller
57
+ if @part_container.is_a?(ActionMailer::Base) or @part_container.is_a?(ActionMailer::Part)
58
+ if source =~ /file:\/\//
59
+ # Handle attached files from the local filesystem. Should sandbox this somehow.
60
+ file_path = source.sub(/file:\/\//, '')
61
+ else
62
+ # Public image files
63
+ file_path = "#{RAILS_ROOT}/public#{path_to_image_without_inline_attachment(source).split('?').first}"
64
+ end
65
+
66
+ if File.exists?(file_path)
67
+ basename = File.basename(file_path)
68
+ ext = basename.split('.').last
69
+ cid = Time.now.to_f.to_s + "#{basename}@inline_attachment"
70
+ file = File.open(file_path, 'rb')
71
+
72
+ @part_container.inline_attachment(:content_type => "image/#{ext}",
73
+ :body => file.read,
74
+ :filename => basename,
75
+ :cid => "<#{cid}>",
76
+ :disposition => "inline")
77
+
78
+ return "cid:#{cid}"
79
+ end
80
+ end
81
+ return path_to_image_without_inline_attachment(source)
82
+ end
83
+ alias_method_chain :path_to_image, :inline_attachment
84
+ end
85
+ end
86
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: betamatt-inline_attachment
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Griffin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-17 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mime-types
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "1.15"
24
+ version:
25
+ description:
26
+ email: matt@griffinonline.org
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - README
35
+ - lib/inline_attachment.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/betamatt/inline_attachment
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --inline-source
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project: InlineAttachment
59
+ rubygems_version: 1.2.0
60
+ signing_key:
61
+ specification_version: 2
62
+ summary: Makes image_tag in an ActionMailer template embed the images in the emails. Forked from the github project by JasonKing.'
63
+ test_files: []
64
+