class Mail::Header
Experimental RBS support (using type sampling data from the type_fusion
project).
# sig/mail/header.rbs class Mail::Header def []: (Symbol name) -> nil def fields: () -> untyped end
sections 3 and 4 of this standard.
2.2.3. All field bodies MUST conform to the syntax described in
used in header “folding” and “unfolding” as described in section
except for CR and LF. However, a field body may contain CRLF when
colon. A field body may be composed of any US-ASCII characters,
characters that have values between 33 and 126, inclusive), except
name MUST be composed of printable US-ASCII characters (i.e.,
(“:”), followed by a field body, and terminated by CRLF. A field
Header fields are lines composed of a field name, followed by a colon
2.2. Header Fields
===Per RFC2822
Provides access to a header object.
def self.maximum_amount
mail library.
Use this parameter to limit number of headers that will be parsed by
Large amount of headers in Email might create extra high CPU load
def self.maximum_amount @@maximum_amount end
def self.maximum_amount=(value)
def self.maximum_amount=(value) @@maximum_amount = value end
def [](name)
Experimental RBS support (using type sampling data from the type_fusion
project).
def []: (Symbol name) -> nil
This signature was generated using 1 sample from 1 application.
h['To'] #=> 'mikel@me.com'
h.fields = ['To: mikel@me.com', 'X-Mail-SPAM: 15', 'X-Mail-SPAM: 20']
h = Header.new
Example:
in the header ordered from top to bottom.
matching header, will return an array of values in order that they appear
of the value if there is only one header, or if there is more than one
As per RFC, many fields can appear more than once, we will return a string
appears in the Notes column.
in the minimum or maximum column indicates that a special restriction
limitations on the use of those fields. An asterisk next to a value
field may occur in a message header as well as any special
The following table indicates limits on the number of times each
3.6. Field definitions
def [](name) fields.get_field(Utilities.dasherize(name)) end
def []=(name, value)
h['X-Mail-SPAM'] = nil
h['X-Mail-SPAM'] # => ['15', '20', '10000']
h['X-Mail-SPAM'] = '10000'
h['To'] #=> 'bob@you.com'
h['To'] = 'bob@you.com'
h.fields = ['To: mikel@me.com', 'X-Mail-SPAM: 15', 'X-Mail-SPAM: 20']
h = Header.new
Example:
the FIRST field matched from the header if passed nil
Sets the FIRST matching field in the header to passed value, or deletes
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
def charset=(val)
def charset=(val) params = self[:content_type].parameters rescue nil if params if val params[:charset] = val else params.delete(:charset) end end @charset = val end
def decoded
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
def each(&block)
def each(&block) fields.each(&block) end
def encoded
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
def errors
def errors @fields.map(&:errors).flatten(1) end
def field_summary
def field_summary fields.summary end
def fields
Experimental RBS support (using type sampling data from the type_fusion
project).
def fields: () -> untyped
This signature was generated using 1 sample from 1 application.
Returns an array of all the fields in the header in order that they
def fields @fields ||= FieldList.new end
def fields=(unfolded_fields)
h = Header.new
Acceps an array of field string values, for example:
receives them in.
Populates the fields container with Field objects in the order it
information.
prepended to the message. See sections 3.6.6 and 3.6.7 for more
header fields MUST NOT be reordered, and SHOULD be kept in blocks
transformed. More importantly, the trace header fields and resent
fields SHOULD NOT be reordered when a message is transported or
the Internet. However, for the purposes of this standard, header
have been known to be reordered occasionally when transported over
be in a particular order. They may appear in any order, and they
It is important to note that the header fields are not guaranteed to
3.6. Field definitions
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
def has_content_id?
def has_content_id? fields.has_field? 'Content-ID' end
def has_date?
def has_date? fields.has_field? 'Date' end
def has_message_id?
def has_message_id? fields.has_field? 'Message-ID' end
def has_mime_version?
def has_mime_version? fields.has_field? 'Mime-Version' end
def initialize(header_text = nil, charset = nil)
these cases, please make a patch and send it in, or at the least, send
no automatic processing of that field will happen. If you find one of
field and leave it alone. This will mean that the data is preserved but
type), but it fails to parse it, it will simply make it an unstructured
If it finds a field that should be a structured field (such as content
it goes.
it and split it into the various fields, instantiating each field as
Accepts raw text or nothing. If given raw text will attempt to parse
Creates a new header object.
def initialize(header_text = nil, charset = nil) @charset = charset @raw_source = ::Mail::Utilities.to_crlf(header_text).lstrip split_header if header_text end
def initialize_copy(original)
def initialize_copy(original) super @fields = @fields.dup @fields.map!(&:dup) end
def split_header
Splits an unfolded and line break cleaned header into individual field
def split_header self.fields = @raw_source.split(Constants::HEADER_SPLIT) end
def to_s
def to_s encoded end