class Gmail::Message

Constants

PREFETCH_ATTRS

Public Class Methods

new(mailbox, uid, _attrs = nil) click to toggle source
# File lib/gmail/message.rb, line 8
def initialize(mailbox, uid, _attrs = nil)
  @uid     = uid
  @mailbox = mailbox
  @gmail   = mailbox.instance_variable_get("@gmail") if mailbox # UGLY
  @_attrs  = _attrs
end

Public Instance Methods

add_label(name) click to toggle source

Use Gmail IMAP Extensions to add a Label to an email

# File lib/gmail/message.rb, line 144
def add_label(name)
  @gmail.mailbox(@mailbox.name) do
    @gmail.conn.uid_store(uid, "+X-GM-LABELS", [Net::IMAP.encode_utf7(name.to_s)])
    clear_cached_attributes
  end
end
Also aliased as: label, label!, add_label!
add_label!(name)
Alias for: add_label
archive!() click to toggle source

Archiving is done by adding the `All Mail` label. To undo this, you just re-apply the `Inbox` label (see `#unarchive!`)

IMAP's fetch('1:100', (X-GM-LABELS)) function does not fetch inbox, just emails labeled important? stackoverflow.com/a/28973760 In my testing the currently selected mailbox is always excluded from the X-GM-LABELS results. When you called conn.select() it implicitly selected 'INBOX', therefore excluding 'Inbox' from the list of labels. If you selected a different mailbox then you would see '\\Inbox' in your results:

# File lib/gmail/message.rb, line 125
def archive!
  @gmail.find(message.message_id).remove_label('\Inbox')
end
as_json() click to toggle source
Calls superclass method
# File lib/gmail/message.rb, line 167
def as_json
  super(except: ["gmail"])
end
delete!() click to toggle source

Deleting is done by adding the `Trash` label. To undo this, you just re-apply the `Inbox` label (see `#undelete!`)

# File lib/gmail/message.rb, line 112
def delete!
  add_label("\\Trash")
end
envelope() click to toggle source
# File lib/gmail/message.rb, line 29
def envelope
  @envelope ||= fetch("ENVELOPE")
end
flag(name) click to toggle source

Mark message with given flag.

# File lib/gmail/message.rb, line 47
def flag(name)
  !!@gmail.mailbox(@mailbox.name) do
    @gmail.conn.uid_store(uid, "+FLAGS", [name])
    clear_cached_attributes
  end
end
flags() click to toggle source
# File lib/gmail/message.rb, line 38
def flags
  @flags ||= fetch("FLAGS")
end
inspect() click to toggle source
# File lib/gmail/message.rb, line 163
def inspect
  "#<Gmail::Message#{'0x%04x' % (object_id << 1)} mailbox=#{@mailbox.name}#{' uid=' + @uid.to_s if @uid}#{' message_id=' + @msg_id.to_s if @msg_id}>"
end
label(name)
Alias for: add_label
label!(name)
Alias for: add_label
labels() click to toggle source
# File lib/gmail/message.rb, line 42
def labels
  @labels ||= fetch("X-GM-LABELS")
end
mark(flag) click to toggle source

Do commonly used operations on message.

# File lib/gmail/message.rb, line 63
def mark(flag)
  case flag
  when :read    then read!
  when :unread  then unread!
  when :deleted then delete!
  when :spam    then spam!
  else
    flag(flag)
  end
end
message() click to toggle source
# File lib/gmail/message.rb, line 33
def message
  @message ||= Mail.new(fetch("BODY[]"))
end
Also aliased as: raw_message
message_id()
Alias for: msg_id
method_missing(meth, *args, &block) click to toggle source
Calls superclass method
# File lib/gmail/message.rb, line 171
def method_missing(meth, *args, &block)
  # Delegate rest directly to the message.
  if envelope.respond_to?(meth)
    envelope.send(meth, *args, &block)
  elsif message.respond_to?(meth)
    message.send(meth, *args, &block)
  else
    super
  end
end
move(name, from = nil)
Alias for: move_to
move!(name, from = nil)
Alias for: move_to
move_to(name, from = nil) click to toggle source
# File lib/gmail/message.rb, line 135
def move_to(name, from = nil)
  add_label(name)
  @gmail.find(message.message_id).remove_label(from) if from
end
Also aliased as: move, move!, move_to!
move_to!(name, from = nil)
Alias for: move_to
msg_id() click to toggle source
# File lib/gmail/message.rb, line 19
def msg_id
  @msg_id ||= fetch("X-GM-MSGID")
end
Also aliased as: message_id
raw_message()
Alias for: message
read!() click to toggle source

Mark as read.

# File lib/gmail/message.rb, line 80
def read!
  flag(:Seen)
end
read?() click to toggle source

Check whether message is read

# File lib/gmail/message.rb, line 75
def read?
  flags.include?(:Seen)
end
remove_label(name) click to toggle source

Use Gmail IMAP Extensions to remove a Label from an email

# File lib/gmail/message.rb, line 155
def remove_label(name)
  @gmail.mailbox(@mailbox.name) do
    @gmail.conn.uid_store(uid, "-X-GM-LABELS", [Net::IMAP.encode_utf7(name.to_s)])
    clear_cached_attributes
  end
end
Also aliased as: remove_label!
remove_label!(name)
Alias for: remove_label
respond_to?(meth, *args, &block) click to toggle source
Calls superclass method
# File lib/gmail/message.rb, line 182
def respond_to?(meth, *args, &block)
  return true if envelope.respond_to?(meth) || message.respond_to?(meth)
  super(meth, *args, &block)
end
respond_to_missing?(meth, include_private = false) click to toggle source
Calls superclass method
# File lib/gmail/message.rb, line 187
def respond_to_missing?(meth, include_private = false)
  envelope.respond_to?(meth) || message.respond_to?(meth) || super
end
spam!() click to toggle source

Marking as spam is done by adding the `Spam` label. To undo this, you just re-apply the `Inbox` label (see `#unspam!`)

# File lib/gmail/message.rb, line 106
def spam!
  add_label("\\Spam")
end
star!() click to toggle source

Mark message with star.

# File lib/gmail/message.rb, line 95
def star!
  flag(:Flagged)
end
starred?() click to toggle source

Check whether message is starred

# File lib/gmail/message.rb, line 90
def starred?
  flags.include?(:Flagged)
end
thr_id() click to toggle source
# File lib/gmail/message.rb, line 24
def thr_id
  @thr_id ||= fetch("X-GM-THRID")
end
Also aliased as: thread_id
thread_id()
Alias for: thr_id
uid() click to toggle source
# File lib/gmail/message.rb, line 15
def uid
  @uid ||= fetch("UID")
end
unarchive!() click to toggle source
# File lib/gmail/message.rb, line 129
def unarchive!
  @gmail.find(message.message_id).add_label('\Inbox')
end
Also aliased as: unspam!, undelete!
undelete!()
Alias for: unarchive!
unflag(name) click to toggle source

Unmark message.

# File lib/gmail/message.rb, line 55
def unflag(name)
  !!@gmail.mailbox(@mailbox.name) do
    @gmail.conn.uid_store(uid, "-FLAGS", [name])
    clear_cached_attributes
  end
end
unread!() click to toggle source

Mark as unread.

# File lib/gmail/message.rb, line 85
def unread!
  unflag(:Seen)
end
unspam!()
Alias for: unarchive!
unstar!() click to toggle source

Remove message from list of starred.

# File lib/gmail/message.rb, line 100
def unstar!
  unflag(:Flagged)
end

Private Instance Methods

clear_cached_attributes() click to toggle source
# File lib/gmail/message.rb, line 193
def clear_cached_attributes
  @_attrs   = nil
  @msg_id   = nil
  @thr_id   = nil
  @envelope = nil
  @message  = nil
  @flags    = nil
  @labels   = nil
end
fetch(value) click to toggle source
# File lib/gmail/message.rb, line 203
def fetch(value)
  @_attrs ||= begin
    @gmail.mailbox(@mailbox.name) do
      @gmail.conn.uid_fetch(uid, PREFETCH_ATTRS)[0]
    end
  end
  @_attrs.attr[value]
end