Module: ImmosquareExtensions::PreviewMailerHelper

Defined in:
lib/immosquare-extensions/helpers/preview_mailer_helper.rb

Instance Method Summary collapse

Instance Method Details

#display_email_header_infos(email) ⇒ Object

##

Inspired by: github.com/rails/rails/blob/main/railties/lib/rails/templates/rails/mailers/email.html.erb


Here concact with “#{}” is not working, so used “data:application/octet-stream;charset=utf-8;base64,”+Base64.encode64(a.body.to_s)

##


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/immosquare-extensions/helpers/preview_mailer_helper.rb', line 10

def display_email_header_infos(email)
  ApplicationController.render(
    :layout => false,
    :locals => {
      :email   => email,
      :formats => get_email_formats(email),
      :locales => I18n.available_locales.map do |locale|
        {
          :name      => locale,
          :url       => url_for(:mailer_locale => locale, :part => params[:part].presence),
          :css_class => (params[:mailer_locale].present? && params[:mailer_locale] == locale.to_s) || (params[:mailer_locale].blank? && I18n.locale == locale) ? "" : "text-body"
        }
      end
    },
    :inline => <<~HTML
      <header>
        <table>
          <% if email.respond_to?(:smtp_envelope_from) && Array(email.from) != Array(email.smtp_envelope_from) %>
            <tr>
              <th>SMTP-From:</th>
              <td><%= email.smtp_envelope_from %></td>
            </tr>
          <% end %>

          <% if email.respond_to?(:smtp_envelope_to) && email.to != email.smtp_envelope_to %>
            <tr>
              <th>SMTP-To:</th>
              <td><%= email.smtp_envelope_to %></td>
            </tr>
          <% end %>

          <tr>
            <th>From:</th>
            <td><%= email.header["from"] %></td>
          </tr>

          <% if email.reply_to %>
            <tr>
              <th>Reply-To:</th>
              <td><%= email.header["reply-to"] %></td>
            </tr>
          <% end %>

          <tr>
            <th>To:</th>
            <td><%= email.header["to"] %></td>
          </tr>

          <% if email.cc %>
            <tr>
              <th>CC:</th>
              <td><%= email.header["cc"] %></td>
            </tr>
          <% end %>

          <% if email.bcc %>
            <tr>
              <th>BCC:</th>
              <td><%= email.header["bcc"] %></td>
            </tr>
          <% end %>

          <tr>
            <th>Date:</th>
            <td><%= Time.current.rfc2822 %></td>
          </tr>

          <tr>
            <th>Subject:</th>
            <td><%= email.subject %></td>
          </tr>

          <tr>
            <th>Formats:</th>
            <td>
            <% formats.each do |format| %>
              <%= link_to(format[:name], format[:url], :class => "text-decoration-none hover-primary" + " " + format[:css_class]) %>
              <%= content_tag(:span, " | ") unless format == formats.last %>
            <% end %>
            </td>
          </tr>

          <% if locales.size > 1 %>
          <tr>
            <th>Locale:</th>
            <td>
              <% locales.each do |locale| %>
                <%= link_to(locale[:name], locale[:url], :class => "text-decoration-none hover-primary" + " " + locale[:css_class]) %>
                <%= content_tag(:span, " | ") unless locale == locales.last %>
              <% end %>
            </td>
          </tr>
          <% end %>

          <% if !(email.attachments.nil? || email.attachments.empty?) %>
            <tr>
              <th>Attachments:</th>
              <td>
              <% email.attachments.each do |a| %>
                <% filename = a.respond_to?(:original_filename) ? a.original_filename : a.filename %>
                <%= link_to filename, "data:application/octet-stream;charset=utf-8;base64,"+Base64.encode64(a.body.to_s), download: filename %>
              <% end %>
              </td>
            </tr>
          <% end %>

          <% if !(email.header_fields.nil? || email.header_fields.empty?) %>
            <tr>
              <th class="align-top">Headers:</th>
              <td>
                <details>
                  <summary>Show all headers</summary>
                  <table>
                    <% email.header_fields.each do |field| %>
                      <tr>
                        <th><%= field.name %>:</th>
                        <td><%= field.value %></td>
                      </tr>
                    <% end %>
                  </table>
                </details>
              </td>
            </tr>
          <% end %>
        </table>
      </header>
    HTML
  )
end

#display_email_preview(email) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/immosquare-extensions/helpers/preview_mailer_helper.rb', line 140

def display_email_preview(email)
  begin
    formats     = get_email_formats(email)
    part_format = formats.find {|f| f[:selected] }
    part_type   = Mime::Type.lookup(part_format[:content_type])

    raise("No email part found for content type: #{part_format[:content_type]}") if !(part = find_part(email, part_type))

    body = part.respond_to?(:decoded) ? part.decoded : part
    part_format[:name] == "html" ? body.html_safe : simple_format(body)
  rescue StandardError => e
    "Error displaying email: #{e.message}"
  end
end