immosquare-extensions 0.1.18 → 0.1.20

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9a814af40a5f4308e8f2ece8708febeac2256e77b99974587c93383dc990783
4
- data.tar.gz: 98f958827d42134e7ce59fa687e41c9b9ce66f59fca17cde9b0983492ce39c69
3
+ metadata.gz: 839917dfe2c067bc70d57463de526a06881912487b894157636314507ddc6f80
4
+ data.tar.gz: 2976c5d03bdbf5459a8a31ff56958e59ebd309642fc37936c414e1b5cdedf854
5
5
  SHA512:
6
- metadata.gz: 0a060b80cb2d6eaea808d2eccaeab51ad03ed0bc4e6e28494e817dd5a7b6f46a5d04da6a9ca0960ad6c53ff62bf69c27ff773fab6b972b2f439f3847a32a936f
7
- data.tar.gz: 7f9e1551a265073653fe6514361b41140ce25f31d69256fdd25b1d2ea79038095811a5fd824c904cbbb7fd7af567d1e79ac413f1dfe37c9953f2139157783e7e
6
+ metadata.gz: 0ab25383ed25488126a7618dfa478e0dcb0892041558b186671a1c891b7738903070ccd2e434bd07fb32be38f2957eb2c0365017dcaf078cb78d1aa7c3d006ee
7
+ data.tar.gz: 98910fa8d9d62bf352af1add06273134f86ab59b21e352eac5d48e9ea9dca189052a0998aa442e11f6054b0b316ed1b3519da568d730a499f53a523817adafde
@@ -0,0 +1,207 @@
1
+ module ImmosquareExtensions
2
+ module MailHelper
3
+ ##============================================================##
4
+ ## Inspired by:
5
+ ## https://github.com/rails/rails/blob/main/railties/lib/rails/templates/rails/mailers/email.html.erb
6
+ ## -------------
7
+ ## Here concact with "#{}" is not working, so used
8
+ ## "data:application/octet-stream;charset=utf-8;base64,"+Base64.encode64(a.body.to_s)
9
+ ##============================================================##
10
+ def display_email_header_infos(email)
11
+ ApplicationController.render(
12
+ :layout => false,
13
+ :locals => {
14
+ :email => email,
15
+ :formats => get_email_formats(email).map do |format|
16
+ name = format.split("/").last
17
+ {:name => name, :url => url_for(:part => name)}
18
+ end,
19
+ :locales => I18n.available_locales.map do |locale|
20
+ {:name => locale, :url => url_for(:locale => locale)}
21
+ end
22
+ },
23
+ :inline => <<~HTML
24
+ <header>
25
+ <table>
26
+ <% if email.respond_to?(:smtp_envelope_from) && Array(email.from) != Array(email.smtp_envelope_from) %>
27
+ <tr>
28
+ <th>SMTP-From:</th>
29
+ <td><%= email.smtp_envelope_from %></td>
30
+ </tr>
31
+ <% end %>
32
+
33
+ <% if email.respond_to?(:smtp_envelope_to) && email.to != email.smtp_envelope_to %>
34
+ <tr>
35
+ <th>SMTP-To:</th>
36
+ <td><%= email.smtp_envelope_to %></td>
37
+ </tr>
38
+ <% end %>
39
+
40
+ <tr>
41
+ <th>From:</th>
42
+ <td><%= email.header["from"] %></td>
43
+ </tr>
44
+
45
+ <% if email.reply_to %>
46
+ <tr>
47
+ <th>Reply-To:</th>
48
+ <td><%= email.header["reply-to"] %></td>
49
+ </tr>
50
+ <% end %>
51
+
52
+ <tr>
53
+ <th>To:</th>
54
+ <td><%= email.header["to"] %></td>
55
+ </tr>
56
+
57
+ <% if email.cc %>
58
+ <tr>
59
+ <th>CC:</th>
60
+ <td><%= email.header["cc"] %></td>
61
+ </tr>
62
+ <% end %>
63
+
64
+ <% if email.bcc %>
65
+ <tr>
66
+ <th>BCC:</th>
67
+ <td><%= email.header["bcc"] %></td>
68
+ </tr>
69
+ <% end %>
70
+
71
+ <tr>
72
+ <th>Date:</th>
73
+ <td><%= Time.current.rfc2822 %></td>
74
+ </tr>
75
+
76
+ <tr>
77
+ <th>Subject:</th>
78
+ <td><%= email.subject %></td>
79
+ </tr>
80
+
81
+ <tr>
82
+ <th>Formats:</th>
83
+ <td>
84
+ <% formats.each do |format| %>
85
+ <%= link_to(format[:name], format[:url]) %>
86
+ <%= content_tag(:span, " | ") unless format == formats.last %>
87
+ <% end %>
88
+ </td>
89
+ </tr>
90
+
91
+ <% if locales.size > 1 %>
92
+ <tr>
93
+ <th>Locale:</th>
94
+ <td>
95
+ <% locales.each do |locale| %>
96
+ <%= link_to(locale[:name], locale[:url]) %>
97
+ <% end %>
98
+ </td>
99
+ </tr>
100
+ <% end %>
101
+
102
+ <% if !(email.attachments.nil? || email.attachments.empty?) %>
103
+ <tr>
104
+ <th>Attachments:</th>
105
+ <td>
106
+ <% email.attachments.each do |a| %>
107
+ <% filename = a.respond_to?(:original_filename) ? a.original_filename : a.filename %>
108
+ <%= link_to filename, "data:application/octet-stream;charset=utf-8;base64,"+Base64.encode64(a.body.to_s), download: filename %>
109
+ <% end %>
110
+ </td>
111
+ </tr>
112
+ <% end %>
113
+
114
+ <% if !(email.header_fields.nil? || email.header_fields.empty?) %>
115
+ <tr>
116
+ <th class="align-top">Headers:</th>
117
+ <td>
118
+ <details>
119
+ <summary>Show all headers</summary>
120
+ <table>
121
+ <% email.header_fields.each do |field| %>
122
+ <tr>
123
+ <th><%= field.name %>:</th>
124
+ <td><%= field.value %></td>
125
+ </tr>
126
+ <% end %>
127
+ </table>
128
+ </details>
129
+ </td>
130
+ </tr>
131
+ <% end %>
132
+ </table>
133
+ </header>
134
+ HTML
135
+ )
136
+ end
137
+
138
+ def display_email_preview(email)
139
+ begin
140
+ availabe_formats = get_email_formats(email)
141
+ part_format = params[:part].present? ? availabe_formats.find {|format| format.end_with?(params[:part]) } : availabe_formats.first
142
+ part_format = availabe_formats.first if part_format.nil?
143
+ parts = get_email_parts(email)
144
+ parts.find {|part| part[:content_type] == part_format }[:body]
145
+ rescue StandardError => e
146
+ "Error displaying email: #{e.message}"
147
+ end
148
+ end
149
+
150
+ private
151
+
152
+
153
+ ##============================================================##
154
+ ## Return email formats
155
+ ## ["text/plain", "text/html"]
156
+ ## ["text/html"]
157
+ ## ["text/plain"]
158
+ ##============================================================##
159
+ def get_email_formats(email)
160
+ content_types = email.parts.map(&:content_type)
161
+ content_types_allowed = ["text/html", "text/plain"]
162
+ if !email.multipart? || content_types.empty?
163
+ if email.header_fields.nil? || email.header_fields.empty?
164
+ ["text/plain"]
165
+ else
166
+ header = email.header_fields.find {|header| header.name == "Content-Type" }
167
+ [header.nil? || !content_types_allowed.include?(header.value) ? "text/plain" : header.value]
168
+ end
169
+ else
170
+ content_types.map do |content_type|
171
+ content_type = content_type.split(";").first
172
+ content_types_allowed.include?(content_type) ? content_type : nil
173
+ end
174
+ .compact.sort_by do |content_type|
175
+ content_types_allowed.index(content_type)
176
+ end
177
+ end
178
+ end
179
+
180
+ ##============================================================##
181
+ ## Return email parts with content type and body
182
+ ##============================================================##
183
+ def get_email_parts(email)
184
+ parts_type = email.parts.map(&:content_type)
185
+ if !email.multipart? || parts_type.empty?
186
+ if email.header_fields.nil? || email.header_fields.empty?
187
+ content_type = "text/plain"
188
+ else
189
+ header = email.header_fields.find {|header| header.name == "Content-Type" }
190
+ content_type = header.nil? ? "text/plain" : header.value
191
+ end
192
+ [{
193
+ :content_type => content_type,
194
+ :body => content_type == "text/plain" ? simple_format(email.body.decoded) : email.body.decoded
195
+ }]
196
+ else
197
+ email.parts.map do |part|
198
+ content_type = part.content_type.split(";").first
199
+ {
200
+ :content_type => content_type,
201
+ :body => content_type == "text/plain" ? simple_format(part.body.decoded) : part.body.decoded
202
+ }
203
+ end
204
+ end
205
+ end
206
+ end
207
+ end
@@ -0,0 +1,13 @@
1
+ require "rails"
2
+
3
+ module ImmosquareExtensions
4
+ class Railtie < Rails::Railtie
5
+
6
+ initializer "immosquare_extensions.action_view" do
7
+ ActiveSupport.on_load(:action_view) do
8
+ include ImmosquareExtensions::MailHelper
9
+ end
10
+ end
11
+
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module ImmosquareExtensions
2
- VERSION = "0.1.18".freeze
2
+ VERSION = "0.1.20".freeze
3
3
  end
@@ -3,6 +3,8 @@ require_relative "immosquare-extensions/array"
3
3
  require_relative "immosquare-extensions/file"
4
4
  require_relative "immosquare-extensions/hash"
5
5
  require_relative "immosquare-extensions/string"
6
+ require_relative "immosquare-extensions/helpers/mail_helper"
7
+ require_relative "immosquare-extensions/railtie" if defined?(Rails)
6
8
 
7
9
  module ImmosquareExtensions
8
10
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immosquare-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.18
4
+ version: 0.1.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - IMMO SQUARE
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-14 00:00:00.000000000 Z
11
+ date: 2024-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: unicode_utils
@@ -38,6 +38,8 @@ files:
38
38
  - lib/immosquare-extensions/array.rb
39
39
  - lib/immosquare-extensions/file.rb
40
40
  - lib/immosquare-extensions/hash.rb
41
+ - lib/immosquare-extensions/helpers/mail_helper.rb
42
+ - lib/immosquare-extensions/railtie.rb
41
43
  - lib/immosquare-extensions/shared_methods.rb
42
44
  - lib/immosquare-extensions/string.rb
43
45
  - lib/immosquare-extensions/version.rb
@@ -60,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
62
  - !ruby/object:Gem::Version
61
63
  version: '0'
62
64
  requirements: []
63
- rubygems_version: 3.4.13
65
+ rubygems_version: 3.5.9
64
66
  signing_key:
65
67
  specification_version: 4
66
68
  summary: Utility extensions for Ruby core classes