Engine23
blogBannerImage
By Engine23

Extract email address from a mail log file

Here is an example on how I extracted email address from a mail log file: $subject = "1 Aug 5 13:26:20 269701-web1 postfix/smtp[20033]: E730B10582C3: to=, relay=gmail-smtp-in.l.google.com[74.125.91.27]:25, delay=1.4, delays=0.02/0.01/0.13/1.3, dsn=2.0.0, status=sent (250 2.0.0 OK 1312568780 fp10si6690298qab.63) 1 Aug 5 13:30:49 269701-web1 postfix/smtp[20753]: F06EF10582C3: to= , relay=mx4.hotmail.com[65.55.92.136]:25, delay=0.28, delays=0.02/0/0.1/0.15, dsn=2.0.0, status=sent (250 <20110805183048.F06EF10582C3@269701-web1.www.bloom.com> Queued mail for delivery) 1 Aug 5 13:34:13 269701-web1 postfix/smtp[20822]: 5121410582C3: to=, relay=mx1.labcorp.iphmx.com[68.232.129.161]:25, delay=0.91, delays=0.01/0/0.42/0.47, dsn=2.0.0, status=sent (250 ok: Message 21101182 accepted) 1 Aug 5 13:34:14 269701-web1 postfix/smtp[20822]: 75C8610582C3: to=, relay=mx2.labcorp.iphmx.com[68.232.129.150]:25, delay=1, delays=0.02/0/0.49/0.54, dsn=2.0.0, status=sent (250 ok: Message 22975937 accepted) 1 Aug 5 13:44:53 269701-web1 postfix/smtp[21625]: 84B5B10582C3: to=, relay=gmail-smtp-in.l.google.com[74.125.91.27]:25, delay=1.7, delays=0.01/0/0.27/1.4, dsn=2.0.0, status=sent (250 2.0.0 OK 1312569893 ef1si6693357qab.35) ";

function extract_emails_from($string){
  preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);
  return $matches[0];
}

$emails = extract_emails_from($subject); $mail = array_unique($emails);

foreach($mail as $list)
{
    if(strlen($list) <= 28 )
    {
        echo  $list.'
';
    }

}