IMAP is handling mail in many cases. For example, POP3’s single mailbox model can lead to thousands of large messages in a single location, making access and control of the mailbox tough. IMAP, however, is designed specifically for leaving the messages on the server, classified in various mailboxes. Also, IMAP can check for new mail over an existing IMAP connection, while POP3 has to open a new connection every time when it contacts server.
Let’s run IMAP using PERL. I use ifrom for listing, printing, and moving messages on an IMAP server. I also run it nightly for backing up my IMAP mail
Listing Inbox Mails
Invocation of ifrom is just so, without any options. It will access your INBOX mailbox on the IMAP server and list your mails in a numbered list. The -host option tells ifrom which IMAP server to contact.
Note that ifrom uses the AppConfig module, which provides for default values, among other nice features.
Authentication in ifrom is done with plain LOGIN
The authinfo file is used by other programs, and the format is variable depending on what those programs need; ifrom parses it looking for the machine, login, password, and port keywords. If any of those keywords are not specified, the defaults are used. The authinfo file overrides the -user and -password command-line switches. Make sure your authinfo file is only readable by you!
The machine name is your IMAP server’s name. The machine name in the authinfo file has to match exactly the name given to ifrom with the -host switch.
When ifrom connects to the server, it sets the Peek variable to 1, so that messages we examine are not marked read.
Next, ifrom opens the mailbox specified by the -mailbox switch. This is INBOX by default: the standard IMAP main mailbox.
After all this trouble, ifrom prints out messages using this format:
Follow the link to the Authmechanism capability of the Mail::IMAPClient package in Resources. You can use CRAM-MD5 authentication with the -crammd5 switch; no other mechanisms are supported by Mail::IMAPClient, so I did not bother to set up a generic -authentication switch. Note that you can implement your own authentication; see the Mail::IMAPClient documentation.
if ($config->CRAMMD5())
{
my $authmech = "CRAM-MD5";
if ($imap->has_capability($authmech))
{
print "Switching to $authmech authentication\n";
$imap->Authmechanism($authmech);
}
}
You can specify your name and password from the command line with the -user and -password switches, or through an authinfo (AKA netrc) file. The authinfo file stores a list of authentication options, such as:
maachine imap.yourserver.here login webnesbay password test1234 machine imap.yourserver.there password test1234 port 244
Print a message header
printf "%5d %-35.35s %s\n", $count, $address,
((defined $data->{Subject}->[0]) ? $data->{Subject}->[0] : '');
The sender's address in$addressis extracted from the full user name whenever possible, using a simple regular expression match. If you giveifromthe-dumpswitch, it will also print the message's contents after the header. This is very useful if you don't have time to launch your e-mail application to see the contents of a last-minute message. It's okay to interruptifromwhile it's printing a large message; the message will not be deleted or otherwise affected, since we opened the server with thePeekoption set to 1. Note that the-dumpswitch uses thebody_string()function, whereas the-backupoption we'll see later uses themessage_string()function.Moving Mails
Occasionally, you want to move messages from one mailbox to another. If there are many messages and your mail client loses the connection to the IMAP server while moving them (as happens often over a slow network link), this capability is for you. Just use the-mailboxand-tooptions. It's that simple. Runifromlike this:
ifrom -mailbox newmail -to archive How the messages get moved by Perlforeach my $message (@msg_list) { $count++; if ($config->TO) { die "Could not move message $message: $!" unless $imap->move($config->TO, $message); print "Moved message $message to " . $config->TO, "\n"; $imap->expunge() if $config->EXPUNGE_OFTEN; last if $count >= $config->N; } } if ($config->TO) { $imap->expunge(); } Moving messages in IMAP does not respect thePeeksetting. Even if it is set to 1,Peekis only relevant to looking at messages. Moving the messages will always delete their originals after the copies have been made in the destination mailbox, as implemented byMail::IMAPClient.Expunging and deleting mailboxes
The
-expunge_oftenflag is for moving messages when you are on an unreliable link and could get disconnected any time. It ensures that after every message is moved,expunge()will be called (otherwise, the message will remain in the mailbox). It is better, however, to use plainexpunge()after all the messages are moved, in combination with the-nflag. That way,expunge()will be called after every 10, 15, or however many messages you specify, are moved. I also gaveifromthe-delete_mailbox_reallyoption. When given that option,ifromwill delete whatever mailbox is named by the-mailboxswitch, so don't use it on the default INBOX mailbox! The Perl code involved couldn't be simpler When moving messages above, you must have noticed the mysteriousexpunge()function. It simply tells the IMAP server to flush the mailbox, removing all messages marked as deleted. Normally, when listing messages, we don't mark them as deleted because we havePeekset to 1. Themove()function, however, will mark them as deleted as mentioned above. In order to really delete those messages, theexpunge()function must be called. Deleting a mailboxif ($config->DELETE_MAILBOX_REALLY) { $imap->delete($config->MAILBOX) or warn "Could not delete mailbox " . $config->MAILBOX . "\n"; }
Backing up IMAP mail
To back up your mail using ifrom, just give it the -backup flag. The -savedir switch is also important — set it to wherever you want the saved messages to go. Everything else — authentication, host, port, and so on — works just like the regular ifrom. The -mailbox parameter doesn’t work, because ifrom backs up all the mailboxes. I could have had a special -backup_mailbox flag to override “all mailboxes,” but frankly I never needed it.
Back up your mails
foreach my $message (@msg_list) { my $filename = "$dir/$f/$message"; next if -e $filename; print "saving message $f/$message to $filename\n" if $config->VERBOSE; my $data_fh = new IO::File $filename, "w"; my $data = $imap->message_string($message); warn "Empty message data for $f/$message" unless defined $data && length $data; $data_fh->print($data); } Thebody_string()function used by the-dumpswitch earlier skips the messages headers, which are usually not interesting when you're looking at e-mail quickly. I have already used theifrombackups several times, when I've lost important mail by accident. It's a load off my mind to know that even if the ISP that runs my IMAP server should lose my mailboxes (it's happened to me before), I will lose at most one day's worth of mail. Reference: IBM.com
Related posts:
- Send sms using perl
- Send and Receive Hotmail through Evolution in ubuntu
- send email with attachments in perl
- Create PDF using perl
- Sockets and TCP/IP Networking using PERL
- Perl script for search & replace in several files
- Special shortcuts in Perl
- search & replace in several files in perl script
- Perl Server Side CGI Scripting
- convert date to unix time in perl

