Class: Mail::Header
- Inherits:
-
Object
- Object
- Mail::Header
- 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
- LIMITED_FIELDS =
%w[ orig-date from sender reply-to to cc bcc message-id in-reply-to references subject return-path content-type mime-version content-transfer-encoding content-description content-id content-disposition content-location]
Constants included from Patterns
Patterns::ATOM_UNSAFE, Patterns::CONTROL_CHAR, Patterns::CRLF, Patterns::FIELD_BODY, Patterns::FIELD_LINE, Patterns::FIELD_NAME, Patterns::FWS, Patterns::HEADER_LINE, Patterns::PHRASE_UNSAFE, Patterns::TEXT, Patterns::TOKEN_UNSAFE, Patterns::WSP
Instance Method Summary collapse
-
#[](name) ⇒ Object
3.6.
-
#[]=(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.
- #decoded ⇒ Object
- #encoded ⇒ Object
-
#fields ⇒ Object
Returns an array of all the fields in the header in order that they were read in.
-
#fields=(unfolded_fields) ⇒ Object
3.6.
-
#has_content_id? ⇒ Boolean
Returns true if the header has a Content-ID defined (empty or not).
-
#has_date? ⇒ Boolean
Returns true if the header has a Date defined (empty or not).
-
#has_message_id? ⇒ Boolean
Returns true if the header has a Message-ID defined (empty or not).
-
#has_mime_version? ⇒ Boolean
Returns true if the header has a message_id defined (empty or not).
-
#initialize(header_text = nil) ⇒ Header
constructor
Creates a new header object.
-
#raw_source ⇒ Object
The preserved raw source of the header as you passed it in, untouched for your Regexing glory.
- #to_s ⇒ Object
Methods included from Utilities
Methods included from Patterns
Constructor Details
#initialize(header_text = 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.
36 37 38 39 |
# File 'lib/mail/header.rb', line 36 def initialize(header_text = nil) self.raw_source = header_text.to_crlf split_header if header_text 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']
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/mail/header.rb', line 109 def [](name) name = dasherize(name) selected = select_field_for(name) case when selected.length > 1 selected.map { |f| f } when !selected.blank? selected.first else nil end 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
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/mail/header.rb', line 135 def []=(name, value) name = dasherize(name) selected = select_field_for(name) case # User wants to delete the field when !selected.blank? && value == nil fields.delete_if { |f| selected.include?(f) } # User wants to change the field when !selected.blank? && limited_field?(name) selected.first.update(name, value) # User wants to create the field else # Need to insert in correct order for trace fields self.fields << Field.new(name.to_s, value) end end |
#decoded ⇒ Object
173 174 175 |
# File 'lib/mail/header.rb', line 173 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 |
#encoded ⇒ Object
161 162 163 164 165 166 167 |
# File 'lib/mail/header.rb', line 161 def encoded buffer = '' fields.each do |field| buffer << field.encoded end buffer end |
#fields ⇒ Object
Returns an array of all the fields in the header in order that they were read in.
49 50 51 |
# File 'lib/mail/header.rb', line 49 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]']
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/mail/header.rb', line 72 def fields=(unfolded_fields) @fields = Mail::FieldList.new unfolded_fields.each do |field| field = Field.new(field) selected = select_field_for(field.name) if selected.any? && limited_field?(field.name) selected.first.update(field.name, field.value) else @fields << field end end end |
#has_content_id? ⇒ Boolean
Returns true if the header has a Content-ID defined (empty or not)
183 184 185 |
# File 'lib/mail/header.rb', line 183 def has_content_id? !fields.select { |f| f.responsible_for?('Content-ID') }.empty? end |
#has_date? ⇒ Boolean
Returns true if the header has a Date defined (empty or not)
188 189 190 |
# File 'lib/mail/header.rb', line 188 def has_date? !fields.select { |f| f.responsible_for?('Date') }.empty? end |
#has_message_id? ⇒ Boolean
Returns true if the header has a Message-ID defined (empty or not)
178 179 180 |
# File 'lib/mail/header.rb', line 178 def !fields.select { |f| f.responsible_for?('Message-ID') }.empty? end |
#has_mime_version? ⇒ Boolean
Returns true if the header has a message_id defined (empty or not)
193 194 195 |
# File 'lib/mail/header.rb', line 193 def has_mime_version? !fields.select { |f| f.responsible_for?('Mime-Version') }.empty? end |
#raw_source ⇒ Object
The preserved raw source of the header as you passed it in, untouched for your Regexing glory.
43 44 45 |
# File 'lib/mail/header.rb', line 43 def raw_source @raw_source end |
#to_s ⇒ Object
169 170 171 |
# File 'lib/mail/header.rb', line 169 def to_s encoded end |