Class: Mail::Header

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mail/header.rb

Overview

Provides access to a header object.

Per RFC2822

2.2. Header Fields

 Header fields are lines composed of a field name, followed by a colon
 (":"), followed by a field body, and terminated by CRLF.  A field
 name MUST be composed of printable US-ASCII characters (i.e.,
 characters that have values between 33 and 126, inclusive), except
 colon.  A field body may be composed of any US-ASCII characters,
 except for CR and LF.  However, a field body may contain CRLF when
 used in header "folding" and  "unfolding" as described in section
 2.2.3.  All field bodies MUST conform to the syntax described in
 sections 3 and 4 of this standard.

Constant Summary collapse

@@maximum_amount =
1000

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header_text = nil, charset = nil) ⇒ Header

Creates a new header object.

Accepts raw text or nothing. If given raw text will attempt to parse it and split it into the various fields, instantiating each field as it goes.

If it finds a field that should be a structured field (such as content type), but it fails to parse it, it will simply make it an unstructured field and leave it alone. This will mean that the data is preserved but no automatic processing of that field will happen. If you find one of these cases, please make a patch and send it in, or at the least, send me the example so we can fix it.



53
54
55
56
57
# File 'lib/mail/header.rb', line 53

def initialize(header_text = nil, charset = nil)
  @charset = charset
  @raw_source = ::Mail::Utilities.to_crlf(header_text).lstrip
  split_header if header_text
end

Instance Attribute Details

#charsetObject

Returns the value of attribute charset.



39
40
41
# File 'lib/mail/header.rb', line 39

def charset
  @charset
end

#raw_sourceObject (readonly)

Returns the value of attribute raw_source.



39
40
41
# File 'lib/mail/header.rb', line 39

def raw_source
  @raw_source
end

Class Method Details

.maximum_amountObject

Large amount of headers in Email might create extra high CPU load Use this parameter to limit number of headers that will be parsed by mail library. Default: 1000



31
32
33
# File 'lib/mail/header.rb', line 31

def self.maximum_amount
  @@maximum_amount
end

.maximum_amount=(value) ⇒ Object



35
36
37
# File 'lib/mail/header.rb', line 35

def self.maximum_amount=(value)
  @@maximum_amount = value
end

Instance Method Details

#[](name) ⇒ Object

3.6. Field definitions

The following table indicates limits on the number of times each
field may occur in a message header as well as any special
limitations on the use of those fields.  An asterisk next to a value
in the minimum or maximum column indicates that a special restriction
appears in the Notes column.

<snip table from 3.6>

As per RFC, many fields can appear more than once, we will return a string of the value if there is only one header, or if there is more than one matching header, will return an array of values in order that they appear in the header ordered from top to bottom.

Example:

h = Header.new
h.fields = ['To: [email protected]', 'X-Mail-SPAM: 15', 'X-Mail-SPAM: 20']
h['To']          #=> '[email protected]'
h['X-Mail-SPAM'] #=> ['15', '20']


130
131
132
# File 'lib/mail/header.rb', line 130

def [](name)
  fields.get_field(Utilities.dasherize(name))
end

#[]=(name, value) ⇒ Object

Sets the FIRST matching field in the header to passed value, or deletes the FIRST field matched from the header if passed nil

Example:

h = Header.new
h.fields = ['To: [email protected]', 'X-Mail-SPAM: 15', 'X-Mail-SPAM: 20']
h['To'] = '[email protected]'
h['To']    #=> '[email protected]'
h['X-Mail-SPAM'] = '10000'
h['X-Mail-SPAM'] # => ['15', '20', '10000']
h['X-Mail-SPAM'] = nil
h['X-Mail-SPAM'] # => nil


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/mail/header.rb', line 147

def []=(name, value)
  name = name.to_s
  if name.include?(Constants::COLON)
    raise ArgumentError, "Header names may not contain a colon: #{name.inspect}"
  end

  name = Utilities.dasherize(name)

  # Assign nil to delete the field
  if value.nil?
    fields.delete_field name
  else
    fields.add_field Field.new(name.to_s, value, charset)

    # Update charset if specified in Content-Type
    if name == 'content-type'
      params = self[:content_type].parameters rescue nil
      @charset = params[:charset] if params && params[:charset]
    end
  end
end

#decodedObject

Raises:

  • (NoMethodError)


195
196
197
# File 'lib/mail/header.rb', line 195

def decoded
  raise NoMethodError, 'Can not decode an entire header as there could be character set conflicts. Try calling #decoded on the various fields.'
end

#encodedObject



182
183
184
185
186
187
188
189
# File 'lib/mail/header.rb', line 182

def encoded
  buffer = String.new
  buffer.force_encoding('us-ascii') if buffer.respond_to?(:force_encoding)
  fields.each do |field|
    buffer << field.encoded
  end
  buffer
end

#errorsObject



105
106
107
# File 'lib/mail/header.rb', line 105

def errors
  @fields.map(&:errors).flatten(1)
end

#field_summaryObject



199
200
201
# File 'lib/mail/header.rb', line 199

def field_summary
  fields.summary
end

#fieldsObject

Returns an array of all the fields in the header in order that they were read in.



67
68
69
# File 'lib/mail/header.rb', line 67

def fields
  @fields ||= FieldList.new
end

#fields=(unfolded_fields) ⇒ Object

3.6. Field definitions

It is important to note that the header fields are not guaranteed to
be in a particular order.  They may appear in any order, and they
have been known to be reordered occasionally when transported over
the Internet.  However, for the purposes of this standard, header
fields SHOULD NOT be reordered when a message is transported or
transformed.  More importantly, the trace header fields and resent
header fields MUST NOT be reordered, and SHOULD be kept in blocks
prepended to the message.  See sections 3.6.6 and 3.6.7 for more
information.

Populates the fields container with Field objects in the order it receives them in.

Acceps an array of field string values, for example:

h = Header.new
h.fields = ['From: [email protected]', 'To: [email protected]']


90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mail/header.rb', line 90

def fields=(unfolded_fields)
  @fields = Mail::FieldList.new

  if unfolded_fields.size > self.class.maximum_amount
    Kernel.warn "WARNING: More than #{self.class.maximum_amount} header fields; only using the first #{self.class.maximum_amount} and ignoring the rest"
    unfolded_fields = unfolded_fields.slice(0...self.class.maximum_amount)
  end

  unfolded_fields.each do |field|
    if field = Field.parse(field, charset)
      @fields.add_field field
    end
  end
end

#has_content_id?Boolean

Returns true if the header has a Content-ID defined (empty or not)

Returns:

  • (Boolean)


209
210
211
# File 'lib/mail/header.rb', line 209

def has_content_id?
  fields.has_field? 'Content-ID'
end

#has_date?Boolean

Returns true if the header has a Date defined (empty or not)

Returns:

  • (Boolean)


214
215
216
# File 'lib/mail/header.rb', line 214

def has_date?
  fields.has_field? 'Date'
end

#has_message_id?Boolean

Returns true if the header has a Message-ID defined (empty or not)

Returns:

  • (Boolean)


204
205
206
# File 'lib/mail/header.rb', line 204

def has_message_id?
  fields.has_field? 'Message-ID'
end

#has_mime_version?Boolean

Returns true if the header has a MIME version defined (empty or not)

Returns:

  • (Boolean)


219
220
221
# File 'lib/mail/header.rb', line 219

def has_mime_version?
  fields.has_field? 'MIME-Version'
end

#initialize_copy(original) ⇒ Object



59
60
61
62
63
# File 'lib/mail/header.rb', line 59

def initialize_copy(original)
  super
  @fields = @fields.dup
  @fields.map!(&:dup)
end

#to_sObject



191
192
193
# File 'lib/mail/header.rb', line 191

def to_s
  encoded
end