1

I'm trying to parse emails using Mail. Emails saved in Maildir. As result I'm getting wrong data.

Elements in hash that matches to email's headers are empty and body of the mail is parsed as raw data of mail.

It's results:

{:multipart=>false, 
:parts_length=>0, 
:from_addresses=>"", 
:sender=>nil, 
:to=>[], 
:cc=>[], 
:subject=>nil, 
:date=>"", 
:message_id=>nil, 
:body=>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 <s.bezugliy@gmail.com>) 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 <developer@nertis.com.ua>; 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 <s.bezugliy@gmail.com> To: developer@nertis.com.ua Message-ID: <CAC68ufhUQcF6PV94_ELiwQH1pGhE6w1-cwk6g+g0iXADQQ=Hfw@mail.gmail.com> 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: <4e735779cc836_13bc2cda944140b7@ADMIN.mail> 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: <4e735779d126f_13bc2cda94414164@ADMIN.mail> sad<br> --20cf30563d5b31c6f004acc5993d--, :body_1=>#<Mail::Message:43274736, Multipart: false, Headers: <Return-Path: <s.bezugliy@gmail.com>>>}

I'm using Mail, Maildir and Maildir-queue

Controller:

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 << new_message

    end
  end

  def read_mails_from_server(account_id)
    @account = Account.where(:id => account_id.to_s).last
    #TODO: check existence of directory and pass it if true
    @mail = Mail::POP3.new :address => @account.incoming_server.address.to_s,
                           :port => @account.incoming_server.port.to_i,
                           :user_name => @account.username.to_s,
                           :password => @account.password.to_s,
                           :authentication => @account.incoming_server.authentication.to_sym,
                           :enable_ssl => (@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

View:

<table class="standart" style="font-size: smaller;">
  <tr>
    <th>Multipart:</th>
    <th>Parts</th>
    <th>From</th>
    <th>From addresses</th>
    <th>Sender</th>
    <th>To</th>
    <th>CC</th>
    <th>Subject</th>
    <th>Date</th>
    <th>Message ID</th>
    <th>Body</th>
  </tr>
  <% @new_messages.each do |message| %>
      <tr>
        <td><%= message[:multipart] %></td>
        <td><%= message[:parts_length] %></td>
        <td><%= message[:from] %></td>
        <td><%= message[:from_addresses] %></td>
        <td><%= message[:sender] %></td>
        <td><%= message[:to].each{|address| address.to_s + ", "} %></td>
        <td><%= message[:cc] %></td>
        <td><%= message[:subject] %></td>
        <td><%= message[:date] %></td>
        <td><%= message[:message_id] %></td>
        <td><%= message[:body] %></td>
        <td><%= message[:body_1] %></td>
      </tr>
  <% end %>
</table>

I'm found, that Mail receiving data correctly, data parsed before saving to Maildir as must. But Maildir is damaging data structure when saving.

Can someone help me with this problem?

Sergey Bezugliy
  • 580
  • 7
  • 23

1 Answers1

0

I found solution of my problem.

I separated actions in two methods: index and read_mails_from_server, I defined serializer for Maildir only before saving mail, but before reading messages don't defined it.

This code works good:

 def index
    read_mails_from_server(params[:account_id])
    @new_messages=[]
    @queue.serializer = Maildir::Serializer::Mail.new
    @queue.list(:new).each do |message|
      new_message ={}
      mail = Mail.read_from_string(message.data)
      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.encoded
      new_message[:body_1] = message.data
      @new_messages << new_message

    end
  end

I can't understand, why I must define serializer in each method, where is used single @queue object?

Sergey Bezugliy
  • 580
  • 7
  • 23