title: How to parse emails saved with maildir?
body: <p>I'm trying to parse emails using Mail. Emails saved in Maildir. As result I'm getting wrong data.</p>
  
  <p>Elements in hash that matches to email's headers are empty and body of the mail is parsed as raw data of mail.</p>
  
  <p>It's results:</p>
  
  <pre><code>{:multipart=&gt;false, 
  :parts_length=&gt;0, 
  :from_addresses=&gt;"", 
  :sender=&gt;nil, 
  :to=&gt;[], 
  :cc=&gt;[], 
  :subject=&gt;nil, 
  :date=&gt;"", 
  :message_id=&gt;nil, 
  :body=&gt;Received: from mail-gw0-f47.google.com ([74.125.83.47]:43013) by ruby3.hostpro.com.ua with esmtps (TLSv1:RC4-MD5:128) (Exim 4.69) (envelope-from &lt;s.bezugliy@gmail.com&gt;) id 1R3EUO-00085Q-Po for developer@nertis.com.ua; Tue, 13 Sep 2011 00:56:28 +0300 Received: by gwb11 with SMTP id 11so4512971gwb.34 for &lt;developer@nertis.com.ua&gt;; Mon, 12 Sep 2011 14:56:25 -0700 Received: by 10.236.191.198 with SMTP id g46mr29023630yhn.124.1315864585553; Mon, 12 Sep 2011 14:56:25 -0700 Received: by 10.236.201.37 with HTTP; Mon, 12 Sep 2011 14:56:25 -0700 Date: Tue, 13 Sep 2011 00:56:25 +0300 From: Sergey Bezugliy &lt;s.bezugliy@gmail.com&gt; To: developer@nertis.com.ua Message-ID: &lt;CAC68ufhUQcF6PV94_ELiwQH1pGhE6w1-cwk6g+g0iXADQQ=Hfw@mail.gmail.com&gt; Subject: asd Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=20cf30563d5b31c6f004acc5993d; charset=UTF-8 Content-Transfer-Encoding: 7bit Envelope-to: developer@nertis.com.ua Delivery-date: Tue, 13 Sep 2011 00:56:28 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; bh=B269q1/RZz/2Q9f3tQxjWWePPCL/RFujCHI07a88SYY=; b=Bab/aF8n9BG/w0Qm0fk4d1sYvkTfHhy9rt0976+aUzI2RMYG8vKeW/KQvFsfQ8k1nl dkxfKgc+slIBm6SMp7i3VBW4nmAHNSRSjphOqgJMZnBO19Ku1igc2pJNqCSj1DKUVMvY zZhQdz+wy7tTfZifvn3fczYo+SmM8ZfrwKejk= --20cf30563d5b31c6f004acc5993d Date: Fri, 16 Sep 2011 17:04:41 +0300 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-ID: &lt;4e735779cc836_13bc2cda944140b7@ADMIN.mail&gt; sad --20cf30563d5b31c6f004acc5993d Date: Fri, 16 Sep 2011 17:04:41 +0300 Mime-Version: 1.0 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-ID: &lt;4e735779d126f_13bc2cda94414164@ADMIN.mail&gt; sad&lt;br&gt; --20cf30563d5b31c6f004acc5993d--, :body_1=&gt;#&lt;Mail::Message:43274736, Multipart: false, Headers: &lt;Return-Path: &lt;s.bezugliy@gmail.com&gt;&gt;&gt;}
  </code></pre>
  
  <p>I'm using Mail, Maildir and Maildir-queue</p>
  
  <p>Controller:</p>
  
  <pre><code>def index
      read_mails_from_server(params[:account_id])
      @new_messages=[]
      @queue.list(:new).each do |message|
        new_message ={}
        mail = Mail.new(message.data)
        new_message[:multipart] = mail.multipart?
        new_message[:parts_length] = mail.parts.length
        new_message[:from_addresses] = mail.from.to_s
        new_message[:sender] = mail.sender
        new_message[:to] = mail.to_addrs
        new_message[:cc] = mail.cc_addrs
        new_message[:subject] = mail.subject
        new_message[:date] = mail.date.to_s
        new_message[:message_id] = mail.message_id
        new_message[:body] = mail.body
        @new_messages &lt;&lt; new_message
  
      end
    end
  
    def read_mails_from_server(account_id)
      @account = Account.where(:id =&gt; account_id.to_s).last
      #TODO: check existence of directory and pass it if true
      @mail = Mail::POP3.new :address =&gt; @account.incoming_server.address.to_s,
                             :port =&gt; @account.incoming_server.port.to_i,
                             :user_name =&gt; @account.username.to_s,
                             :password =&gt; @account.password.to_s,
                             :authentication =&gt; @account.incoming_server.authentication.to_sym,
                             :enable_ssl =&gt; (@account.incoming_server.ssl_led.to_s == "1" ? true : false)
  
      @queue = Maildir::Queue.new("./mails/#{@account.username.to_s}/maildir")
  
      @mail.all.each { |mail| @queue.push(mail) }
    end
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;table class="standart" style="font-size: smaller;"&gt;
    &lt;tr&gt;
      &lt;th&gt;Multipart:&lt;/th&gt;
      &lt;th&gt;Parts&lt;/th&gt;
      &lt;th&gt;From&lt;/th&gt;
      &lt;th&gt;From addresses&lt;/th&gt;
      &lt;th&gt;Sender&lt;/th&gt;
      &lt;th&gt;To&lt;/th&gt;
      &lt;th&gt;CC&lt;/th&gt;
      &lt;th&gt;Subject&lt;/th&gt;
      &lt;th&gt;Date&lt;/th&gt;
      &lt;th&gt;Message ID&lt;/th&gt;
      &lt;th&gt;Body&lt;/th&gt;
    &lt;/tr&gt;
    &lt;% @new_messages.each do |message| %&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;%= message[:multipart] %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= message[:parts_length] %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= message[:from] %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= message[:from_addresses] %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= message[:sender] %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= message[:to].each{|address| address.to_s + ", "} %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= message[:cc] %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= message[:subject] %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= message[:date] %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= message[:message_id] %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= message[:body] %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= message[:body_1] %&gt;&lt;/td&gt;
        &lt;/tr&gt;
    &lt;% end %&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>I'm found, that Mail receiving data correctly, data parsed before saving to Maildir as must. But Maildir is damaging data structure when saving.</p>
  
  <p>Can someone help me with this problem?</p>