Examples in Perl

Retrieve Mail

You can use the Mail::POP3Client module to retrieve mail from a POP server.

#!/usr/bin/perl

  use Mail::POP3Client;

  $pop = new Mail::POP3Client( USER     => "username",
                               PASSWORD => "password",
                               HOST     => "pop.server.com" );

  $a = $pop->Count();             # Number of messages
  print $a;

  for ($i = 1; $i <= $pop->Count(); $i++) {
    foreach ( $pop->Head( $i ) ) {            # Print the header
       print "$_\n";
    }
    foreach ( $pop->Body( $i ) ) {            # Print the body
       print "$_\n";
    }
    print "\n";
  }

$pop->close();                                # close the connection

This displays the header and body of each email in your account and exits gracefully.
There methods available to delete messages, write the message to a file, etc.
Read the documentation at http://www.cpan.org

If you want to know how it works behind the scene, read on. The lines like this are the ones typed in by me and the patrs on those lines after # are comments to help you understand what's happening :

> telnet <POP server deleted> 110   # Connect to the POP server on port 110

+OK POP3 [cppop 7.8] at [<ip address deleted>]
USER <username deleted>   # enter the username
+OK Need a password
PASS <password deleted>   # enter the password
+OK You have 1 messages totaling 1546 octets from <directory path deleted>
/<content deleted>/inbox (full load)
LIST   # list the number of messages
+OK 1 messages (1546 octets) [0 control message(s) hidden]
1 1546
.
STAT   # Statistics - number of messages and the total current size
+OK 1 1546
RETR 1   # retrieve message number 1
+OK 1546 octets
Return-path: <<email address deleted>>
Envelope-to: <email address deleted>
Delivery-date: Sat, 14 Feb 2004 03:19:09 -0600
Received: from [<ip address deleted>] (helo=name)
        by <server address deleted> with smtp (Exim 4.24)
        id 1Arvx6-0006Yy-Rs
        for <email address deleted>; Sat, 14 Feb 2004 03:19:09 -0600
Message-ID: <<email address deleted>>
From: <name and email address deleted>
To: <email address deleted>
Subject: trial message
Date: Sat, 14 Feb 2004 14:47:28 +0530
MIME-Version: 1.0
Content-Type: multipart/alternative;
        boundary="----=_NextPart_000_0005_01C3F309.77B34B40"
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 5.00.2615.200
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200

This is a multi-part message in MIME format.

------=_NextPart_000_0005_01C3F309.77B34B40
Content-Type: text/plain;
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

This is the body of the trial message

------=_NextPart_000_0005_01C3F309.77B34B40
Content-Type: text/html;
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2614.3500" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>This is the body of the trial=20
message</FONT></DIV></BODY></HTML>

------=_NextPart_000_0005_01C3F309.77B34B40--



.
TOP 1 3   # retrieve the top 3 lines of message number 1
+OK 841 octets
Return-path: <<email address deleted>>
Envelope-to: <email address deleted>
Delivery-date: Sat, 14 Feb 2004 03:19:09 -0600
Received: from [<ip address deleted>] (helo=<content deleted>)
        by <server address deleted> with smtp (Exim 4.24)
        id 1Arvx6-0006Yy-Rs
        for <email address deleted>; Sat, 14 Feb 2004 03:19:09 -0600
Message-ID: <<email address deleted>>
From: <name and email address deleted>
To: <email address deleted>
Subject: trial message
Date: Sat, 14 Feb 2004 14:47:28 +0530
MIME-Version: 1.0
Content-Type: multipart/alternative;
        boundary="----=_NextPart_000_0005_01C3F309.77B34B40"
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 5.00.2615.200
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200

This is a multi-part message in MIME format.

------=_NextPart_000_0005_01C3F309.77B34B40

.
DELE 1   # mark message number 1 for deletion
+OK message 1 will be deleted when you logout
QUIT   # close the connection. Any messages marked for deletion will be deleted
+OK Bye!

Back to Table of Contents