$activity = $wpdb->get_results("SELECT ID, user_email FROM {$wpdb->prefix}wc_crm_log ");
if ($activity) {
    foreach ($activity as $act) {
        $emails = explode(',', $act->user_email);
        $log_id = $act->ID;
        if (!empty($emails)) {
            foreach ($emails as $key => $email) {
                if (empty($email)) {
                    continue;
                }
                $customer = $wpdb->get_row("SELECT c_id, user_id FROM {$wpdb->prefix}wc_crm_customer_list WHERE email = '{$email}' LIMIT 1");
                if ($customer) {
                    if ($customer->user_id > 0) {
                        add_user_meta($customer->user_id, 'wc_crm_log_id', $log_id);
                    } else {
                        wc_crm_add_cmeta($customer->c_id, 'wc_crm_log_id', $log_id);
                    }
                }
            }
        }
    }
}
$groups = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wc_crm_groups_relationships ");
if ($groups) {
    foreach ($groups as $group) {
        $email = $group->customer_email;
        $group_id = $group->group_id;
        $ID = $group->ID;
        if (empty($email)) {
            continue;
        }
 /**
  * Processes the form data.
  */
 public static function process_email_form()
 {
     global $wpdb;
     wc_crm_clear_notices();
     $recipients = explode(',', $_POST['recipients']);
     $text = wpautop($_POST['emaileditor']);
     $subject = $_POST['subject'];
     if (!empty($_POST['from_email']) && filter_var($_POST['from_email'], FILTER_VALIDATE_EMAIL)) {
         add_filter('wp_mail_from', __CLASS__ . '::change_from_email', 9999);
     }
     if (!empty($_POST['from_name'])) {
         add_filter('wp_mail_from_name', __CLASS__ . '::change_from_name', 9999);
     }
     $mailer = WC()->mailer();
     ob_start();
     wc_crm_custom_woocommerce_get_template('emails/customer-send-email.php', array('email_heading' => $subject, 'email_message' => $text));
     $message = ob_get_clean();
     $order_ID = '';
     if (isset($_GET['order_id']) && $_GET['order_id'] != '') {
         $order_ID = $_GET['order_id'];
     }
     //save log
     $emails_ = $_POST['recipients'];
     $type = "email";
     $table_name = $wpdb->prefix . "wc_crm_log";
     $created = current_time('mysql');
     $created_gmt = get_gmt_from_date($created);
     $insert = $wpdb->prepare("(%s, %s, %s, %s, %s, %d)", $created, $created_gmt, $subject, $text, $type, get_current_user_id());
     $wpdb->query("INSERT INTO {$table_name} (created, created_gmt, subject, message, activity_type, user_id) VALUES " . $insert);
     $log_id = $wpdb->insert_id;
     foreach ($recipients as $r) {
         $mailer->send($r, stripslashes($subject), stripslashes($message));
         $result = $wpdb->get_results("SELECT c_id, user_id FROM {$wpdb->prefix}wc_crm_customer_list WHERE email = '{$r}' LIMIT 1");
         if ($result) {
             $customer = $result[0];
             if ($customer->user_id > 0) {
                 add_user_meta($customer->user_id, 'wc_crm_log_id', $log_id);
             } else {
                 wc_crm_add_cmeta($customer->c_id, 'wc_crm_log_id', $log_id);
             }
         }
     }
     wc_crm_add_notice(__("Email sent.", 'wc_crm'), 'success');
 }