Skip to content

Instantly share code, notes, and snippets.

@titodevera
Created December 19, 2018 09:09
Show Gist options
  • Save titodevera/d7b27430da9759ead1739688a320ed24 to your computer and use it in GitHub Desktop.
Save titodevera/d7b27430da9759ead1739688a320ed24 to your computer and use it in GitHub Desktop.
Export sent email addresses to a CSV file
<?php
/*
* Export sent email addresses to a CSV file
*/
class Export_Sent_Emails_Addresses{
private $username, $pass, $imap_path;
function __construct( $username, $pass, $imap_path ){
$this->username = $username;
$this->pass = $pass;
$this->imap_path = $imap_path;
$this->run();
}
protected function run(){
set_time_limit(4000);
$mbox = imap_open($this->imap_path, $this->username, $this->pass);
$emails = imap_search($mbox,'ALL');
if($emails) {
rsort($emails);
$email_dirs = array();
foreach( $emails as $email_number ){
$overview = imap_fetch_overview( $mbox, $email_number, 0 );
$email_dirs[] = $overview[0]->to;
}
$email_dirs = array_unique( $email_dirs );
$this->export_to_csv( $email_dirs );
}
imap_close($mbox);
}
private function export_to_csv( $email_dirs ){
$fileName = 'emails'.time().'.csv';
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$fileName}");
header("Expires: 0");
header("Pragma: public");
$fh = @fopen( 'php://output', 'w' );
fputcsv($fh, $email_dirs);
fclose($fh);
exit;
}
}
$exporter = new Export_Sent_Emails_Addresses(
'test@test.com',
'passwordhere',
'{mail.test.com:143/novalidate-cert}Sent'
);
$exporter->run();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment