InlineAttachment 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,39 @@
1
+ = Inline Attachment
2
+
3
+ This package adds full support for embedding inline images into your HTML emails
4
+ through ActionMailer.
5
+
6
+ It is created from a Rails patch #2179 (found at http://dev.rubyonrails.org/ticket/2179).
7
+
8
+ The goal of this gem is to remove the need for manual patching.
9
+
10
+ == Installation
11
+
12
+ To perform a system wide installation:
13
+
14
+ <tt>gem install inline_attachment</tt>
15
+
16
+ To use inline_attachment in your project, add the following line to your project's
17
+ config/environment.rb:
18
+
19
+ <tt>require 'inline_attachment'</tt>
20
+
21
+
22
+ == Usage
23
+
24
+ The code to add inline attachments into your HTML emails is pretty simple:
25
+
26
+ @cid = Time.now.to_f.to_s + "foobar.png@domain.com"
27
+
28
+ inline_attachment :content_type => "image/png",
29
+ :body => File.read("#{RAILS_ROOT}/public/images/foobar.png"),
30
+ :filename => "foobar.png",
31
+ :cid => "<#{@cid}>"
32
+
33
+ Note that the above code should go into your ActionMailer.
34
+
35
+ The instance variable @cid is the unique identifier your MIME email will be using to
36
+ locate the attached image. All you have to do is then pass it over to your email
37
+ template, and display it like so:
38
+
39
+ <img src="cid:<%= @cid %>" alt="Foo Bar" />
@@ -0,0 +1,68 @@
1
+ module ActionMailer
2
+
3
+ module PartContainer
4
+
5
+ # Add an inline attachment to a multipart message.
6
+ def inline_attachment(params, &block)
7
+ params = { :content_type => params } if String === params
8
+ params = { :disposition => "inline",
9
+ :transfer_encoding => "base64" }.merge(params)
10
+ params[:headers] ||= {}
11
+ params[:headers]['Content-ID'] = params[:cid]
12
+ part(params, &block)
13
+ end
14
+
15
+ end
16
+
17
+ class Part
18
+
19
+ def to_mail(defaults)
20
+ part = TMail::Mail.new
21
+
22
+ if @parts.empty?
23
+ part.content_transfer_encoding = transfer_encoding || "quoted-printable"
24
+ case (transfer_encoding || "").downcase
25
+ when "base64" then
26
+ part.body = TMail::Base64.folding_encode(body)
27
+ when "quoted-printable"
28
+ part.body = [Utils.normalize_new_lines(body)].pack("M*")
29
+ else
30
+ part.body = body
31
+ end
32
+
33
+ # Always set the content_type after setting the body and or parts
34
+
35
+ # CHANGE: treat attachments and inline files the same
36
+ if content_disposition == "attachment" || ((content_disposition == "inline") && filename)
37
+ part.set_content_type(content_type || defaults.content_type, nil,
38
+ squish("charset" => nil, "name" => filename))
39
+ else
40
+ part.set_content_type(content_type || defaults.content_type, nil,
41
+ "charset" => (charset || defaults.charset))
42
+ end
43
+
44
+ part.set_content_disposition(content_disposition, squish("filename" => filename)) unless content_disposition.blank?
45
+ headers.each {|k,v| part[k] = v }
46
+ # END CHANGE
47
+
48
+ else
49
+ if String === body
50
+ part = TMail::Mail.new
51
+ part.body = body
52
+ part.set_content_type content_type, nil, { "charset" => charset }
53
+ part.set_content_disposition "inline"
54
+ m.parts << part
55
+ end
56
+
57
+ @parts.each do |p|
58
+ prt = (TMail::Mail === p ? p : p.to_mail(defaults))
59
+ part.parts << prt
60
+ end
61
+
62
+ part.set_content_type(content_type, nil, { "charset" => charset }) if content_type =~ /multipart/
63
+ end
64
+
65
+ part
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,40 @@
1
+ module TMail
2
+
3
+ class HeaderField # redefine
4
+
5
+ FNAME_TO_CLASS = {
6
+ 'date' => DateTimeHeader,
7
+ 'resent-date' => DateTimeHeader,
8
+ 'to' => AddressHeader,
9
+ 'cc' => AddressHeader,
10
+ 'bcc' => AddressHeader,
11
+ 'from' => AddressHeader,
12
+ 'reply-to' => AddressHeader,
13
+ 'resent-to' => AddressHeader,
14
+ 'resent-cc' => AddressHeader,
15
+ 'resent-bcc' => AddressHeader,
16
+ 'resent-from' => AddressHeader,
17
+ 'resent-reply-to' => AddressHeader,
18
+ 'sender' => SingleAddressHeader,
19
+ 'resent-sender' => SingleAddressHeader,
20
+ 'return-path' => ReturnPathHeader,
21
+ 'message-id' => MessageIdHeader,
22
+ 'resent-message-id' => MessageIdHeader,
23
+ 'in-reply-to' => ReferencesHeader,
24
+ 'received' => ReceivedHeader,
25
+ 'references' => ReferencesHeader,
26
+ 'keywords' => KeywordsHeader,
27
+ 'encrypted' => EncryptedHeader,
28
+ 'mime-version' => MimeVersionHeader,
29
+ 'content-type' => ContentTypeHeader,
30
+ 'content-transfer-encoding' => ContentTransferEncodingHeader,
31
+ 'content-disposition' => ContentDispositionHeader,
32
+ # 'content-id' => MessageIdHeader,
33
+ 'subject' => UnstructuredHeader,
34
+ 'comments' => UnstructuredHeader,
35
+ 'content-description' => UnstructuredHeader
36
+ }
37
+
38
+ end
39
+
40
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: InlineAttachment
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2007-06-01 00:00:00 +10:00
8
+ summary: "A package created from a Rails patch (ticket #2179) that adds embedded images into ActionMailer."
9
+ require_paths:
10
+ - lib
11
+ email: edmond.leung@handle.it
12
+ homepage:
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: inline_attachment
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Edmond Leung
31
+ files:
32
+ - lib/inline_attachment.rb
33
+ - lib/tmail_content_id.rb
34
+ - README
35
+ test_files: []
36
+
37
+ rdoc_options: []
38
+
39
+ extra_rdoc_files:
40
+ - README
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ requirements: []
46
+
47
+ dependencies: []
48
+