如何解析用maildir保存的电子邮件?

发布于 2024-12-04 17:22:57 字数 6448 浏览 2 评论 0原文

我正在尝试使用 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 <[email protected]>) id 1R3EUO-00085Q-Po for [email protected]; Tue, 13 Sep 2011 00:56:28 +0300 Received: by gwb11 with SMTP id 11so4512971gwb.34 for <[email protected]>; 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 <[email protected]> To: [email protected] 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: [email protected] 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: <[email protected]> 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: <[email protected]> sad<br> --20cf30563d5b31c6f004acc5993d--, :body_1=>#<Mail::Message:43274736, Multipart: false, Headers: <Return-Path: <[email protected]>>>}

我正在使用 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在保存时会破坏数据结构。

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

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 <[email protected]>) id 1R3EUO-00085Q-Po for [email protected]; Tue, 13 Sep 2011 00:56:28 +0300 Received: by gwb11 with SMTP id 11so4512971gwb.34 for <[email protected]>; 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 <[email protected]> To: [email protected] 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: [email protected] 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: <[email protected]> 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: <[email protected]> sad<br> --20cf30563d5b31c6f004acc5993d--, :body_1=>#<Mail::Message:43274736, Multipart: false, Headers: <Return-Path: <[email protected]>>>}

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑色毁心梦 2024-12-11 17:22:57

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

我将操作分为两种方法: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 对象?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文