Verify Email Addresses at Checkout & in FunnelKit Automations

Grant Ambrose

WagePirate Founder

Related Video: https://www.youtube.com/watch?v=6vO5_gWaisU

/**
 * Callback from FunnelKit Automation for Email Verification
 */
add_action('wage_validate_user_email_callback', 'wage_validate_user_email');

function wage_validate_user_email($args)
{
    // Check if the BWFCRM_Contact class exists at the beginning
    if (!class_exists('BWFCRM_Contact')) {
        //error_log("Was going to validate the user's email address via clearout.io, but class_exists('BWFCRM_Contact') not found.");
        return; // Exit if the class is not available
    }

    // Log the args for debugging
    //error_log(print_r($args, true)); // Log the contents of the $args array

    // Use the email from the args
    $email = $args['email'];
    $api_key = 'XXXXXXXXXXXX'; // Your Clearout API key
    $url = "https://api.clearout.io/v2/email_verify/instant"; // Clearout API endpoint

    // Prepare the request body
    $body = json_encode(['email' => $email]);

    // Set up the request arguments
    $response = wp_remote_post($url, [
        'headers' => [
            'Content-Type' => 'application/json',
            'Authorization' => $api_key // Use your API token directly without "Bearer"
        ],
        'body' => $body,
    ]);

    // Log the response
    if (is_wp_error($response)) {
        //error_log("Clearout API Error: " . $response->get_error_message());
        return; // Consider the email invalid if there's an error
    }

    // Decode the response body
    $body = json_decode(wp_remote_retrieve_body($response), true);

    // Log the response body
    //error_log("Clearout API Response: " . print_r($body, true));

    // Check the validation result
    if (isset($body['data']['status'])) {
        $crm_contact = new BWFCRM_Contact($email); // Passing Email as argument
        $field_slug = 'email-verification-status'; // Use the updated slug

        switch ($body['data']['status']) {
            case 'valid':
                // Email is valid, update the custom field to "Valid"
                $crm_contact->set_field_by_slug($field_slug, 'Valid');
                // error_log("Email is valid. Custom field '$field_slug' set to 'Valid' for contact: $email");
                break;

            case 'invalid':
                // Email is invalid, update the custom field to "Invalid"
                $crm_contact->set_field_by_slug($field_slug, 'Invalid');
                // error_log("Email is invalid. Custom field '$field_slug' set to 'Invalid' for contact: $email");
                break;

            case 'catch_all':
                // Email is catch-all, update the custom field to "Catch-all"
                $crm_contact->set_field_by_slug($field_slug, 'Catch-all');
                // error_log("Email is a catch-all. Custom field '$field_slug' set to 'Catch-all' for contact: $email");
                break;

            default:
                // Handle unknown status
                // error_log("Email verification returned unknown status for address: $email");
                break;
        }

        // Save the fields after setting them
        $crm_contact->save_fields();
    } else {
        // error_log("No status returned for email address: $email");
    }
}

Code language: PHP (php)
Grant Ambrose Avatar

Leave a Reply

Your email address will not be published. Required fields are marked *