http://qs321.pair.com?node_id=255613

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

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

Name

Net::IMAP::Simple

Author

Joao Fonseca

Version

0.93

Description

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

Methods

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
IO::Socket::INET
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!";
	
mailboxes()
This method returns a list of all user's mailboxes:
	my $mailbox_list = $imap->mailboxes;
	
select(mailbox_name)
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!";
	
get(message_number)
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) };
	
getfh(message_number)
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;
	
delete(message_number)
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:
	$imap->delete(10);
	
top(message_number)
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');	
	
list()
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; 

	or

	my $msg_size = $imap->list(10);
	print "Message No 10: $msg_size bytes!\n";
	
seen(message_number)
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";
	
quit()
Method quit deletes all marked messages and close a connection with IMAP server:
	$imap->quit;
	

Others methods

You can manipulate a mailbox by using following methods:

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'.

Resume

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