email_pop_reader 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +45 -0
- data/lib/email_pop_reader.rb +87 -0
- data/spec/lib/email_pop_reader_spec.rb +53 -0
- data/spec/spec_helper.rb +11 -0
- metadata +66 -0
data/README
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
Author
|
2
|
+
====
|
3
|
+
Niranjan Sarade
|
4
|
+
|
5
|
+
About the utility - EmailPopReader
|
6
|
+
====
|
7
|
+
POP3 (Post Office Protocol) is the standard client/server protocol to receive emails.
|
8
|
+
In ruby, we have a libray called Net::POP3 which provides functionality for retrieving
|
9
|
+
email via POP3. In that, you basically start the pop session for accessing the emails
|
10
|
+
and close the pop session at the end.
|
11
|
+
|
12
|
+
TMail library has a complete way to handle and manipulate emails from within ruby code.
|
13
|
+
It allows you to treat an email totally as an object.
|
14
|
+
|
15
|
+
There can be a business requirement for scanning emails and storing those in some sort of data structure to process further.
|
16
|
+
This utility makes use of Net::POP3 and TMail libraries and provides with some handy methods such as
|
17
|
+
'retrieve emails' as array of hashes. The hash has email's from,to,cc,bcc,subject,body fields. Email body with attachment has not been considered
|
18
|
+
for simplicity.
|
19
|
+
|
20
|
+
It also provides 'delete_emails(unique_email_ids=[])' method which takes array of unique email ids (retrieved with pop email unique_id) as parameter
|
21
|
+
and deletes those.
|
22
|
+
|
23
|
+
This gem has a TMail gem dependency.
|
24
|
+
|
25
|
+
Example
|
26
|
+
====
|
27
|
+
|
28
|
+
You can set the following three valiables in either environment.rb file in RAILS application or as global constants.
|
29
|
+
POP_HOST
|
30
|
+
POP_USERNAME
|
31
|
+
POP_PASSWORD
|
32
|
+
|
33
|
+
pop_reader = EmailPopReader.new
|
34
|
+
emails = pop_reader.retrieve_emails
|
35
|
+
pop_reader.delete_emails <array of email unique ids to be deleted>
|
36
|
+
|
37
|
+
Install
|
38
|
+
====
|
39
|
+
gem install http://github.com/NiranjanSarade/email-pop-reader.git/
|
40
|
+
|
41
|
+
Uninstall
|
42
|
+
====
|
43
|
+
gem uninstall email_pop_reader
|
44
|
+
|
45
|
+
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'tmail'
|
2
|
+
require 'net/pop'
|
3
|
+
|
4
|
+
class EmailPopReader
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@pop = Net::POP.new(POP_HOST)
|
8
|
+
end
|
9
|
+
|
10
|
+
def retrieve_emails
|
11
|
+
ids, emails, email_list = Array.new, Array.new, Array.new
|
12
|
+
begin
|
13
|
+
@pop.start(POP_USERNAME, POP_PASSWORD) do |pop|
|
14
|
+
if pop.mails.empty?
|
15
|
+
puts "No new email is received "
|
16
|
+
else
|
17
|
+
pop.each_mail do |m|
|
18
|
+
begin
|
19
|
+
ids << m.unique_id
|
20
|
+
emails << TMail::Mail.parse(m.pop)
|
21
|
+
rescue Exception => e
|
22
|
+
log_exception e
|
23
|
+
end
|
24
|
+
end
|
25
|
+
puts "#{@pop.mails.size} mails read."
|
26
|
+
end
|
27
|
+
end
|
28
|
+
unless emails.empty?
|
29
|
+
email_list = store_emails emails, ids
|
30
|
+
end
|
31
|
+
rescue Exception => excep
|
32
|
+
puts "There may be some problem with pop3 connectivity."
|
33
|
+
log_exception excep
|
34
|
+
end
|
35
|
+
email_list
|
36
|
+
end
|
37
|
+
|
38
|
+
def delete_emails uid_array=[]
|
39
|
+
begin
|
40
|
+
@pop.start(POP_USERNAME, POP_PASSWORD) do |pop|
|
41
|
+
pop.mails.select { |m| need_pop?(uid_array, m.unique_id) }.each do |m|
|
42
|
+
puts "deleting mail with id #{m.unique_id}"
|
43
|
+
m.delete
|
44
|
+
end
|
45
|
+
end
|
46
|
+
rescue => excep
|
47
|
+
log_exception excep
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def store_emails mails, ids
|
54
|
+
count = 0
|
55
|
+
mails.collect { |email|
|
56
|
+
email_hash = Hash.new
|
57
|
+
begin
|
58
|
+
email_hash['body'] = email.body
|
59
|
+
email_hash['date_sent'] = email.date
|
60
|
+
email_hash['subject'] = email.subject
|
61
|
+
email_hash['from'] = email.from
|
62
|
+
email_hash['to'] = !email.to ? [] : email.to
|
63
|
+
email_hash['cc'] = !email.cc ? [] : email.cc
|
64
|
+
email_hash['bcc'] = !email.bcc ? [] : email.bcc
|
65
|
+
rescue Exception => e
|
66
|
+
puts "There is exception while parsing email fields."
|
67
|
+
log_exception e
|
68
|
+
end
|
69
|
+
email_hash['unique_id'] = ids[count]
|
70
|
+
count = count + 1
|
71
|
+
email_hash
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
def need_pop?(uid_array, id)
|
76
|
+
uid_array.include?(id)
|
77
|
+
end
|
78
|
+
|
79
|
+
def log_exception excep
|
80
|
+
puts excep.class.name
|
81
|
+
puts excep.message
|
82
|
+
puts excep.backtrace.join("\n") if excep.backtrace
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../lib/email_pop_reader")
|
3
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
4
|
+
|
5
|
+
describe EmailPopReader do
|
6
|
+
POP_HOST = "test"
|
7
|
+
POP_USERNAME = "test"
|
8
|
+
POP_PASSWORD = "test"
|
9
|
+
|
10
|
+
before(:each) do
|
11
|
+
Net::POP.expects(:new).with(POP_HOST).returns(@pop3 = mock("pop"))
|
12
|
+
@pop_reader = EmailPopReader.new
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should throw error and should not retrieve mail if failed to open TCP connection" do
|
16
|
+
@pop3.expects(:start).with(POP_USERNAME,POP_PASSWORD).raises("Authentication error")
|
17
|
+
@pop_reader.retrieve_emails.should == []
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should not throw error and should retrieve zero mails" do
|
21
|
+
@pop3.expects(:start).with(POP_USERNAME,POP_PASSWORD).yields(popmail = mock("popmail"))
|
22
|
+
popmail.expects(:mails).returns([])
|
23
|
+
@pop_reader.retrieve_emails.should == []
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should not throw error and should retrieve a mail" do
|
27
|
+
@pop3.expects(:start).with(POP_USERNAME,POP_PASSWORD).yields(popmail = mock("popmail"))
|
28
|
+
@pop3.expects(:mails).returns(["mail1"])
|
29
|
+
popmail.expects(:mails).returns(["mail1"])
|
30
|
+
popmail.expects(:each_mail).yields(mail = mock("mail"))
|
31
|
+
mail.expects(:unique_id).returns("12")
|
32
|
+
mail.expects(:pop).returns(true)
|
33
|
+
email = OpenStruct.new({'to' => 'address@domain.com', 'cc' => '', 'bcc' => '', 'from' => 'add@domain.com', 'date_sent' => Time.now, 'subject' => 'subject' })
|
34
|
+
TMail::Mail.expects(:parse).with(true).returns(email)
|
35
|
+
@pop_reader.retrieve_emails.size.should == 1
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should not throw error and should delete a mail" do
|
39
|
+
@pop3.expects(:start).with(POP_USERNAME,POP_PASSWORD).yields(popmail = mock("popmail"))
|
40
|
+
mail = mock("mail")
|
41
|
+
mail.expects(:unique_id).returns("12").times(2)
|
42
|
+
popmail.expects(:mails).returns([mail])
|
43
|
+
mail.expects(:delete).returns(true)
|
44
|
+
@pop_reader.delete_emails(["12"]).should be_nil
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should not delete a mail if it throws error" do
|
48
|
+
@pop3.expects(:start).with(POP_USERNAME,POP_PASSWORD).raises("Authentication error")
|
49
|
+
@pop_reader.delete_emails(["12"]).should be_nil
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: email_pop_reader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Niranjan Sarade
|
8
|
+
autorequire: email_pop_reader
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-19 00:00:00 +05:30
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: tmail
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.3.1
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: nirusuma@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- ./README
|
33
|
+
files:
|
34
|
+
- lib/email_pop_reader.rb
|
35
|
+
- ./README
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/NiranjanSarade/email-pop-reader
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.3.5
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: A utility in ruby to read the emails from pop3 as array of hashes and delete emails with specified email unique ids
|
64
|
+
test_files:
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
- spec/lib/email_pop_reader_spec.rb
|