Write an application that reads POP3 mailboxes in Perl

Source: Internet
Author: User
Tags object mail net stdin mozilla thunderbird perl script microsoft outlook


This article discusses the ability of developers to write POP3 e-mail applications with Perl-specific capabilities.



Email clients such as Microsoft Outlook and Mozilla Thunderbird (Thunderbird) can make email very easy--most of the time you just have to click the button on the toolbar The software will perform complex tasks such as communicating with your e-mail server, verifying your password, and collecting e-mail.



But there is a lot of software programming involved behind this deceptive, simple representation. And, if you're a software developer, you might one day find out how good it would be if you could put such a program in your application.



Don't worry so much, because if you're using Perl, you can use a small CPAN module called Net::P OP3 to do this, and this very useful module will do most of the complex work for you. This module exposes a simple but very powerful API to the POP3 server, offering a number of preset methods for server authentication, message listing and collection, message deletion, and end sessions-in short, it provides everything to meet the basic needs of the user.



Note: The code text in list A through C can be obtained from the downloadable zip file.



This article explores some of its features by using Net::P op to develop a simple POP3 e-mail application. At first you need to run the following command at the Perl prompt to download and install the module (if you haven't yet):



perl> perl-mcpan-e "Install Net::P OP3"



Start creating the following Perl script (List A):



List A


#!/bin/perl
# import package
use Net::POP3;
# ask user for critical variables
print "Mail host: ";
$host = <STDIN>;
chomp($host);
print "";
print "Mailbox username: ";
$user = <STDIN>;
chomp($user);
print "";
print "Mailbox password: ";
$pass = <STDIN>;
chomp($pass);
# initiate connection
# default timeout = 120 sec
$conn = Net::POP3->new($host) or die("ERROR: Unable to connect.");
# login
$numMsg = $conn->login($user, $pass) or die("ERROR: Unable to login.");
# display number of messages
if ($numMsg > 0) {
  print "Mailbox has $numMsg message(s).";
} else {
  print "Mailbox is empty.";
}
# close connection
$conn->quit();



This snippet will require the user to enter three content: the host name of the e-mail server, the POP3 username, and the corresponding password. Once you have entered these three items, a new net::P OP3 object is created, and the object's login () method is used to open a connection to the host and verify the credentials provided. If the credentials provided are approved by the server, the login () method returns the number of messages in the mailbox (or returns 0 if no news is available).




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.