net/imap.rb

Copyright (C) 2000 Shugo Maeda <shugo@ruby-lang.org>

This library is distributed under the terms of the Ruby license. You can freely distribute/modify this library.

class Net::IMAP

Net::IMAP implements Internet Message Access Protocol (IMAP) clients.

Super Class

Object

Class Methods

new(host, port = 143)

Creates a new Net::IMAP object and connects it to the specified port on the named host.

Methods

greeting

Returns an initial greeting response from the server.

responses

Returns recorded untagged responses.

ex).

imap.select("inbox")
p imap.responses["EXISTS"][-1]
#=> [2]
p imap.responses["UIDVALIDITY"][-1]
#=> [968263756]
disconnect

Disconnects from the server.

capability

Sends a CAPABILITY command, and returns a listing of capabilities that the server supports.

noop

Sends a NOOP command to the server. It does nothing.

logout

Sends a LOGOUT command to inform the server that the client is done with the connection.

authenticate(auth_type, arg...)

Sends an AUTEHNTICATE command to authenticate the client. The auth_type parameter is a string that represents the authentication mechanism to be used. Currently Net::IMAP supports "LOGIN" and "CRAM-MD5" for the auth_type.

ex).

imap.authenticate('LOGIN', user, password)
login(user, password)

Sends a LOGIN command to identify the client and carries the plaintext password authenticating this user.

select(mailbox)

Sends a SELECT command to select a mailbox so that messages in the mailbox can be accessed.

examine(mailbox)

Sends a EXAMINE command to select a mailbox so that messages in the mailbox can be accessed. However, the selected mailbox is identified as read-only.

create(mailbox)

Sends a CREATE command to create a new mailbox.

delete(mailbox)

Sends a DELETE command to remove the mailbox.

rename(mailbox, newname)

Sends a RENAME command to change the name of the mailbox to the newname.

subscribe(mailbox)

Sends a SUBSCRIBE command to add the specified mailbox name to the server's set of "active" or "subscribed" mailboxes.

unsubscribe(mailbox)

Sends a UNSUBSCRIBE command to remove the specified mailbox name from the server's set of "active" or "subscribed" mailboxes.

list(refname, mailbox)

Sends a LIST command, and returns a subset of names from the complete set of all names available to the client.

ex).

imap.create("foo/bar")
imap.create("foo/baz")
p imap.list("", "foo/%")
#=> [[[:NoSelect], "/", "foo/"], [[:NoInferiors], "/", "foo/baz"], [[:NoInferiors], "/", "foo/bar"]]
lsub(refname, mailbox)

Sends a LSUB command, and returns a subset of names from the set of names that the user has declared as being "active" or "subscribed".

status(mailbox, attr)

Sends a STATUS command, and returns the status of the indicated mailbox.

ex).

p imap.status("inbox", ["MESSAGES", "RECENT"])
#=> {"RECENT"=>0, "MESSAGES"=>5}
append(mailbox, message, flags = nil, date_time = nil)

Sends a APPEND command to append the message to the end of the mailbox.

ex).

imap.append("inbox", <<EOF.gsub(/\n/, "\r\n"), [:Seen], Time.now)
Subject: hello
From: shugo@ruby-lang.org
To: shugo@ruby-lang.org

hello world
EOF
check

Sends a CHECK command to request a checkpoint of the currently selected mailbox.

close

Sends a CLOSE command to close the currently selected mailbox. The CLOSE command permanently removes from the mailbox all messages that have the \Deleted flag set.

expunge

Sends a EXPUNGE command to permanently remove from the currently selected mailbox all messages that have the \Deleted flag set.

search(keys, charset = nil)
uid_search(keys, charset = nil)

Sends a SEARCH command to search the mailbox for messages that match the given searching criteria, and returns message sequence numbers (search) or unique identifiers (uid_search).

ex).

p imap.search(["SUBJECT", "hello"])
#=> [1, 6, 7, 8]
fetch(set, attr)
uid_fetch(set, attr)

Sends a FETCH command to retrieve data associated with a message in the mailbox. the set parameter is a number or an array of numbers or a Range object. the number is a message sequence number (fetch) or a unique identifier (uid_fetch).

ex).

p imap.fetch(6..-1, "UID")
#=> [[6, {"UID"=>28}], [7, {"UID"=>29}], [8, {"UID"=>30}]]
store(set, attr, flags)
uid_store(set, attr, flags)

Sends a STORE command to alter data associated with a message in the mailbox. the set parameter is a number or an array of numbers or a Range object. the number is a message sequence number (fetch) or a unique identifier (uid_fetch).

ex).

p imap.store(6..-1, "+FLAGS", [:Deleted])
#=> [[6, {"FLAGS"=>[:Deleted]}], [7, {"FLAGS"=>[:Seen, :Deleted]}], [8, {"FLAGS"=>[:Seen, :Deleted]}]]
copy(set, mailbox)
uid_copy(set, mailbox)

Sends a COPY command to copy the specified message(s) to the end of the specified destination mailbox. the set parameter is a number or an array of numbers or a Range object. the number is a message sequence number (fetch) or a unique identifier (uid_fetch).

sort(sort_keys, search_keys, charset)
uid_sort(sort_keys, search_keys, charset)

Sends a SORT command to sort messages in the mailbox.

ex).

p imap.sort(["FROM"], ["ALL"], "US-ASCII")
#=> [1, 2, 3, 5, 6, 7, 8, 4, 9]
p imap.sort(["DATE"], ["SUBJECT", "hello"], "US-ASCII")
#=> [6, 7, 8, 1]