A perl script to retrieve all the mails from your gmail account

I just wrote a code to extract all the mails/messages from our gmail account into a text file.
Inoder to run this perl script you have to install POP3Client library. You can accomplish that using this command

$ perl -MCPAN -e "Mail::POP3Client"

Here is the code. You have to only change the username and password in this code. Then execute the code.

mail.pl

#!/bin/perl
use Mail::POP3Client;
use IO::Socket::SSL;
use CGI qw(:standard);
$cgi = new CGI;

$username = 'YOUR-EMAIL-ID-HERE';
$password = 'YOUR-PASSWORD_HERE';

$mailhost = 'pop.gmail.com';
$port = '995';

print $cgi->header();

$pop = Mail::POP3Client->new(USER=> $username,
PASSWORD => $password,
HOST => $mailhost,
PORT => $port,
USESSL => 'true',
DEBUG => 0,) or die("ERROR: Unable to connect to mail server.\n");

my $count = $pop->Count();

if (($pop->Count()) < 1)
{
print "No messages...\n";
exit;
}

print "There are $count Messages in your inbox\n";

for($i = 1; $i Count(); $i++)
{
print "\n\n\nMail number $1\n";
foreach($pop->Head($i))
{
/^(From|Subject|Email):\s+/i && print $_, "\n";
}
print 'Message: '.$pop->Body($i);
}

$pop->Close();

exit;

Save the perl script and then execute this command to store the mails to a textfile

$ perl mail.pl > atextfile

Have fun :)

Advertisements

4 comments

  1. Hey i’m getting this error in the above programm,can u please help me

    Bareword found where operator expected at test.pl line 31, near “$i Count”
    (Missing operator before Count?)
    syntax error at test.pl line 31, near “$i Count”
    syntax error at test.pl line 32, near “++)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s