Skip to content

Deleting custom header deletes all custom headers #8

Open
pepawel opened this Issue Mar 4, 2011 · 0 comments

1 participant

@pepawel
pepawel commented Mar 4, 2011

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.