class Gmail::Client::Base

Constants

GMAIL_IMAP_HOST

GMail IMAP defaults

GMAIL_IMAP_PORT
GMAIL_SMTP_HOST

GMail SMTP defaults

GMAIL_SMTP_PORT

Attributes

options[R]
username[R]

Public Class Methods

new(username, options={}) click to toggle source
# File lib/gmail/client/base.rb, line 17
      def initialize(username, options={})
        defaults       = {}
        @username      = fill_username(username)
        @options       = defaults.merge(options)
#        @mailbox_mutex = Mutex.new
      end

Public Instance Methods

compose(mail=nil, &block) click to toggle source

Compose new e-mail.

Examples

mail = gmail.compose
mail.from "test@gmail.org"
mail.to "friend@gmail.com"

… or block style:

mail = gmail.compose do 
  from "test@gmail.org"
  to "friend@gmail.com"
  subject "Hello!"
  body "Hello my friend! long time..."
end

Now you can deliver your mail:

gmail.deliver(mail)
# File lib/gmail/client/base.rb, line 96
def compose(mail=nil, &block)
  if block_given?
    mail = Mail.new(&block)
  elsif !mail 
    mail = Mail.new
  end 

  mail.delivery_method(*smtp_settings)
  mail.from = username unless mail.from
  mail
end
Also aliased as: message
conn()
Alias for: connection
connect(raise_errors=false) click to toggle source

Connect to gmail service.

# File lib/gmail/client/base.rb, line 25
def connect(raise_errors=false)
  @imap = Net::IMAP.new(GMAIL_IMAP_HOST, GMAIL_IMAP_PORT, true, nil, false)
rescue SocketError
  raise_errors and raise ConnectionError, "Couldn't establish connection with GMail IMAP service"
end
connect!() click to toggle source

This version of connect will raise error on failure…

# File lib/gmail/client/base.rb, line 32
def connect!
  connect(true)
end
connection() click to toggle source

Return current connection. Log in automaticaly to specified account if it is necessary.

# File lib/gmail/client/base.rb, line 38
def connection
  login and at_exit { logout } unless logged_in?
  @imap
end
Also aliased as: conn
deliver(mail=nil, raise_errors=false, &block) click to toggle source

Compose (optionaly) and send given email.

Examples

gmail.deliver do 
  to "friend@gmail.com"
  subject "Hello friend!"
  body "Hi! How are you?"
end

… or with already created message:

mail = Mail.new { ... }
gmail.deliver(mail)

mail = gmail.compose { ... }
gmail.deliver(mail)
# File lib/gmail/client/base.rb, line 126
def deliver(mail=nil, raise_errors=false, &block)
  mail = compose(mail, &block) if block_given?
  mail.deliver!
rescue Object => ex
  raise_errors and raise DeliveryError, "Couldn't deliver email: #{ex.to_s}"
end
deliver!(mail=nil, &block) click to toggle source

This version of deliver will raise error on failure…

# File lib/gmail/client/base.rb, line 134
def deliver!(mail=nil, &block)
  deliver(mail, true, &block)
end
fill_username(username) click to toggle source
# File lib/gmail/client/base.rb, line 188
def fill_username(username)
  username =~ /@/ ? username : "#{username}@gmail.com"
end
in_label(name, &block)
Alias for: mailbox
in_mailbox(name, &block)
Alias for: mailbox
inbox() click to toggle source

Alias for mailbox("INBOX"). See Gmail::Client#mailbox for details.

# File lib/gmail/client/base.rb, line 176
def inbox
  mailbox("INBOX")
end
inspect() click to toggle source
# File lib/gmail/client/base.rb, line 184
def inspect
  "#<Gmail::Client#{'0x%04x' % (object_id << 1)} (#{username}) #{'dis' if !logged_in?}connected>"
end
label(name, &block)
Alias for: mailbox
labels() click to toggle source

Return labels object, which helps you with managing your GMail labels. See Gmail::Labels for details.

# File lib/gmail/client/base.rb, line 72
def labels
  @labels ||= Labels.new(conn)
end
logged_in?() click to toggle source

Returns true when you are logged in to specified account.

# File lib/gmail/client/base.rb, line 57
def logged_in?
  !!@logged_in
end
Also aliased as: signed_in?
login(*args) click to toggle source

Login to specified account.

# File lib/gmail/client/base.rb, line 45
def login(*args)
  raise NotImplementedError, "The `#{self.class.name}#login` method is not implemented."
end
Also aliased as: sign_in
login!() click to toggle source

This version of login will raise error on failure…

# File lib/gmail/client/base.rb, line 51
def login!
  login(true)
end
Also aliased as: sign_in!
logout() click to toggle source

Logout from GMail service.

# File lib/gmail/client/base.rb, line 63
def logout
  @imap && logged_in? and @imap.logout
ensure
  @logged_in = false
end
Also aliased as: sign_out
mail_domain() click to toggle source
# File lib/gmail/client/base.rb, line 192
def mail_domain
  username.split('@')[0]
end
mailbox(name, &block) click to toggle source

Do something with given mailbox or within it context.

Examples

mailbox = gmail.mailbox("INBOX")
mailbox.emails(:all)
mailbox.count(:unread, :before => Time.now-(20*24*3600))

… or block style:

gmail.label("Work") do |mailbox|
  mailbox.emails(:unread)
  mailbox.count(:all)
  ...
end
# File lib/gmail/client/base.rb, line 153
      def mailbox(name, &block)
#        @mailbox_mutex.synchronize do
          name = name.to_s
          mailbox = (mailboxes[name] ||= Mailbox.new(self, name))
          switch_to_mailbox(name) if @current_mailbox != name

          if block_given?
            mailbox_stack << @current_mailbox
            result = block.arity == 1 ? block.call(mailbox) : block.call
            mailbox_stack.pop
            switch_to_mailbox(mailbox_stack.last)
            return result
          end

          return mailbox
 #       end
      end
Also aliased as: in_mailbox, in_label, label
mailboxes() click to toggle source
# File lib/gmail/client/base.rb, line 180
def mailboxes
  @mailboxes ||= {}
end
message(mail=nil, &block)
Alias for: compose
sign_in(*args)
Alias for: login
sign_in!()
Alias for: login!
sign_out()
Alias for: logout
signed_in?()
Alias for: logged_in?

Private Instance Methods

mailbox_stack() click to toggle source
# File lib/gmail/client/base.rb, line 206
def mailbox_stack
  @mailbox_stack ||= []
end
smtp_settings() click to toggle source
# File lib/gmail/client/base.rb, line 210
def smtp_settings
  [:smtp, {
    :address => GMAIL_SMTP_HOST,
    :port => GMAIL_SMTP_PORT,
    :domain => mail_domain,
    :user_name => username,
    :password => password,
    :authentication => 'plain',
    :enable_starttls_auto => true
  }]
end
switch_to_mailbox(mailbox) click to toggle source
# File lib/gmail/client/base.rb, line 198
def switch_to_mailbox(mailbox)
  if mailbox
    mailbox = Net::IMAP.encode_utf7(mailbox)
    conn.select(mailbox)
  end
  @current_mailbox = mailbox
end