Mailbeam
WordPress + PHPBeginner20 minutesUpdated January 2025

Email Verification in WordPress

This tutorial creates a small WordPress plugin that hooks into user registration (and optionally WooCommerce checkout) to verify email addresses via Mailbeam. No extra plugin dependencies required — it uses WordPress's built-in wp_remote_post.

Prerequisites

  • WordPress 6.0+
  • PHP 8.1+
  • WooCommerce (optional, for checkout hook)
  • A Mailbeam API key (sign up free)

Create the plugin file

Create /wp-content/plugins/mailbeam-verify/mailbeam-verify.php:

<?php
/**
 * Plugin Name: Mailbeam Email Verify
 * Description: Verify email addresses via Mailbeam on WordPress and WooCommerce signup.
 * Version: 1.0.0
 * Requires PHP: 8.1
 */

if (!defined('ABSPATH')) exit;

define('MAILBEAM_API_KEY', defined('MAILBEAM_KEY') ? MAILBEAM_KEY : '');
define('MAILBEAM_MIN_SCORE', 60);

/**
 * Verify an email address via Mailbeam.
 * Returns the API response array or null on error (fail open).
 * Uses WordPress transients for caching (1 hour TTL).
 */
function mailbeam_verify(string $email): ?array
{
    $cacheKey = 'mb_' . md5(strtolower(trim($email)));
    $cached = get_transient($cacheKey);
    if ($cached !== false) return $cached;

    $response = wp_remote_post('https://api.mailbeam.dev/v1/verify', [
        'timeout' => 5,
        'headers' => [
            'Authorization' => 'Bearer ' . MAILBEAM_API_KEY,
            'Content-Type'  => 'application/json',
        ],
        'body' => wp_json_encode(['email' => $email]),
    ]);

    if (is_wp_error($response)) {
        error_log('[Mailbeam] wp_remote_post error: ' . $response->get_error_message());
        return null; // fail open
    }

    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body, true);

    if (!is_array($data)) return null;

    set_transient($cacheKey, $data, HOUR_IN_SECONDS);
    return $data;
}

function mailbeam_get_error_message(?string $reason): string
{
    return match ($reason) {
        'disposable_domain' => __('Please use a permanent email address.', 'mailbeam-verify'),
        'no_mx_records'     => __('This email domain cannot receive mail.', 'mailbeam-verify'),
        'smtp_rejected'     => __('This email address does not appear to exist.', 'mailbeam-verify'),
        default             => __('Please enter a valid email address.', 'mailbeam-verify'),
    };
}

// ─── WordPress user registration ─────────────────────────────────────────────

add_filter('registration_errors', function (WP_Error $errors, string $sanitized_user_login, string $user_email) {
    if (!$user_email || $errors->has_errors()) return $errors;

    $result = mailbeam_verify($user_email);
    if ($result === null) return $errors; // fail open

    $valid = $result['valid'] ?? false;
    $score = $result['score'] ?? 0;
    $reason = $result['reason'] ?? null;

    if (!$valid || $score < MAILBEAM_MIN_SCORE) {
        $errors->add('mailbeam_invalid_email', mailbeam_get_error_message($reason));
    }

    return $errors;
}, 10, 3);

// ─── WooCommerce registration ─────────────────────────────────────────────────

add_action('woocommerce_register_post', function (string $username, string $email, WP_Error $errors) {
    if (!$email || $errors->has_errors()) return;

    $result = mailbeam_verify($email);
    if ($result === null) return; // fail open

    $valid = $result['valid'] ?? false;
    $score = $result['score'] ?? 0;
    $reason = $result['reason'] ?? null;

    if (!$valid || $score < MAILBEAM_MIN_SCORE) {
        $errors->add('mailbeam_invalid_email', mailbeam_get_error_message($reason));
    }
}, 10, 3);

// ─── WooCommerce checkout ─────────────────────────────────────────────────────

add_action('woocommerce_checkout_process', function () {
    $email = isset($_POST['billing_email']) ? sanitize_email($_POST['billing_email']) : '';
    if (!$email) return;

    $result = mailbeam_verify($email);
    if ($result === null) return; // fail open

    $valid = $result['valid'] ?? false;
    $score = $result['score'] ?? 0;
    $reason = $result['reason'] ?? null;

    if (!$valid || $score < MAILBEAM_MIN_SCORE) {
        wc_add_notice(mailbeam_get_error_message($reason), 'error');
    }
});

Configure the API key

Add to wp-config.php:

define('MAILBEAM_KEY', 'mb_live_xxxxxxxxxxxxxxxxxxxx');

Or use an environment variable via your hosting provider:

define('MAILBEAM_KEY', getenv('MAILBEAM_KEY') ?: '');

Activate the plugin

  1. Upload to /wp-content/plugins/mailbeam-verify/
  2. Go to Plugins in your WP Admin
  3. Activate Mailbeam Email Verify

Testing

Try registering with temp@mailinator.com at /wp-login.php?action=register — you should see the error message. Use user@valid.mailbeam-test.dev to test the success path without consuming quota.

Next steps