Skip to content

Instantly share code, notes, and snippets.

@GianpaMX
Created August 1, 2013 22:35
Show Gist options
  • Save GianpaMX/6135965 to your computer and use it in GitHub Desktop.
Save GianpaMX/6135965 to your computer and use it in GitHub Desktop.
PHP Script to extract all email addresses from a mail box over IMAP protocol Usage: ./imap-addressess-extractor.php INBOX 0 1000 first parameter is the mailbox name second is the offset third is the quantity message to scan
#!/usr/bin/php
<?php
class Contact {
public $name;
public $email;
public function __construct($name = "", $email = "") {
$this->name = $name;
$this->email = $email;
}
public function toArray() {
return array($this->name, $this->email);
}
}
$fp = fopen('file.csv', 'w');
$mailboxName = "INBOX";
if(isset($argv[1]) && $argv[1] != "list") {
$mailboxName = $argv[1];
}
$server = "server"
$username = "user";
$password = "password";
$port = 143
$mailboxPath = "{$server:$port/novalidate-cert/norsh/notls}$mailboxName";
$mbox = @imap_open($mailboxPath, $username, $password)
or die("can't connect: " . imap_last_error());
if(isset($argv[1]) && $argv[1] == "list") {
$list = imap_list($mbox, $mailboxPath, "*");
is_array($list) or die("imap_list failed: " . imap_last_error());
foreach ($list as $val) {
echo imap_utf7_decode($val) . "\n";
}
exit(0);
}
$offset = 0;
if(isset($argv[2]) && is_int($argv[2])) {
$offset = $argv[2];
}
$quantity = 0;
if(isset($argv[3]) && $argv[3] > 0) {
$quantity = $argv[3];
}
$percent = 0;
$numMessages = imap_num_msg($mbox);
for ($i = $numMessages - $offset; $i > ($numMessages - $offset - $quantity); $i--) {
$header = @imap_headerinfo($mbox, $i);
if(!$header instanceof StdClass) {
$retries = 0;
while($retries < 5) {
echo "Re-openning $retries\n";
if(imap_reopen($mbox, $mailboxPath)) {
$header = @imap_headerinfo($mbox, $i);
break;
}
$retries++;
}
if(!$header instanceof StdClass) continue;
}
if(isset($header->from[0])) {
$from = $header->from[0];
$contact = new Contact(isset($from->personal) ? $from->personal : "", $from->mailbox . "@" . $from->host);
fputcsv($fp, $contact->toArray());
}
if(isset($header->to)) {
$toContacts = $header->to;
foreach($toContacts as $to) {
$contact = new Contact(isset($to->personal) ? $to->personal : "", $to->mailbox . "@" . $to->host);
fputcsv($fp, $contact->toArray());
}
}
if(isset($header->cc)) {
$ccContacts = $header->cc;
foreach($ccContacts as $cc) {
$contact = new Contact(isset($cc->personal) ? $cc->personal : "", $cc->mailbox . "@" . $cc->host);
fputcsv($fp, $contact->toArray());
}
}
if( $percent != (int)((($numMessages - $offset - $i) / $quantity) * 100) && (($percent = (int)((($numMessages - $offset - $i) / $quantity) * 100)) % 1) == 0) echo "$percent% mails: " . ($numMessages - $i - $offset) . "\n";
}
echo "Mailbox: $mailboxName\n";
echo "Number of messages: $numMessages\n";
echo "Offset: $offset\n";
echo "Quantity: $quantity\n";
imap_close($mbox);
fclose($fp);
@GianpaMX
Copy link
Author

GianpaMX commented Aug 1, 2013

The you could use something like

$ cat file.csv | awk -F "\"*,\"*" '{print $2}' | sort | uniq

to extract unique addresses

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment