Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2019-05-26 06:02 GMT
Find Nodes?
    Voting Booth?
    Do you enjoy 3D movies?

    Results (153 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!