echo '<tr valign="top">';
     echo '<td colspan="2">';
     if (!$bounce_connection) {
         // connection error
         echo '<div class="text-alert"><p><img src="' . ALO_EM_PLUGIN_URL . '/images/12-exclamation.png" /> ';
         echo '<strong>' . @imap_last_error() . '.</strong><br />';
         echo '</p></div>';
     } else {
         echo '<div class="easymail-alert" style="background-color:#99FF66">';
         echo '<img src="' . ALO_EM_PLUGIN_URL . '/images/yes.png" style="vertical-align: text-bottom;" /> ';
         echo __('The connection test has been successfully completed', 'alo-easymail') . '</div>';
     }
     echo '</td></tr>';
 } else {
     if (isset($_POST['check_bounces_now'])) {
         $bounce_connection = alo_em_bounce_connect();
         echo '<tr valign="top">';
         echo '<td colspan="2">';
         if (!$bounce_connection) {
             // connection error
             echo '<div class="text-alert"><p><img src="' . ALO_EM_PLUGIN_URL . '/images/12-exclamation.png" /> ';
             echo '<strong>' . @imap_last_error() . '.</strong><br />';
             echo '</p></div>';
         } else {
             echo '<div class="easymail-alert" style="background-color:#99FF66">';
             echo '<img src="' . ALO_EM_PLUGIN_URL . '/images/yes.png" style="vertical-align: text-bottom;" /> ';
             echo '<strong>' . __('The bounces has been successfully handled', 'alo-easymail') . '</strong><br />';
             // Manually check bounce now!
             $bounce_report = alo_em_handle_bounces(true);
             echo '<div style="overflow: auto;max-height: 150px">' . $bounce_report . '</div>';
             echo '</div>';
 function alo_em_handle_bounces($report = false)
 {
     global $wpdb;
     $output = '';
     $bounce_settings = alo_em_bounce_settings();
     $conn = alo_em_bounce_connect();
     if (!$conn) {
         return FALSE;
     }
     $num_msgs = imap_num_msg($conn);
     // start bounce class
     require_once 'inc/bouncehandler/bounce_driver.class.php';
     $bouncehandler = new Bouncehandler();
     // get the failures
     $email_addresses = array();
     $delete_addresses = array();
     $max_msgs = min($num_msgs, $bounce_settings['bounce_maxmsg']);
     if ($report) {
         $output .= 'Bounces handled in: ' . $bounce_settings['bounce_email'];
     }
     for ($n = 1; $n <= $max_msgs; $n++) {
         $msg_headers = imap_fetchheader($conn, $n);
         $msg_body = imap_body($conn, $n);
         $bounce = $msg_headers . $msg_body;
         //entire message
         $multiArray = $bouncehandler->get_the_facts($bounce);
         if (!empty($multiArray[0]['action']) && !empty($multiArray[0]['status']) && !empty($multiArray[0]['recipient'])) {
             if ($report) {
                 $output .= '<br /> - MSG #' . $n . ' - Bounce response: ' . $multiArray[0]['action'];
             }
             // If delivery permanently failed, unsubscribe
             if ($multiArray[0]['action'] == 'failed') {
                 $email = trim($multiArray[0]['recipient']);
                 // Unsubscribe email address
                 if ($s_id = alo_em_is_subscriber($email)) {
                     alo_em_delete_subscriber_by_id($s_id);
                     do_action('alo_easymail_bounce_email_unsubscribed', $email);
                     // Hook
                     if ($report) {
                         $output .= ' - ' . $email . ' UNSUBSCRIBED';
                     }
                 }
             }
             // If delivery temporary or permanently failed, mark recipient as bounced
             if ($multiArray[0]['action'] == 'failed' || $multiArray[0]['action'] == 'transient' || $multiArray[0]['action'] == 'autoreply') {
                 // TODO maybe use: $bouncehandler->x_header_search_1 = 'ALO-EM-Newsletter';
                 // Look fo EasyMail custom headers: Newsletter and Recipient
                 // NOTE: searching in body because IDs are inside original message included in body
                 $newsletter_id = 0;
                 $recipient_id = 0;
                 if (preg_match('/X-ALO-EM-Newsletter: (\\d+)/i', $bounce, $matches)) {
                     if (!empty($matches[1]) && is_numeric($matches[1])) {
                         $newsletter_id = (int) $matches[1];
                     }
                 }
                 if (preg_match('/X-ALO-EM-Recipient: (\\d+)/i', $bounce, $matches)) {
                     if (!empty($matches[1]) && is_numeric($matches[1])) {
                         $recipient_id = (int) $matches[1];
                     }
                 }
                 // Mark recipient as bounced only if not a debug to author
                 if ($newsletter_id > 0 && $recipient_id > 0 && strpos($msg_headers, "( DEBUG - TO: ") === false) {
                     $wpdb->update("{$wpdb->prefix}easymail_recipients", array('result' => -3), array('ID' => $recipient_id, 'newsletter' => $newsletter_id, 'email' => $email));
                 }
                 if ($report) {
                     $output .= ' - Recipient ID #' . $recipient_id . ' marked as not delivered';
                 }
                 // mark msg for deletion
                 imap_delete($conn, $n);
             }
         } else {
             if ($report) {
                 $output .= '<br /><span class="description"> - MSG #' . $n . ' - Not a bounce</span>';
             }
         }
     }
     //for loop
     // delete messages
     imap_expunge($conn);
     // close
     imap_close($conn);
     if ($report) {
         return $output;
     }
 }