Skip to content

Commit

Permalink
parse email content
Browse files Browse the repository at this point in the history
  • Loading branch information
siuying committed Dec 3, 2011
1 parent 6259ac5 commit 5c849bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/models/reply_listener.rb
Expand Up @@ -5,7 +5,7 @@ def self.perform(reply_id, key, from, text, message_id)
previous_reply = Reply.find(reply_id)
recipient = User.find(from)

valid_reply_key = TopicMailer.reply_key(previous_reply.email_key, recipient.email)
valid_reply_key = TopicMailer.reply_key(previous_reply.email_key, recipient.email)
if valid_reply_key != key
raise "Invalid reply: #{reply_id}, key: #{key}, from: #{from}"
end
Expand Down
29 changes: 24 additions & 5 deletions script/mailman.rb
@@ -1,7 +1,5 @@
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'

require 'mailman'
require 'resque'

Expand All @@ -15,13 +13,34 @@
:ssl => config["ssl"]
}

def plaintext_body(message)
if message.multipart?
message.parts.each do |p|
if p.content_type =~ /text\/plain/
encoding = p.content_type.to_s.split("=").last.to_s
return safe_str_encoding(p.body,encoding)
end
end
raise "mail body multipart, but not text/plain part"
elsif message.content_type == 'text/plain'
return safe_str_encoding(message.body, message.encoding)
else
raise "mail body is not multipart nor text/plain"
end
end

def safe_str_encoding(html, coding)
doc = Nokogiri::HTML(html.to_s, nil, coding)
return doc.css("body").text
end

Mailman::Application.run do
to('notification+%reply_id%-%key%@ruby-hk.org') do |reply_id, key|
puts "user: #{params[:user]}"
puts "from: #{message.from.first}"
puts "reply_id: #{reply_id}"
puts "key: #{key}"
puts "message: #{message}"
puts "message_id: #{message.message_id}"

puts "body: #{plaintext_body(message)}"

end
end

0 comments on commit 5c849bc

Please sign in to comment.