- starttls
-
$imap->starttls;
If you start an IMAP session and wish to upgrade to SSL later,
you can use this function to start TLS. This function will try to
"require" IO::Socket::SSL and
Net::SSLeay at runtime.
- login
-
my $inbox_msgs = $imap->login($user, $passwd);
This method takes two required parameters, a username and
password. This pair is authenticated against the server. If
authentication is successful TRUE (1) will be returned
Nothing is returned on failure and the
"errstr()" error handler is set with
the error message.
- status
-
my $num_messages = $imap->status($folder);
my ($unseen, $recent, $num_messages) = $imap->status($folder);
Issue a "STATUS" command.
The "STATUS" command counts messages
without altering the state of the named (optionally) mailbox. It returns
either the number of messages, or the number of unseen messages, recent,
and the total number of messages.
$folder is an optional argument.
"status()" will use the current
mailbox or "INBOX" if the
$folder argument is not provided.
This method does not use caching.
This method can also query custom status values. The first
argument to the function (if any) is assumed to be the folder name, so
the folder argument is required when trying to query custom status
values.
my ($f1, $f2) = $imap->status($folder, qw(f1 f2));
my $f2 = $imap->status($folder, qw(f1 f2));
- uidnext
-
my $uidnext = $imap->uidnext($folder);
Return the "UIDNEXT" value
for a mailbox. The $folder argument is optional.
This is really just an alias for
my $uidnext = $imap->status($folder, qw(uidnext));
with the mild difference that it can compute the folder
argument for you
- uidvalidity
-
my $uidvalidity = $imap->uidnext($folder);
Return the "UIDVALIDITY"
value for a mailbox. The $folder argument is
optional. This is also an alias for the status call like
"uidnext()" above.
- uid
- This function is actually an alias for
"$imap->uidsearch($msg_range)".
my($uid)= $imap->uid($msgno);
my @uid = $imap->uid($msg_range); # eg 4:14 or 15,4,14
Return the "UID" value(s)
for a message. These unique IDs "must" stay the same
during the session and "should" stay the same between
sessions. Whether they stay the same depends on the
"UIDVALIDITY" value; see: above and
RFC3501.
Warning, although you might thing @uid
should contain the "UID"s for 15, then
4, then 14 in the example above; most IMAP servers seem to return the
UIDs in increasing order. Normally the sequence numbers are in
increasing order also, so it all maches up.
my ($uid4, $uid14, $uid15) = $imap->uid("15,4,14"); # warning
One final note, this gives the size of the search match, not
the uid like you might expect:
my $uid_search_result_list_size = $imap->uid('3'); # probably always 1
- seq
-
my $seq = $imap->seq($uids);
my @seq = $imap->seq($uids); # eg 58888:58900
Rather like "uid()" above,
but maps uids to sequence numbers.
- select
-
my $num_messages = $imap->select($folder);
Selects a folder named in the single required parameter. The
number of messages in that folder is returned on success. On failure,
nothing is returned and the "errstr()"
error handler is set with the error message.
- examine
- This is very nearly a synonym for
"select()". The only real difference is
that the EXAMINE command is sent to the server instead of SELECT.
Net::IMAP::Simple is otherwise unaware of the read-only-ness of the
mailbox.
- close
-
$imap->close;
Un-selects the current mailbox, leaving no mailbox
selected.
- messages
-
print "Messages in Junk Mail -- " . $imap->messages("INBOX.Junk Mail") . "\n";
This method is an alias for
"$imap->select"
- flags
-
print "Available server flags: " . join(", ", $imap->flags) . "\n";
This method accepts an optional folder name and returns the
current available server flags as a list, for the selected folder. If no
folder name is provided the last folder
"$imap->select"'ed will be
used.
This method uses caching.
- separator
- Returns the folder separator (technically "hierarchy separator",
rfc3501§6.3.8) for the server.
- recent
-
print "Recent messages value: " . $imap->recent . "\n";
This method accepts an optional folder name and returns the
'RECENT' value provided durning a SELECT result set. If no folder name
is provided the last folder
"$imap->select"'ed will be
used.
This method uses caching.
See also: search
- unseen
-
print "Unseen messages value: " . $imap->unseen . "\n";
This method accepts an optional folder name and returns the
'UNSEEN' value provided during a SELECT command result. If no folder
name is provided the last folder
"$imap->select"'ed will be used. If
a folder name is provided, this will issue a SELECT first.
This method uses caching.
If the server does not provide UNSEEN during SELECT --
surprisingly common -- this method will fall back and use STATUS to
determine the unseen count.
NOTE: This is not the opposite of seen below. The
UNSEEN value varies from server to server, but according to the IMAP
specification, it should be the number of the first unseen
message, in the case the flag is provided. (If the flag is not
provided, users would have to use the SEARCH command to find it.)
See also: search
- current_box
-
print "Current Mail Box folder: " . $imap->current_box . "\n";
This method returns the current working mail box folder
name.
- top
-
my $header = $imap->top( $message_number ); print for @{$header};
This method accepts a message number as its required
parameter. That message will be retrieved from the currently selected
folder. On success this method returns a list reference containing the
lines of the header. Nothing is returned on failure and the
"errstr()" error handler is set with
the error message.
- seen
-
defined( my $seen = $imap->seen( $message_number ) )
or warn "problem testing for \Seen: "
. $imap->errstr;
print "msg #$message_number has been \Seen!" if $seen;
A message number is the only required parameter for this
method. The message's "\Seen" flag
will be examined and if the message has been seen a true value is
returned. A defined false value is returned if the message does not have
the "\Seen" flag set. The undefined
value is returned when an error has occurred while checking the flag
status.
NOTE: This is not the opposite of unseen above. This
issues a "FETCH" command and checks to
see if the given message has been
"\Seen" before.
- deleted
-
defined( my $deleted = $imap->deleted( $message_number ) )
or warn "problem testing for \Deleted: "
. $imap->errstr;
print "msg #$message_number has been \Deleted!" if $deleted;
A message number is the only required parameter for this
method. The message's "\Deleted" flag
will be examined and if the message has been deleted a true value is
returned. A defined false value is returned if the message does not have
the "\Deleted" flag set. The undefined
value is returned when an error has occurred while checking the flag
status.
- list
-
my $message_size = $imap->list($message_number);
my $mailbox_sizes = $imap->list;
This method returns size information for a message, as
indicated in the single optional parameter, or all messages in a
mailbox. When querying a single message a scalar value is returned. When
listing the entire mailbox a hash is returned. On failure, nothing is
returned and the "errstr()" error
handler is set with the error message.
- fetch
-
my $headers = $imap->fetch("1:5")
Fetch the headers for messages 1-5 in the current folder.
for my $midx ( keys %$headers ) {
for my $hdr in ($headers->{$midx}) {
say "$hdr"
# In many situations the headers will be parsed and may not be simple
# strings (e.g., with the SimpleX RecDescent parse). The simplest
# way to get a feel for the output is to use a dumper on it.
}
}
- get
-
my $message = $imap->get( $message_number ) or die $imap->errstr;
my @message_lines = $map->get( $message_number ) or die $imap->errstr;
my $part = $imap->get( $message_number, '1.1' ) or die $imap->errstr;
my @part_lines = $imap->get( $message_number, '1.1' ) or die $imap->errstr;
This method fetches a message and returns its lines as an
array or, the actual message. On failure, either an empty list is
returned and the "errstr()" error
handler is set with the error message.
Optionally, a part can be specified in order to fetch a
specific portion of a message. This is the raw, encoded body of the
message part. The part number is a set of zero or more part specifiers
delimited by periods. Every message has at least one part. Specifying a
part of '1' returns the raw, encoded body. This is only useful if you
know the header information such as encoding.
Historically, "get()"
returned the array of lines as a reference to the array instead of
returning the message or the array itself. Please note that it still
does this, although it may be deprecated in the future.
The scalar result returned is actually a blessed arrayref with
the stringify member overloaded. If you're intending to use the
resulting message as a string more than once, it may make sense
to force the stringification first.
my $message = $imap->get(1);
$message = "$message"; # force stringification
It is not normally necessary to do this.
- put
-
$imap->put( $mailbox_name, $message, @flags ) or warn $imap->errstr;
Save a message to the server under the folder named
$mailbox_name. You may optionally specify flags
for the mail (e.g. "\Seen",
"\Answered"), but they must start with
a slash.
If $message is an arrayref, the lines
will be printed correctly.
- put_with_date
-
$imap->put_with_date( $mailbox_name, $message, $date, @flags ) or warn $imap->errstr;
Save a message to the server under the folder named
$mailbox_name just like the put method above,
but supplying a date will set the IMAP server internal date for the
message if supported per RFC 3501 Section 6.3.11.
Note when using this with Gmail it expects the date format to
be: DD-Mon-YYYY hh:mm:ss tz for example 31-Dec-2016 12:59:59 -0500
- msg_flags
-
my @flags = $imap->msg_flags( $message_number );
my $flags = $imap->msg_flags( $message_number );
# aught to come out roughly the same
print "Flags on message #$message_number: @flags\n";
print "Flags on message #$message_number: $flags\n";
Detecting errors with this member functions is usually
desirable. In the scalar context, detecting an error is synonymous with
testing for defined.
if( defined( my $flags = $imap->msg_flags($num) ) ) {
# it has $flags!
} else {
warn "problem listing flags for message #$num: "
. $imap->errstr;
}
In list context, you must call waserr() to test for
success.
my @flags = $imap->msg_flags($num);
warn "problem listing flags for msg #$num: "
. $imap->errstr if $imap->waserr;
- getfh
-
my $file = $imap->getfh( $message_number ); print <$file>;
On success this method returns a file handle pointing to the
message identified by the required parameter. On failure, nothing is
returned and the "errstr()" error
handler is set with the error message.
- quit
-
$imap->quit;
OR
$imap->quit(BOOL);
This method logs out of the IMAP server, expunges the selected
mailbox, and closes the connection. No error message will ever be
returned from this method.
Optionally if BOOL is TRUE (1) then a hard quit is performed
which closes the socket connection. This hard quit will still issue both
EXPUNGE and LOGOUT commands however the response is ignored and the
socket is closed after issuing the commands.
- logout
-
$imap->logout;
This method is just like the quit method except that it does
not have a hard quit option and it does not expunge the mailbox before
it hangs up and closes the socket.
- last
-
my $message_number = $imap->last;
This method returns the message number of the last message in
the selected mailbox, since the last time the mailbox was selected. On
failure, nothing is returned and the
"errstr()" error handler is set with
the error message.
- delete
-
print "Gone!" if $imap->delete( $message_number );
This method sets the
"\Deleted" flag on the given message
(or messages). On success it returns true, false on failure and the
"errstr()" error handler is set with
the error message. If the flag was already there, no error is produced.
I takes either a message number or "sequence set" as the only
argument. Note that messages aren't actually deleted until they are
expunged (see expunge_mailbox).
- undelete
-
print "Resurrected!" if $imap->undelete( $message_number );
This method removes the
"\Deleted" flag on the given message.
On success it returns true, false on failure and the
"errstr()" error handler is set with
the error message. If the flag wasn't there, no error is produced.
- see
-
print "You've seen message #$msgno" if $imap->see( $messageno );
This method sets the "\Seen"
flag on the given message. On success it returns true, false on failure
and the "errstr()" error handler is
set with the error message. If the flag was already there, no error is
produced.
- unsee
-
print "You've not seen message #$msgno" if $imap->unsee( $messageno );
This method removes the
"\Seen" flag on the given message. On
success it returns true, false on failure and the
"errstr()" error handler is set with
the error message. If the flag wasn't there, no error is produced.
- add_flags
- delete and see above really just call this function for those flags.
$imap->add_flags( $msgno, qw(\Seen \Deleted) )
or die $imap->errstr;
- sub_flags
- unsee above really just calls this function for that flag.
$imap->sub_flags( $msgno, '\Seen' ) or die $imap->errstr;
- mailboxes
-
my @boxes = $imap->mailboxes;
my @folders = $imap->mailboxes("Mail/%");
my @lists = $imap->mailboxes("lists/perl/*", "/Mail/");
This method returns a list of mailboxes. When called with no
arguments it recurses from the IMAP root to get all mailboxes. The first
optional argument is a mailbox path and the second is the path
reference. RFC 3501 section 6.3.8 has more information.
On failure nothing is returned and the
"errstr()" error handler is set with
the error message.
- mailboxes_subscribed
-
my @boxes = $imap->mailboxes_subscribed;
my @folders = $imap->mailboxes_subscribed("Mail/%");
my @lists = $imap->mailboxes_subscribed("lists/perl/*", "/Mail/");
This method returns a list of mailboxes subscribed to. When
called with no arguments it recurses from the IMAP root to get all
mailboxes. The first optional argument is a mailbox path and the second
is the path reference. RFC 3501 has more information.
On failure nothing is returned and the
"errstr()" error handler is set with
the error message.
- create_mailbox
-
print "Created" if $imap->create_mailbox( "/Mail/lists/perl/advocacy" );
This method creates the mailbox named in the required
argument. Returns true on success, false on failure and the
"errstr()" error handler is set with
the error message.
- expunge_mailbox
-
my @expunged = $imap->expunge_mailbox( "/Mail/lists/perl/advocacy" );
die $imap->errstr if $imap->waserr;
my $expunged = $imap->expunge_mailbox( "/Mail/lists/perl/advocacy" )
or die $imap->errstr;
This method removes all mail marked as deleted in the mailbox
named in the required argument. Returns either the number of messages
that were expunged, or the indexes of those messages -- which has a
questionable usefulness since it tends to return numbers that don't
relate to the message numbers marked with the
"\Deleted" flags.
If 0 messages were expunged without error, the function will
return 0E0 so it will still test true, but also
evaluate to 0.
In list context, you must call waserr() to test for
success.
- delete_mailbox
-
print "Deleted" if $imap->delete_mailbox( "/Mail/lists/perl/advocacy" );
This method deletes the mailbox named in the required
argument. Returns true on success, false on failure and the
"errstr()" error handler is set with
the error message.
- rename_mailbox
-
print "Renamed" if $imap->rename_mailbox( $old => $new );
This method renames the mailbox in the first required argument
to the mailbox named in the second required argument. Returns true on
success, false on failure and the
"errstr()" error handler is set with
the error message.
- folder_subscribe
-
print "Subscribed" if $imap->folder_subscribe( "/Mail/lists/perl/advocacy" );
This method subscribes to the folder. Returns true on success,
false on failure and the "errstr()"
error handler is set with the error message.
- folder_unsubscribe
-
print "Unsubscribed" if $imap->folder_unsubscribe( "/Mail/lists/perl/advocacy" );
This method un-subscribes to the folder. Returns true on
success, false on failure and the
"errstr()" error handler is set with
the error message.
- copy
-
print "copied" if $imap->copy( $message_number, $mailbox );
This method copies the message number (or "sequence
set") in the currently selected mailbox to the folder specified in
the second argument. Both arguments are required. On success this method
returns true. Returns false on failure and the
"errstr()" error handler is set with
the error message.
- uidcopy
-
print "copied" if $imap->uidcopy( $message_uid, $mailbox );
This method is identical to
"copy()" above, except that it uses
UID numbers instead of sequence numbers.
- noop
-
$imap->noop;
Performs a null operation. This may be needed to get updates
on a mailbox, or ensure that the server does not close the connection as
idle. RFC 3501 states that servers' idle timeouts must not be less than
30 minutes.
- errstr
-
print "Login ERROR: " . $imap->errstr . "\n" if !$imap->login($user, $pass);
Return the last error string captured for the last operation
which failed.
- waserr
-
my @flags = $imap->msg_flags(14);
die $imap->errstr if $imap->waserr;
Because "msg_flags()" can
optionally return a list, it's not really possible to detect failure in
list context. Therefore, you must call
"waserr()" if you wish to detect
errors.
Few of the Net::IMAP::Simple methods use
"waserr()". The ones that do will
mention it.
- list2range
- Sometimes you have a long list of sequence numbers which are consecutive
and really want to be an IMAP-style range.
my @list = (5..9, 13..38, 55,56,57);
my $short = $imap->list2range(@list);
# $short how says: 5:9,13:38,55:57
- range2list
- Pretty much the opposite of
"list2range".
my @list = $imap->range2list("1,3,5:9");
# @list is (1,3,5,6,7,8,9);