29 |
29 |
imap = Net::IMAP.new(host, port, ssl)
|
30 |
30 |
imap.login(imap_options[:username], imap_options[:password]) unless imap_options[:username].nil?
|
31 |
31 |
imap.select(folder)
|
|
32 |
|
|
33 |
# Get the unread messages
|
32 |
34 |
imap.search(['NOT', 'SEEN']).each do |message_id|
|
|
35 |
# Get the message body
|
33 |
36 |
msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
|
34 |
37 |
logger.debug "Receiving message #{message_id}" if logger && logger.debug?
|
|
38 |
|
|
39 |
# Redmine manages the message
|
35 |
40 |
if MailHandler.receive(msg, options)
|
36 |
|
logger.debug "Message #{message_id} successfully received" if logger && logger.debug?
|
|
41 |
# Ennder trace the message enveloppe (to, cc, etc.)
|
|
42 |
if logger && logger.debug?
|
|
43 |
_enveloppe = imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
|
|
44 |
logger.debug "Message #{message_id} successfully received, to.emails=[#{ trace_to(_enveloppe) }]"
|
|
45 |
fi
|
|
46 |
|
37 |
47 |
if imap_options[:move_on_success]
|
38 |
|
imap.copy(message_id, imap_options[:move_on_success])
|
|
48 |
# Ennder new trace
|
|
49 |
logger.debug "move to Archive" if logger && logger.debug?
|
|
50 |
# Ennder Defect #9030 manage exception if the Imap Archive folder does not exist
|
|
51 |
begin
|
|
52 |
imap.copy(message_id, imap_options[:move_on_success])
|
|
53 |
rescue
|
|
54 |
logger.error "Exception : #{$!} (#{imap_options[:move_on_success]}), the folder does NOT exist (at the same level as INBOX)" if logger
|
|
55 |
end
|
39 |
56 |
end
|
|
57 |
|
|
58 |
# Ennder new trace
|
|
59 |
logger.debug "store Seen, Deleted..." if logger && logger.debug?
|
40 |
60 |
imap.store(message_id, "+FLAGS", [:Seen, :Deleted])
|
41 |
61 |
else
|
42 |
|
logger.debug "Message #{message_id} can not be processed" if logger && logger.debug?
|
|
62 |
logger.error "Message #{message_id} can not be processed" if logger
|
|
63 |
|
|
64 |
# Ennder new trace
|
|
65 |
logger.debug "store Seen..." if logger && logger.debug?
|
43 |
66 |
imap.store(message_id, "+FLAGS", [:Seen])
|
|
67 |
|
44 |
68 |
if imap_options[:move_on_failure]
|
45 |
|
imap.copy(message_id, imap_options[:move_on_failure])
|
|
69 |
# Ennder new trace
|
|
70 |
logger.debug "copy..." if logger && logger.debug?
|
|
71 |
|
|
72 |
# Ennder Defect #9030 manage exception if the Imap Archive folder does not exist
|
|
73 |
begin
|
|
74 |
imap.copy(message_id, imap_options[:move_on_failure])
|
|
75 |
rescue
|
|
76 |
logger.error "Exception : #{$!} (#{imap_options[:move_on_failure]}), the folder does NOT exist (at the same level as INBOX)" if logger
|
|
77 |
end
|
|
78 |
|
|
79 |
# Ennder new trace
|
|
80 |
logger.debug "store Deleted..." if logger && logger.debug?
|
46 |
81 |
imap.store(message_id, "+FLAGS", [:Deleted])
|
47 |
82 |
end
|
48 |
83 |
end
|
|
84 |
|
|
85 |
# Ennder flush mails
|
|
86 |
# logger.flush
|
49 |
87 |
end
|
50 |
88 |
imap.expunge
|
51 |
89 |
end
|
... | ... | |
55 |
93 |
def logger
|
56 |
94 |
RAILS_DEFAULT_LOGGER
|
57 |
95 |
end
|
|
96 |
|
|
97 |
# Ennder
|
|
98 |
def trace_to(p_enveloppe)
|
|
99 |
_to_mails = ''
|
|
100 |
|
|
101 |
p_enveloppe.to.each do |_to|
|
|
102 |
_to_mails += _to.mailbox + '@' + _to.host + ','
|
|
103 |
end
|
|
104 |
|
|
105 |
_to_mails
|
|
106 |
end
|
58 |
107 |
end
|
59 |
108 |
end
|
60 |
109 |
end
|