Module: SanitizeEmail::MailHeaderTools

Defined in:
lib/sanitize_email/mail_header_tools.rb

Overview

Tools for modifying the header of an email

Class Method Summary collapse

Class Method Details

.add_original_addresses_as_headers(message) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sanitize_email/mail_header_tools.rb', line 37

def self.add_original_addresses_as_headers(message)
  # Add headers by string concat.
  # Setting hash values on message.headers does nothing, strangely.
  # See: http://goo.gl/v46GY
  {
    # can be an arrays, so casting it as arrays
    'X-Sanitize-Email-To' => Array(message.to).uniq,
    'X-Sanitize-Email-Cc' => Array(message.cc).uniq
    # Don't write out the BCC, as those addresses should not be visible
    #   in message headers for obvious reasons
  }.each do |header_key, header_value|
    # For each type of address line
    SanitizeEmail::MailHeaderTools.update_header(
      header_key,
      header_value,
      message
    )
  end
end

.custom_subject(message) ⇒ Object


24
25
26
# File 'lib/sanitize_email/mail_header_tools.rb', line 24

def self.custom_subject(message)
  prepend_subject_array(message).join(' ')
end

.prepend_custom_subject(message) ⇒ Object


57
58
59
60
61
# File 'lib/sanitize_email/mail_header_tools.rb', line 57

def self.prepend_custom_subject(message)
  message.subject = '' unless message.subject
  custom_subject = SanitizeEmail::MailHeaderTools.custom_subject(message)
  message.subject = custom_subject + message.subject
end

.prepend_email_to_subject(actual_addresses) ⇒ Object


32
33
34
35
# File 'lib/sanitize_email/mail_header_tools.rb', line 32

def self.prepend_email_to_subject(actual_addresses)
  "(#{Array(actual_addresses).uniq.join(',').gsub(/@/, ' at ').
    gsub(/[<>]/, '~')})"
end

.prepend_environment_to_subjectObject


28
29
30
# File 'lib/sanitize_email/mail_header_tools.rb', line 28

def self.prepend_environment_to_subject
  SanitizeEmail::Config.config[:environment]
end

.prepend_subject_array(message) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sanitize_email/mail_header_tools.rb', line 9

def self.prepend_subject_array(message)
  prepend = []
  if SanitizeEmail.use_actual_email_prepended_to_subject
    prepend << SanitizeEmail::MailHeaderTools.
               prepend_email_to_subject(Array(message.to))
  end
  if SanitizeEmail.use_actual_environment_prepended_to_subject
    prepend << SanitizeEmail::MailHeaderTools.
               prepend_environment_to_subject
  end
  # this will force later joins to add an extra space
  prepend << '' unless prepend.empty?
  prepend
end

.update_header(header_key, header_value, message) ⇒ Object

According to github.com/mikel/mail

this is the correct way to update headers.

65
66
67
68
69
70
71
72
73
# File 'lib/sanitize_email/mail_header_tools.rb', line 65

def self.update_header(header_key, header_value, message)
  return nil unless header_value
  # For each address, as header_value can be an array of addresses
  Array(header_value).each_with_index do |elem, index|
    num = index + 1
    new_header_key = num > 1 ? "#{header_key}-#{num}" : header_key
    message.header[new_header_key] = elem.to_s
  end
end