Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Gets a list of email addresses that bounces via Amazon SES
- */
- function getBounces() {
- $this->ImapProcess( 'Amazon/Bounces', 'Amazon/Bounces-processed', 'Bounced');
- }
- /**
- * Gets a list of email addresses that received Complaints via Amazon SES
- */
- function getComplaints() {
- $this->ImapProcess( 'Amazon/Complaints', 'Amazon/C-processed', 'Complained');
- }
- /**
- * Does tha main job of picking bounces from GMail box,
- */
- function ImapProcess( $from = 'Amazon/Bounces', $to = 'Amazon/Bounces-processed', $comment = 'bounced') {
- $hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}'.$from;
- $username = 'no-reply@domain.com';
- $password = '*******';
- $limit = 200;
- $timeStart = time();
- /* try to connect */
- $inbox = imap_open( $hostname, $username, $password) or die("--Cannot connect to Gmail: " . print_r(imap_errors())."\n");
- /* grab emails */
- $emails = imap_search($inbox,'ALL');
- /* if emails are returned, cycle through each... */
- if($emails) {
- echo '<p>'.count($emails).' bounces found</p>';
- rsort($emails); // newest first
- // Initialize vars
- $output = '';
- $k=0;
- $found = array();
- $toMove = array();
- foreach($emails as $email_number) {
- $k++;
- if( $k>$limit) break;
- $header = explode("\n", imap_fetchheader($inbox, $email_number));
- // browse array for additional headers
- if (is_array($header) && count($header)) {
- $head = array();
- foreach($header as $line) {
- // is line with additional header?
- if (preg_match('%^X-%', $line)) {
- // separate name and value
- preg_match('%^([^:]*): (.*)%', $line, $arg);
- $head[$arg[1]] = $arg[2];
- }
- }
- }
- $overview = imap_fetch_overview( $inbox, $email_number, 0);
- $message = imap_fetchbody( $inbox, $email_number, 2);
- self::_getEmails( $message, $found);
- $toMove[] = $email_number;
- }
- echo $output;
- // Move processed
- if( !imap_mail_move( $inbox, implode(',', $toMove), $to)) {
- echo 'IMAP error: '.imap_last_error().'<br />'.PHP_EOL;
- } else {
- imap_expunge( $inbox);
- echo count($toMove).' messages moved to '.$to.'<br />'.PHP_EOL;
- }
- // Output interim results
- echo count( $found)." unique emails:<br />\n";
- echo '<textarea rows="5" cols="80">'.PHP_EOL.implode(' ', $found).PHP_EOL.'</textarea>'.PHP_EOL;
- // Unsubscribe the guys
- require_once( JPATH_COMPONENT_SITE.DS.'helpers'.DS.'mail.php');
- $n = OffersHelperMail::unsubscribe( $found, $comment.' '.date('Y-m-d H:i:s'), true);
- echo "<p>$n unsubscribed</p>";
- echo "<p>Limit: $limit.<br />Time elapsed: ".(time()-$timeStart).'s. </p>'.PHP_EOL;
- imap_close($inbox);
- } else {
- echo '<p>No emails in '.$from.'</p>';
- imap_close($inbox);
- jexit(-1);
- }
- }
- /**
- * Gets a message text and extracts all email addresses seen in it.
- *
- * There are certain exceptions: self and service email addresses are filtered out.
- *
- * @param string $subject Text to look for emails in.
- * @param array &$matches Only new unique emails are added into this array.
- *
- * @return Integer Numberof unique emails added.
- *
- */
- private function _getEmails($subject, &$matches) {
- $pattern = array();
- // valid e-mail regexp
- $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,}))/';
- // Following patters are to exclude from emails pool
- // 550-'vassia@mail.ru regexp
- $pattern['status'] = '/^\d+-\'.*$/';
- $found=array();
- $added = 0; // counter
- $m = preg_match_all($pattern['email'], $subject, $found);
- if( $m > 0) { // some number of matches found
- // Cycle through found emails, check each for being valid and unique to the result array.
- foreach($found[0] AS $e) {
- $e = strtolower($e);
- if(
- preg_match( $pattern['status'], $e)
- or
- substr( $e, strlen($e)-13) == 'amazonses.com' // complaints@email-abuse.amazonses.com
- or
- substr( $e, strlen($e)-10) == 'domain.com'
- or
- substr( $e, strlen($e)-15) == 'otherdomain.com'
- ) continue; // skip if matches
- // is it new to the result array?
- if( !in_array($e, $matches)) {
- $matches[] = $e;
- $added++;
- }
- }
- return $added;
- } else {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement