Arief Yudhawarman

Masih belajar buat blog

Get Email Header Using Perl

leave a comment »

Penulis membuat script perl untuk download beberapa email header melalui koneksi pop3. Header yang diambil adalah alamat pengirim, informasi tanggal, subject email, besar email dan message-id. Hasil eksekusi script akan disimpan secara default ke file mailheader.log.

Informasi tanggal dibaca dari header “Received from” yang dibuat oleh MUA (Mail User Agent). Jika header ini tidak ada maka ambil informasi tanggal dari header “Date“.

Untuk membaca mengenai apakah itu email header silahkan buka What is an Email Header?

#!/usr/bin/perl -w

# Name
#   getmh.pl
#
# Description
#   This script will get some the mail headers via pop3 connection, eg.
#   sender address, date, subject, size of message and message-id.
#   The output will be saved by default to the file mailheader.log
#
# Notes
#   The date information is fetched from mail header "Received from"
#   which is created by mail server not from header "Date:" which is 
#   create by MUA. In case of the header "Received from:" is not exist 
#   then get date from header "Date:" instead.
# 
# License
#   This program is copyleft. You have the right to freely use, modify,
#   copy, and share software, works of art, etc., on the condition that
#   these rights be granted to all subsequent users or owners. 
#
# Author
#   Arief Yudhawarman <awarmanff@yahoo.com>
#
# Revision
#   Thu, 27 March 2014
#

use Net::POP3;

#
# Subs
#

sub init()
{
  use Getopt::Std;
  my $opt_string = 'hu:p:s:f:';
  getopts( "$opt_string", \%opt ) or usage();
  usage() if $opt{h};

  if(!$opt{u} || !$opt{p} || !$opt{s})
  {
    usage();
  }
}

sub usage()
{
  print STDERR << "EOF";

Usage: $0 [-h] -u username -p password -s server [-f log]
  -h	  : this (help) message
  -u	  : username or email address
  -p	  : password
  -s      : mail server
  -f log  : file log
            if not set the log will be saved at mailheader.log

Example   : $0 -u arief -p 123456 -s mail.domain.com
            $0 -u arief -p 123456 -s mail.domain.com -f arief.header.log

EOF
  exit;
}

#
# main program
#

init();

$username = $opt{u};
$password = $opt{p};
$pop3host = $opt{s};
$log = $opt{f} || "mailheader.log";

open (OUT,">$log") || die "Can not open $log ; $!";

$pop = Net::POP3->new($pop3host, Timeout => 60)
  or die "Can not connect to $pop3host\n";

$time = localtime();
print "\n";
print "Fetching email $username at $pop3host, $time.\nLog will be saved at $log.\n";
print OUT "Fetching email $username at $pop3host, $time\n";

defined ($pop->login($username, $password))
    or die "Can't authenticate.\n";

$msgnums = $pop->list; # hashref of msgnum => size
$totmsgs =  keys (%$msgnums);
if ($totmsgs == 0) {
  print "No messages\n";
  print OUT "No messages\n";
  close (OUT);
  $pop->quit;
  exit;
};
for ($i = 1; $i <= $totmsgs; $i++) {
  $msgsize = $pop->list($i);
  $hdr = $pop->top($i);
  $j = 0;
  $from = "Nobody";
  $subject = "Nosubject";
  $str = '';
  $iter = 0;
  $received = 'y';
  while ($$hdr[$j]) {
    $from = $1 if ( ($$hdr[$j]=~/^Return-Path: <(.*)>/) || ($$hdr[$j]=~/^Return-Path: (.*)/));
    $dateHeader = $1 if ($$hdr[$j] =~ /^Date: (.*)/i);

    # if the line contain "Received: from" at the first sight ($received = 'y')
    # then start counting lines up to 3
    $iter++ if ($$hdr[$j] =~ /^Received: from /);
    if ((($iter > 0) && ($iter<4)) && ($received eq 'y'))
    {
      chomp $$hdr[$j];
      $str = $str.$$hdr[$j];
      $iter++;
      # when got 3 lines then set $received = 'n'
      $received = 'n' if ( $iter > 3);
    }
    # get "msgid" from header "Message-Id:"
    $msgid = $1 if ( $$hdr[$j]=~/^Message-ID: (.*)/i );
    $subject = $1 if ($$hdr[$j] =~ /^Subject: (.*)/i);
    $j++;
  }
  # check the $str
  if ($str ne '') {
    # get the date at second array
    # eg Tue, 25 Oct 2011 09:53:20 +0700 (WIT)
    @fields = split(/;/,$str);
    $fields[1] =~ s/[\t\r\n]//g;
    $date = $fields[1];
  } else {
    $date = $dateHeader;
  }
  print OUT "$i. From: $from; Date: $date; Subject: $subject; $msgsize bytes; Message-ID: $msgid\n"; 
} 
close (OUT);
$pop->quit;
print "\n";

Simpan script dengan nama getmh.pl. Kemudian eksekusi script tersebut.

$ perl getmh.pl

Usage: getmh.pl [-h] -u username -p password -s server [-f log]
  -h	  : this (help) message
  -u	  : username or email address
  -p	  : password
  -s      : mail server
  -f log  : file log
            if not set the log will be saved at mailheader.log

Example   : getmh.pl -u arief -p 123456 -s mail.domain.com
            getmh.pl -u arief -p 123456 -s mail.domain.com -f arief.header.log


Contoh isi file header.

Fetching email xxxxx at pop3.telkom.net, Thu Mar 27 16:46:04 2014
1. From: tm86-bounces@itb.ac.id; Date:  Mon, 25 Dec 2006 20:21:33 +0700; Subject: [TM86] Selamat Hari Natal dan Tahun Baru 2007; 3035 bytes; Message-ID: 
2. From: tm86-bounces@itb.ac.id; Date:  Tue, 26 Dec 2006 08:46:50 +0700; Subject: [TM86] Met Natal & Tahun baru; 2577 bytes; Message-ID: 
3. From: Edij@xxxx.com; Date:  Tue, 26 Dec 2006 09:13:27 +0700; Subject: RE: [TM86] Selamat Hari Natal dan Tahun Baru 2007; 1964 bytes; Message-ID: 
4. From: tm86-bounces@itb.ac.id; Date:  Thu, 04 Jan 2007 15:02:40 +0700; Subject: Re: [TM86] Iklan Titipan; 10222 bytes; Message-ID: 
5. From: tm86-bounces@itb.ac.id; Date:  Thu, 04 Jan 2007 15:59:18 +0700; Subject: Re: [TM86] TPA (Re:  Iklan Titipan); 14798 bytes; Message-ID: 
6. From: tm86-bounces@itb.ac.id; Date:  Thu, 04 Jan 2007 21:46:46 +0700; Subject: [TM86] TPA (Re:  Iklan Titipan); 13105 bytes; Message-ID: 
7. From: tm86-bounces@itb.ac.id; Date:  Thu, 04 Jan 2007 21:46:49 +0700; Subject: Re: [TM86] Iklan Titipan; 11441 bytes; Message-ID: 
8. From: tm86-bounces@itb.ac.id; Date:  Thu, 04 Jan 2007 22:36:59 +0700; Subject: Re: [TM86] Iklan Titipan; 7406 bytes; Message-ID: 
9. From: tm86-bounces@itb.ac.id; Date:  Fri, 05 Jan 2007 15:16:17 +0700; Subject: Re: [TM86] TPA (Re:  Iklan Titipan); 7554 bytes; Message-ID: 



Last update: 2014-04-03 20:00 +07:00

Written by awarmanf

April 2, 2014 at 9:59 am

Posted in Linux, mail, perl, pop3

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

%d bloggers like this: