mikel / tmail

TMail is a Ruby Email Handler.
http://tmail.rubyforge.org/
Other
73 stars 36 forks source link

Deleting custom header deletes all custom headers #8

Open pepawel opened 13 years ago

pepawel commented 13 years ago

Hi,

In the following example notice how 'custom2' header is deleted while deleting 'custom1':

>> m=Mail.new :custom1 => 'test1', :custom2 => 'test2'
=> \#<Mail::Message:-621216768, Multipart: false, Headers: <custom1: test1>, <custom2: test2>>

>> m[:custom2]
=> #<Mail::Field:0xb5f1ecc0 @field=#<Mail::OptionalField:0xb5f1e504 @length=nil, @name="custom2", @tree=nil, @element=nil, @errors=[], @value="test2", @charset="UTF8">>

>> m[:custom1] = nil
=> nil

>> m[:custom2]
=> nil

The problem is caused by Mail::Field#<=> method. This method treats all custom fields as equal.

Quick monkey patch:

class Mail::Field
  def <=>( other )
    self_order = FIELD_ORDER.rindex(self.name.to_s.downcase) || 100
    other_order = FIELD_ORDER.rindex(other.name.to_s.downcase) || 100
    if self_order == 100 && self_order == other_order
      self.name.to_s.downcase <=> other.name.to_s.downcase
    else
      self_order <=> other_order
    end
  end
end