Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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();
- ?>
Add Comment
Please, Sign In to add comment