Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??




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.

  • In reply to Net::IMAP::Simple by nite_man

    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post; it's "PerlMonks-approved HTML":

    • Are you posting in the right place? Check out Where do I post X? to know for sure.
    • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
      <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
    • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
    • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2022-08-16 19:37 GMT
Find Nodes?
    Voting Booth?

    No recent polls found