Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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:

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

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

  • 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

    Title:
    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 How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-19 06:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found