Class: EmailToSms

Inherits:
Object
  • Object
show all
Includes:
Mail::ImapTools, Mail::TmailTools
Defined in:
lib/email_to_sms.rb

Constant Summary collapse

@@SENT_SMS_MAILBOX =
"sent_sms"
@@ERROR_SMS_MAILBOX =
"error"
@@FILTERED_MAILBOX =
"filtered"
@@ENVIRONMENT_MOCK =
2
@@ENVIRONMENT_PRODUCTION =
1
@@CONFIG =
Config.load

Instance Method Summary collapse

Methods included from Mail::TmailTools

#get_receiver_from_subject, #plain_text_body_from_multipart, #puts_tmail, #tmail_to_plaintext

Methods included from Mail::ImapTools

#delete_email, #move_email, #status_or_create_mailbox

Constructor Details

#initialize(environment = @@ENVIRONMENT_MOCK) ⇒ EmailToSms

Returns a new instance of EmailToSms.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/email_to_sms.rb', line 15

def initialize(environment = @@ENVIRONMENT_MOCK)
  @charset        = @@CONFIG["general"]["default_charset"]
  @environment    = environment
  @filter_chain   = FilterChain.build_simple_filter_chain(@@CONFIG, @charset)
  dev_garden_user = @@CONFIG["dev_garden"]["user"]
  dev_garden_pass = @@CONFIG["dev_garden"]["pass"]
  @sms_service    = SmsService::SmsService.new(dev_garden_user, dev_garden_pass)
  @imap           = Net::IMAP.new(@@CONFIG["imap"]["server_host"])
  imap_user       = @@CONFIG["imap"]["user"]
  imap_pass       = @@CONFIG["imap"]["pass"]
  
  @imap.authenticate('LOGIN', imap_user, imap_pass)
  status_or_create_mailboxes
end

Instance Method Details

#closeObject

Close imap connection



52
53
54
55
# File 'lib/email_to_sms.rb', line 52

def close
  @imap.close
  @imap.disconnect
end

#dispatchObject

See which emails to send



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/email_to_sms.rb', line 31

def dispatch
  @imap.select('INBOX')
  puts "Checking mailbox..."
  @imap.uid_search(["UNSEEN"]).each do |uid_unseen|
    tmail_unseen = tmail_from_imap(uid_unseen)

    # Only mails passing all filters will be delivered.
    # Be aware that filters might modify the mail.
    passed = @filter_chain.passed_filter?(tmail_unseen)

    if not passed then
      puts "E-mail has been filtered."
      move_email(uid_unseen, @@FILTERED_MAILBOX)
    else        
      puts "Sending E-mail as text message..."
      send_email_as_sms(uid_unseen, tmail_unseen)
    end
  end
end