Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot


by nite_man (Deacon)
on May 05, 2003 at 13:20 UTC ( #255613=modulereview: print w/replies, xml ) Need Help??

Item Description: A simple Perl interface to the IMAP server.

Review Synopsis: This review describes methods of module Net::IMAP::Simple.




Joao Fonseca




Perl extension for simple IMAP account handling. This module leaves a possibility to create, delete or rename IMAP mailboxes, read, copy, delete mail messages.


new(host, opt_par1 => val1, opt_par2 => var2, ...)
Method new create a new object  Net::IMAP::Simple Parameter host defines a IMAP host name, another parameters are optional and pass to
directly. Usually, it uses parameter Timeout but you can see other parameters in IO::Socket::INET documentation
The method new returns an object which connected to the IMAP server in successful case or it returns undef and stores an error code into variable $!:
	my $imap = Net::IMAP::Simple->new($my_host, Timeout => 30) or die "Can't connect to $my_host: $!";
login(user_name, user_passw)
This method tries to login on IMAP server using a pair  user_name and user_passw. This method returns a number of messages in the default user's mail box (usually this is INBOX) or undef (at that, you can use a hash for defining of user_name and user_passw):
	defined (my $num_msgs = $imap->login($my_name => $my_passw)) or die "Can't login!";
This method returns a list of all user's mailboxes:
	my $mailbox_list = $imap->mailboxes;
This method selects a mailbox according to mailbox_name and return a number of messages which consist in this mailbox (if mailbox is empty the method select returns 0) or undef if mailbox doesn't exist:
	defined(my $num_msgs = $imap->select($mailbox)) or die "Mailbox $mailbox is invalid!";
Method get returns a reference on array which consists lines of message with number message_number:
	print "Message No 10:\n";
	map { print $_ ."\n" } @{ $imap->get(10) };
This method returns the lines of defined message alike previous method but stores result into temporary file. This means that the message transfers early on user's computer and then you can read it from file:
	my $fh = $imap->getfh(10);
	print "Message No 10:\n";
	print <$fh>;
	close $fh;
This method marks a defined message as delete but, in fact, this message will be delete when method quite will be called. Net::IMAP::Simple doesn't leave to possibility of undelete of message which were marked as delete:
Method top returns a header of defined message as an array reference:
	my $header = $imap->top(10);
	my $head_obj = Mail::Header->new($header);
	my $subj = $head_obj->get('Subject');
	my $from = $head_obj->get('From');	
If this method is called without a parameter, it returns size for all messages in the mailbox as hash reference where key is message ID and value is message size:
	my $all_sizes = $imap->list;
	map { print "Message No $_: $$all_sizes{$_} bytes!\n" } keys %$all_sizes; 


	my $msg_size = $imap->list(10);
	print "Message No 10: $msg_size bytes!\n";
This method returns 'true' if defined message was read and 'false' if not. You can use method seen for determination of list of new messages in your mailbox:
	for(1 .. $num_msgs) { push @new_msgs, $_ unless $imap->seen($_);
	print "You have ".scalar @new_msgs." in your mailbox!\n";
Method quit deletes all marked messages and close a connection with IMAP server:

Others methods

You can manipulate a mailbox by using following methods:

  • $res = $imap->create_mailbox(mailbox_name);
  • $res = $imap->delete_mailbox(mailbox_name);
  • $res = $imap->rename_mailbox(old_mailbox_name, new_mailbox_name);
which create, delete and rename defined mailbox accordingly. Those methods return 'true' in success case or 'false'.
For copy message from one mailbox to another you can use method copy:
$res = $imap->copy(message_number, mailbox_name);
This method returns 'true' in success case or 'false'.


This module will be useful in case of creation a mail checker or simple mail interface to IMAP server. In more complex case preferably use more power modules

  • Net::IMAP leaves larger set of methods for intercept and interpretation of server responses, management of mailboxes and messages etc.
  • Mail::IMAPClient leaves most full interface to the IMAP server but you should be ready to interpret some responses of server by yourself using RFC 2060.
Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: modulereview [id://255613]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2023-03-30 13:43 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (74 votes). Check out past polls.