sergiks

Parsing emails on GMail code excerpt

Jan 25th, 2014
251
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.     /**
  4.      * Gets a list of email addresses that bounces via Amazon SES
  5.      */
  6.     function getBounces() {
  7.         $this->ImapProcess( 'Amazon/Bounces', 'Amazon/Bounces-processed', 'Bounced');
  8.     }
  9.    
  10.     /**
  11.      * Gets a list of email addresses that received Complaints via Amazon SES
  12.      */
  13.     function getComplaints() {
  14.         $this->ImapProcess( 'Amazon/Complaints', 'Amazon/C-processed', 'Complained');
  15.     }
  16.    
  17.     /**
  18.      * Does tha main job of picking bounces from GMail box,
  19.      */
  20.     function ImapProcess( $from = 'Amazon/Bounces', $to = 'Amazon/Bounces-processed', $comment = 'bounced') {
  21.         $hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}'.$from;
  22.         $username = 'no-reply@domain.com';
  23.         $password = '*******';
  24.        
  25.         $limit = 200;
  26.         $timeStart = time();
  27.        
  28.         /* try to connect */
  29.         $inbox = imap_open( $hostname, $username, $password) or die("--Cannot connect to Gmail: " . print_r(imap_errors())."\n");
  30.        
  31.         /* grab emails */
  32.         $emails = imap_search($inbox,'ALL');
  33.        
  34.         /* if emails are returned, cycle through each... */
  35.         if($emails) {
  36.             echo '<p>'.count($emails).' bounces found</p>';
  37.             rsort($emails); // newest first
  38.             // Initialize vars
  39.             $output = '';
  40.             $k=0;
  41.             $found = array();
  42.             $toMove = array();
  43.        
  44.             foreach($emails as $email_number) {
  45.                 $k++;
  46.                 if( $k>$limit) break;
  47.                
  48.                 $header = explode("\n", imap_fetchheader($inbox, $email_number));
  49.                 // browse array for additional headers
  50.                 if (is_array($header) && count($header)) {
  51.                     $head = array();
  52.                     foreach($header as $line) {
  53.                         // is line with additional header?
  54.                         if (preg_match('%^X-%', $line)) {
  55.                             // separate name and value
  56.                             preg_match('%^([^:]*): (.*)%', $line, $arg);
  57.                             $head[$arg[1]] = $arg[2];
  58.                         }
  59.                     }
  60.                 }
  61.                 $overview = imap_fetch_overview( $inbox, $email_number, 0);
  62.                 $message = imap_fetchbody( $inbox, $email_number, 2);
  63.  
  64.                 self::_getEmails( $message, $found);
  65.                 $toMove[] = $email_number;
  66.             }
  67.            
  68.             echo $output;
  69.            
  70.             // Move processed
  71.             if( !imap_mail_move( $inbox, implode(',', $toMove), $to)) {
  72.                 echo 'IMAP error: '.imap_last_error().'<br />'.PHP_EOL;
  73.             } else {
  74.                 imap_expunge( $inbox);
  75.                 echo count($toMove).' messages moved to '.$to.'<br />'.PHP_EOL;
  76.             }
  77.            
  78.             // Output interim results
  79.             echo count( $found)." unique emails:<br />\n";
  80.             echo '<textarea rows="5" cols="80">'.PHP_EOL.implode(' ', $found).PHP_EOL.'</textarea>'.PHP_EOL;
  81.  
  82.             // Unsubscribe the guys
  83.             require_once( JPATH_COMPONENT_SITE.DS.'helpers'.DS.'mail.php');
  84.             $n = OffersHelperMail::unsubscribe( $found, $comment.' '.date('Y-m-d H:i:s'), true);
  85.             echo "<p>$n unsubscribed</p>";
  86.             echo "<p>Limit: $limit.<br />Time elapsed: ".(time()-$timeStart).'s. </p>'.PHP_EOL;
  87.             imap_close($inbox);
  88.         } else {
  89.             echo '<p>No emails in '.$from.'</p>';
  90.             imap_close($inbox);
  91.             jexit(-1);
  92.         }
  93.     }
  94.  
  95.     /**
  96.      * Gets a message text and extracts all email addresses seen in it.
  97.      *
  98.      * There are certain exceptions: self and service email addresses are filtered out.
  99.      *
  100.      * @param string $subject Text to look for emails in.
  101.      * @param array &$matches Only new unique emails are added into this array.
  102.      *
  103.      * @return Integer Numberof unique emails added.
  104.      *
  105.      */
  106.     private function _getEmails($subject, &$matches) {
  107.         $pattern = array();
  108.  
  109.         // valid e-mail regexp
  110.         $pattern['email'] = '/(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/';
  111.        
  112.         // Following patters are to exclude from emails pool
  113.         // 550-'vassia@mail.ru regexp
  114.         $pattern['status'] = '/^\d+-\'.*$/';
  115.        
  116.         $found=array();
  117.         $added = 0; // counter
  118.         $m = preg_match_all($pattern['email'], $subject, $found);
  119.         if( $m > 0) { // some number of matches found
  120.  
  121.             // Cycle through found emails, check each for being valid and unique to the result array.
  122.             foreach($found[0] AS $e) {
  123.                 $e = strtolower($e);
  124.                 if(
  125.                     preg_match( $pattern['status'], $e)
  126.                     or
  127.                     substr( $e, strlen($e)-13) == 'amazonses.com'   // complaints@email-abuse.amazonses.com
  128.                     or
  129.                     substr( $e, strlen($e)-10) == 'domain.com'
  130.                     or
  131.                     substr( $e, strlen($e)-15) == 'otherdomain.com'
  132.                 ) continue; // skip if matches
  133.                
  134.                 // is it new to the result array?
  135.                 if( !in_array($e, $matches)) {
  136.                     $matches[] = $e;
  137.                     $added++;
  138.                 }
  139.             }
  140.             return $added;
  141.         } else {
  142.             return 0;
  143.         }
  144.     }
RAW Paste Data