1

我正在尝试使用 Mail 解析电子邮件。保存在 Maildir 中的电子邮件。结果我得到了错误的数据。

哈希中与电子邮件标题匹配的元素为空,邮件正文被解析为邮件的原始数据。

其结果:

{: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>>>}

我正在使用 Mail、Maildir 和 Maildir-queue

控制器:

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

看法:

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

我发现,Mail 正确接收数据,必须在保存到 Maildir 之前解析数据。但是Maildir在保存时会破坏数据结构。

有人可以帮我解决这个问题吗?

4

1 回答 1

0

我找到了我的问题的解决方案。

我在两种方法中分离了动作:index 和 read_mails_from_server,我只在保存邮件之前为 Maildir 定义了序列化程序,但在阅读消息之前没有定义它。

这段代码效果很好:

 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

我不明白,为什么我必须在每个方法中定义序列化程序,在哪里使用单个 @queue 对象?

于 2011-09-23T14:30:21.317 回答