1
私はメールを使って電子メールを解析しようとしています。 Maildirに保存されたメール。結果として私は間違ったデータを取得しています。maildirで保存されたメールを解析するには?
電子メールのヘッダーと一致するハッシュ要素は空であり、メールの本文はメールの生データとして解析されます。
それの結果:私はメール、Maildirのと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: <[email protected]om> 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]>>>}
コントローラー:
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>
メールがデータを受信したことがわかったデータはMaildirに保存する前に解析する必要があります。しかし、Maildirは保存時にデータ構造を破壊しています。
誰かがこの問題を解決できますか?
私は、受信メールを処理しようとしているときにmailman(https://github.com/titanous/mailman)がかなりいいことが分かりました。 –