inline_attachment 0.4.0

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.
Files changed (3) hide show
  1. data/README +68 -0
  2. data/lib/inline_attachment.rb +78 -0
  3. metadata +64 -0
data/README ADDED
@@ -0,0 +1,68 @@
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 JasonKing-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
+
@@ -0,0 +1,78 @@
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
+ alias_method :ia_original_create_mail, :create_mail
19
+ def create_mail
20
+ # sniff out inline html parts with inline images and wrap them up in a
21
+ # multipart/related mime part
22
+ got_inlines = false
23
+ related_parts = []
24
+ for_deletion = []
25
+ @parts.each_with_index do |p,i|
26
+ logger.debug "Considering #{i}"
27
+ if p.content_disposition == 'inline' && p.content_type =~ %r{^image/}i && p.headers[CID_NAME]
28
+ logger.debug "Just adding [#{i}] to the list\n#{p.filename}"
29
+ for_deletion << i
30
+ related_parts << p
31
+ got_inlines = true
32
+ end
33
+
34
+ if got_inlines && p.content_type =~ %r{text/html}i
35
+ @parts[i] = ActionMailer::Part.new( :content_type => "multipart/related" )
36
+ @parts[i].parts << p
37
+ related_parts.each do |rp|
38
+ @parts[i].parts << rp
39
+ end
40
+ got_inlines = false
41
+ related_parts = []
42
+ end
43
+ end
44
+ for_deletion.sort{|a,b|b<=>a}.each {|i|@parts.delete_at(i)}
45
+ ia_original_create_mail
46
+ end
47
+ end
48
+ end
49
+
50
+ module ActionView
51
+ module Helpers #:nodoc:
52
+ module AssetTagHelper
53
+ alias_method :ia_original_path_to_image, :path_to_image
54
+ def path_to_image(source)
55
+ @part_container ||= @controller
56
+ if @part_container.is_a?(ActionMailer::Base) or @part_container.is_a?(ActionMailer::Part)
57
+ file_path = "#{RAILS_ROOT}/public#{ia_original_path_to_image(source).split('?').first}"
58
+
59
+ if File.exists?(file_path)
60
+ basename = File.basename(file_path)
61
+ ext = basename.split('.').last
62
+ cid = Time.now.to_f.to_s + "#{basename}@inline_attachment"
63
+ file = File.open(file_path, 'rb')
64
+
65
+ @part_container.inline_attachment(:content_type => "image/#{ext}",
66
+ :body => file.read,
67
+ :filename => basename,
68
+ :cid => "<#{cid}>",
69
+ :disposition => "inline")
70
+
71
+ return "cid:#{cid}"
72
+ end
73
+ end
74
+ return ia_original_path_to_image(source)
75
+ end
76
+ end
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: inline_attachment
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason King
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-02 00:00:00 +11: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: jk@handle.it
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/JasonKing/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.3.1
60
+ signing_key:
61
+ specification_version: 2
62
+ summary: Makes image_tag in an ActionMailer template embed the images in the emails
63
+ test_files: []
64
+