Guest User

Untitled

a guest
Dec 19th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * Export sent email addresses to a CSV file
  5. */
  6.  
  7. class Export_Sent_Emails_Addresses{
  8.  
  9. private $username, $pass, $imap_path;
  10.  
  11. function __construct( $username, $pass, $imap_path ){
  12. $this->username = $username;
  13. $this->pass = $pass;
  14. $this->imap_path = $imap_path;
  15. $this->run();
  16. }
  17.  
  18. protected function run(){
  19.  
  20. set_time_limit(4000);
  21.  
  22. $mbox = imap_open($this->imap_path, $this->username, $this->pass);
  23. $emails = imap_search($mbox,'ALL');
  24.  
  25. if($emails) {
  26.  
  27. rsort($emails);
  28.  
  29. $email_dirs = array();
  30. foreach( $emails as $email_number ){
  31. $overview = imap_fetch_overview( $mbox, $email_number, 0 );
  32. $email_dirs[] = $overview[0]->to;
  33. }
  34.  
  35. $email_dirs = array_unique( $email_dirs );
  36.  
  37. $this->export_to_csv( $email_dirs );
  38.  
  39. }
  40.  
  41. imap_close($mbox);
  42.  
  43. }
  44.  
  45. private function export_to_csv( $email_dirs ){
  46.  
  47. $fileName = 'emails'.time().'.csv';
  48.  
  49. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  50. header('Content-Description: File Transfer');
  51. header("Content-type: text/csv");
  52. header("Content-Disposition: attachment; filename={$fileName}");
  53. header("Expires: 0");
  54. header("Pragma: public");
  55.  
  56. $fh = @fopen( 'php://output', 'w' );
  57. fputcsv($fh, $email_dirs);
  58. fclose($fh);
  59. exit;
  60.  
  61. }
  62.  
  63. }
  64.  
  65. $exporter = new Export_Sent_Emails_Addresses(
  66. 'test@test.com',
  67. 'passwordhere',
  68. '{mail.test.com:143/novalidate-cert}Sent'
  69. );
  70. $exporter->run();
  71.  
  72. ?>
Add Comment
Please, Sign In to add comment