Module: ImmosquareExtensions::MailHelper

Defined in:
lib/immosquare-extensions/helpers/mail_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
# File 'lib/immosquare-extensions/helpers/mail_helper.rb', line 10

def display_email_header_infos(email)
  ApplicationController.render(
    :layout => false,
    :locals => {
      :email   => email,
      :formats => get_email_formats(email).map do |format|
                    name = format.split("/").last
                    {:name => name, :url => url_for(:part => name)}
                  end,
      :locales => I18n.available_locales.map do |locale|
        {:name => locale, :url => url_for(:locale => locale)}
      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]) %>
              <%= 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]) %>
              <% 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



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/immosquare-extensions/helpers/mail_helper.rb', line 138

def display_email_preview(email)
  begin
    availabe_formats = get_email_formats(email)
    part_format      = params[:part].present? ? availabe_formats.find {|format| format.end_with?(params[:part]) } : availabe_formats.first
    part_format      = availabe_formats.first if part_format.nil?
    parts            = get_email_parts(email)
    parts.find {|part| part[:content_type] == part_format }[:body]
  rescue StandardError => e
    "Error displaying email: #{e.message}"
  end
end