Class: EmailMessage
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- EmailMessage
- Defined in:
- app/models/email_message.rb
Class Method Summary collapse
-
.create_from_pop3 ⇒ Object
Class Methods ========================================================.
Instance Method Summary collapse
-
#parse ⇒ Object
Instance Methods ========================================================.
Class Method Details
.create_from_pop3 ⇒ Object
Class Methods ========================================================
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/models/email_message.rb', line 9 def self.create_from_pop3 = EmailMessage.maximum(:number).to_i msg_count = 0 puts "Connecting to #{OPEN_PORCH_POP3['host']} ..." Net::POP.enable_ssl(OpenSSL::SSL::VERIFY_NONE) if OPEN_PORCH_POP3['enable_ssl'] Net::POP3.start(OPEN_PORCH_POP3['host'], OPEN_PORCH_POP3['port'], OPEN_PORCH_POP3['username'], OPEN_PORCH_POP3['password']) do |pop| puts "Found #{pop.n_mails} messages on the server" puts "Last saved message: #{last_message_number}" unless pop.mails.empty? pop.mails.each do |m| next if (m.number <= ) print "\tSaving message ##{m.number} (#{"%.1f" % (m.length/1024.to_f)} KB) ... " begin = EmailMessage.create( :number => m.number, :content => m.pop, :size => m.length, :parsed => false ) print 'ok' if .parse msg_count += 1 end rescue ActiveRecord::StatementInvalid => e puts puts "========> ERROR saving message ##{m.number}" puts e. end break if msg_count >= 10 end puts "\tCopied #{msg_count} new messages from the server." end end puts "Done!" rescue SocketError => e puts "ERROR: Could not connect to #{OPEN_PORCH_POP3['host']} - #{e.message}" end |
Instance Method Details
#parse ⇒ Object
Instance Methods ========================================================
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/models/email_message.rb', line 52 def parse return true if self.parsed? print "\tParsing message:" mail = Mail.new(self.content) # Parse the body of the message if mail.multipart? if mail.text_part body = mail.text_part.body else body = mail.parts[0].body end else body = mail.body end # Find the user user = User.where(:email => mail.from).first if user.blank? print "\tUser: Could not find user with email #{mail.from}" puts return false end print "\tUser: #{user.id}" # Find the area if mail.to.nil? print "\tArea: recipient is empty. Skipping." puts end slug = Mail::Address.new(mail.to.first).local area = Area.where(:slug => slug).first if area.blank? print "\tArea: Could not find area with slug #{slug}" puts return false end print "\tArea: #{area.name}" print "\tCreating post ... " area.posts.create( :user_id => user.id, :email_message_id => self.id, :title => mail.subject, :content => Iconv.conv("UTF-8//TRANSLIT//IGNORE", 'UTF-8', body.to_s + ' ')[0..-2] ) self.update_attribute(:parsed, true) puts 'ok' return true end |