Using Perl to Get Mail

Using Perl to Get Mail






Note

We are no longer accepting new customers or work orders at this time. Thank you for your interest.


Getting e-Mail with Perl

There are many ways to get mail from a mail server with perl. The best methods fall into these categories:

  1. use Fetchmail to automatically pull mail from a mail server
  2. use a module to access the POP or IMAP mailbox
  3. talk directly to POP or IMAP port of the mail server
  4. access /var/mail files directly (when your perl script is running on the mail server itself)

That first method above is the best, if it will work for you: no coding involved, because someone else already wrote the program! Fetchmail can download mail from a POP or IMAP server, and send it to your local system's SMTP mail server. This allows your local machine to store the file in a local mailbox, for your local mail client application to access directly. Your local system can thus be an offline mail reader, and use Fetchmail to look for any new incoming mail whenever you're back on the Internet again!

Fetchmail is a great software application that's been around a very long time in the Unix/Linux world. You can visit the home page at http://fetchmail.berlios.de. The manual is located at http://fetchmail.berlios.de/fetchmail-man.html.

If Fetchmail can do what you're trying to do, setting it up will save you a lot of time instead of having to write a Perl script.

The second method above is the next best thing: you are calling an existing module from your Perl script to get mail from a POP or IMAP server. You're basically leveraging working code that other people have written and debugged; you're keeping your coding to a minimum; you're making use of existing standards (POP and IMAP).

The third and fourth methods are complex, and not covered in this article.

Using a Module to Get Mail from a POP or IMAP Server

The best place to look for Perl Modules is at CPAN which is the Comprehensive Perl Archive Network.

You can browse through the descriptions there, to see what you want to use. For example, there's Net::POP3, and Mail::POP3Client.

You can see which ones may already be installed on your Unix/Linux system with:

perldoc Net::POP3

When I run that on my standard CentOS Linux system, I can see it's already installed. That perldoc command will tell you everything you need to write code to talk to the module.

If your system doesn't have the module installed yet, you can install it yourself. If you're the superuser, you can install it system-wide. If not, just install it in a perl lib directory in your home directory someplace.

You can use the CPAN module to do the installation for you easily enough. Simply type this from the Unix shell:

perl -MCPAN -e shell;
install Net::POP3

The CPAN module is good about figuring out what other modules may be needed, finding a server to get the packages from, downloading everything, and installing it all in the proper order.

More About POP and IMAP

POP was the first TCP/IP based "remote mail accessing" protocol used on the Internet. It is extremely simple, and does not support most features that people need today. It was in very wide-spread use before the IMAP protocol was developed, but now IMAP is largely replacing POP as the much-improved standard.

Both protocols define a way to receive emails that have been stored in a mailbox on a mail server. Both protocols provide basic authentication protections (login and password requirements). Both let you read all the messages in the mailbox, and empty the mailbox so that only new incoming messages will be stored there. For example, when running a mail client application on your local PC, it may connect to the mail server, download all your new mail, add those messages to all the email you've already received on your local PC, tell the mail server to empty the mailbox, and disconnect.

This gives you the ability to read your email "offline" - without being connected to the Internet - because your local PC is holding all your email, not the mail server.

IMAP provides many other features, including changing or removing individual messages from the mailbox. For example, maybe you want to keep all your mail in your mailbox on the mail server, but IMAP knows which ones you've already read, so it would like to add a little marker to the header of each message with a "status" of whether you've already read it or not, replied to the sender, etc.

Warning

The POP and IMAP protocols are not secure, normally. Neither is SMTP. The data is not encrypted. This means that a hacker, listening to the packets going by at either end, would be able to reconstruct the email messages traveling along!

There are security protocols integrated into SMTP and IMAP, now, for encrypted mail transfer. However, both ends must be able to converse using the same encryption protocol.

For more information, read these pages on IMAP security:

http://www.coruscant.demon.co.uk/mike/imap/security.html
http://www.imap.org/
http://security.fi.infn.it/tools/stunnel/index-en.html

Books on IMAP and POP

There are some good books from O'Reilly and Associates on these subjects:

Managing IMAP by Dianna Mullet and Kevin Mullet
    see "Other Topics" chapter which talks about SSL security

Network Programming With Perl (O'Reilly Safari book)
    chapter 8 talks about writing client code for POP, IMAP and NNTP

Maximum Linux Security (2nd Edition) by John Ray
    chapter 12 addresses SMTP security and talks about replacing Sendmail with Qmail

Conclusion

That should get you pointed in the right direction for using Perl to get mail from a mail server. I'm sorry I don't have any example code for you right now. If you have a good example you'd like to add to this Perl Trick, or if I did not address some aspect of this topic you were looking for, feel free to Contact Me.

Good luck!



Bookmark and Share


Don't miss the latest perl tips and tricks!
Subscribe to our low-volume mailing list:

Privacy Policy

See other tricks:  Web/PHP | Unix/Linux | Perl | SQL | General


Sample Sites | Customers | Our Team | Contact Us | Tips and Tricks | Tools | Our Network | Home

Copyright © 2006 - 2010 Keith Smith Internet Marketing LLC, all rights reserved.
Problem with this web site? please let us know