Woo hoo!
Non technical people look away now! Code Warning!
I haven't included everything, and I would warn against ripping off any of it right now... you might not like the outcome ;-)
class MailMan
#abstraction of the real world chap who handles sending and delivery of mail
@mailManID
@mailServerOutbound
@mailServerInbound
@mailClientID
@mailClientPassword
#runtime vars
@inbox
#mailserver connection
@pop
@smtp
attr_reader :inbox
def initialize(serverOut, serverIn, username, pwd)
@mailServerOutbound = serverOut
@mailServerInbound = serverIn
@mailClientID = username
@mailClientPassword = pwd
@envelopes = Array.new
end
def sendMail(envelope)
#connect to outbound server
connect(true)
if(@smtp)
@smtp.send_message(envelope.email.sendableMessage, @mailClientID, envelope.emailaddressee)
end
rescue => detail
#if someting goes wrong
puts "not connected to inbound server"
print detail.backtrace.join("\n")
@smtp.finish
end
def getMail()
#check inbound server
connect(false)
if @pop.mails.empty?
'no messages'
else
emailParser = EmailParser.new
if @inbox==nil
@inbox = Inbox.new
end
@pop.each_mail do |mail|
env = Envelope.new
email = Email.new
email.verbatim = mail.pop
email.subject = emailParser.getSubjectFromHeader(mail.header)
email.content = emailParser.getContentFromEmail(mail.pop)
email.uniqueID = mail.unique_id
env.email = email
@inbox.addEmail(env)
#p mail.header
end
end
rescue => detail
#if someting goes wrong
puts "not connected to inbound server"
print detail.backtrace.join("\n")
#end
disconnect(false)
"done"
end
private
def connect(outbound)
if outbound
puts "trying to connect to SMTP outbound server"
begin
@smtp = Net::SMTP.start(@mailServerOutbound, 465, 'localhost.localdomain', @mailClientID, @mailClientPassword, 'cram_md5')
puts "connected to outbound server!"
end
else
begin
#do connection tricky stuff
Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
puts "trying to connect to POP3 inbound server"
@pop = Net::POP3.start(@mailServerInbound, 995, @mailClientID, @mailClientPassword, false)
puts "connected to inbound server!"
end
end
end
def disconnect(outbound)
if outbound
else
@pop.finish
end
end
end
No comments:
Post a Comment