mail-embed-html-images 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 18fbffc4fa14e22e22e252d8689acf9779315e6e
4
+ data.tar.gz: d4fe77c7beef921aefd86932e3592a37884fefa5
5
+ SHA512:
6
+ metadata.gz: 72351c3bd207b8817def046e09716bd69aecf80fdd32be7092ecda2ffc4150adb9b74c2ec1d3c776c5de63bdf6225dee3a15e5eabe5386250e29303d3d3e47be
7
+ data.tar.gz: 7f0fa9c630d742b6f00fa5be53a6da6c63a0b30a5d543e2730f13295dfd689fc0d0a5074da287d066cdb63daf86afc43ff7c9bcb1afa4da498a048b014302b75
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2015 by Imperial College Union
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ mail-embed-html-images
2
+ ======================
3
+
4
+ Extends [Mail](https://github.com/mikel/mail) to provide a mechanism for
5
+ converting external image sources to embedded attachments.
6
+
7
+ Usage
8
+ -----
9
+
10
+ ```ruby
11
+ require 'mail'
12
+ require 'mail/embed-html-images'
13
+
14
+ mail = Mail.new do
15
+ from 'sender@example.com'
16
+ to 'recipient@example.com''
17
+ subject 'This is a test email'
18
+ html_part File.read('body.html')
19
+ end
20
+
21
+ mail.embed_html_images
22
+ mail.deliver
23
+ ```
24
+
25
+ The `embed_html_images` method looks for image tags with http[s] sources,
26
+ downloads the image from the src attribute, adds it as an attachment, and
27
+ replaces the original src attribute with a cid reference to the attachment.
28
+
29
+ It keeps track of downloaded files so if you have two images with the same
30
+ source it will only download and attach tha image once and ref the same
31
+ attachment from both images.
32
+
33
+ If you change the html_part, then you will need to re-run
34
+ `embed_html_images`. This will download any new images found, and remove
35
+ any attached images which are no longer required.
36
+
37
+ That is all.
@@ -0,0 +1,58 @@
1
+ require 'mail'
2
+ require 'digest/sha1'
3
+ require 'net/http'
4
+
5
+ module Mail
6
+ module EmbedHTMLImages
7
+ MIME_TO_EXTENSION = {
8
+ 'image/png' => 'png',
9
+ 'image/jpeg' => 'jpg',
10
+ 'image/gif' => 'gif',
11
+ }
12
+
13
+ def embed_html_images
14
+ @_embedded_images ||= {}
15
+ current_images = {}
16
+
17
+ # Don't do anything if we don't have an html part
18
+ return unless html_part
19
+
20
+ # Find all img tags in the html part
21
+ new_html = html_part.body.to_s.gsub(/<img (.*?)src=((?<![\\])['"])(http(?:.(?!(?<![\\])\2))*.?)\2([^>]*)>/) do |match_string|
22
+ img_src_original = $3
23
+ img_src_hash = Digest::SHA1.hexdigest img_src_original
24
+
25
+ # Check to see whether or not we already have this image: if not, attach it
26
+ if !@_embedded_images[img_src_hash]
27
+ response = Net::HTTP.get_response(URI(img_src_original))
28
+ attachment_filename = img_src_hash + '.' + MIME_TO_EXTENSION[response["content-type"]]
29
+ add_file filename: attachment_filename, content: response.body
30
+ @_embedded_images[img_src_hash] = attachment_filename
31
+ end
32
+ current_images[img_src_hash] = @_embedded_images[img_src_hash]
33
+
34
+ attached_image = attachments[@_embedded_images[img_src_hash]]
35
+
36
+ # Create new src using content id of attachment
37
+ img_src_new = "cid:#{attached_image.cid}"
38
+
39
+ # Return substitute with new src
40
+ "<img #{$1}src=\"#{img_src_new}\"#{$4}>"
41
+ end
42
+
43
+ # Detach any images that are no longer needed
44
+ @_embedded_images.each do |hash, filename|
45
+ if !current_images[hash]
46
+ self.parts.delete_if { |p| p.filename == filename }
47
+ end
48
+ end
49
+ @_embedded_images = current_images
50
+
51
+ html_part.body = new_html
52
+ end
53
+ end
54
+
55
+ class Message
56
+ include EmbedHTMLImages
57
+ end
58
+ end
@@ -0,0 +1,5 @@
1
+ module Mail
2
+ module EmbedHTMLImages
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mail-embed-html-images
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Phil Stewart
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mail
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2.6'
27
+ description: Extends Mail to provide a mechanism for converting external image sources
28
+ to embedded attachments.
29
+ email:
30
+ - phil.stewart@lichp.co.uk
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - LICENSE
36
+ - README.md
37
+ - lib/mail/embed-html-images.rb
38
+ - lib/mail/embed-html-images/version.rb
39
+ homepage: https://github.com/lichp/mail-embed-html-images
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.4.5
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Extends Mail to provide a mechanism for converting external image sources
63
+ to embedded attachments.
64
+ test_files: []