WordPress
Email checker for WordPress. Verify emails in contact forms, WooCommerce, and user registration.
向您的 WordPress 網站新增即時電子郵件驗證。驗證聯絡表單、註冊和 WooCommerce 結帳中的電子郵件。
整合方法
| 方法 | 最適合 | 複雜度 |
|---|---|---|
| 外掛程式 | 快速設定 | 低 |
| Contact Form 7 | CF7 使用者 | 低 |
| WPForms | WPForms 使用者 | 低 |
| 自訂 PHP | 完全控制 | 中 |
方法 1:EmailVerify 外掛程式
安裝我們的官方 WordPress 外掛程式以獲得最簡單的整合。
安裝
- 進入 Plugins → Add New
- 搜尋「EmailVerify Email Verification」
- 點擊 Install Now,然後點擊 Activate
- 進入 Settings → EmailVerify
- 輸入您的 API 金鑰
設定
// wp-config.php(選用)
define('EMAILVERIFY_API_KEY', 'bv_live_xxx');功能
- 在所有表單上進行即時驗證
- 阻止一次性電子郵件
- 阻止角色型電子郵件
- 可自訂的錯誤訊息
- WooCommerce 整合
- AJAX 驗證
方法 2:Contact Form 7
向 Contact Form 7 表單新增驗證。
使用掛鉤
// functions.php
add_filter('wpcf7_validate_email*', 'emailverify_cf7_validation', 20, 2);
function emailverify_cf7_validation($result, $tag) {
$email = isset($_POST[$tag->name]) ? sanitize_email($_POST[$tag->name]) : '';
if (empty($email)) {
return $result;
}
$verification = emailverify_check_email($email);
if ($verification['status'] === 'invalid') {
$result->invalidate($tag, 'Please enter a valid email address.');
}
if ($verification['result']['disposable']) {
$result->invalidate($tag, 'Disposable emails are not allowed.');
}
return $result;
}
function emailverify_check_email($email) {
$api_key = defined('EMAILVERIFY_API_KEY')
? EMAILVERIFY_API_KEY
: get_option('emailverify_api_key');
$response = wp_remote_post('https://api.emailverify.ai/v1/verify', [
'headers' => [
'Authorization' => 'Bearer ' . $api_key,
'Content-Type' => 'application/json',
],
'body' => json_encode(['email' => $email]),
'timeout' => 10,
]);
if (is_wp_error($response)) {
return ['status' => 'unknown'];
}
return json_decode(wp_remote_retrieve_body($response), true);
}表單標籤設定
[email* your-email class:emailverify-email]方法 3:WPForms
使用自訂驗證與 WPForms 整合。
PHP 驗證
// functions.php
add_filter('wpforms_process_before_form_data', 'emailverify_wpforms_validation', 10, 2);
function emailverify_wpforms_validation($form_data, $entry) {
foreach ($entry['fields'] as $field_id => $value) {
$field = $form_data['fields'][$field_id] ?? null;
if ($field && $field['type'] === 'email' && !empty($value)) {
$verification = emailverify_check_email($value);
if ($verification['status'] === 'invalid') {
wpforms()->process->errors[$form_data['id']][$field_id] =
'Please enter a valid email address.';
}
if ($verification['result']['disposable'] ?? false) {
wpforms()->process->errors[$form_data['id']][$field_id] =
'Disposable emails are not allowed.';
}
}
}
return $form_data;
}方法 4:自訂 PHP 整合
為了完全控制,直接與我們的 API 整合。
協助類別
<?php
// includes/class-emailverify.php
class EmailVerify {
private $api_key;
private $api_url = 'https://api.emailverify.ai/v1';
public function __construct($api_key = null) {
$this->api_key = $api_key ?: get_option('emailverify_api_key');
}
public function verify($email) {
$response = wp_remote_post($this->api_url . '/verify', [
'headers' => [
'Authorization' => 'Bearer ' . $this->api_key,
'Content-Type' => 'application/json',
],
'body' => json_encode(['email' => $email]),
'timeout' => 10,
]);
if (is_wp_error($response)) {
return [
'success' => false,
'error' => $response->get_error_message(),
];
}
$body = json_decode(wp_remote_retrieve_body($response), true);
return [
'success' => true,
'data' => $body,
];
}
public function is_valid($email) {
$result = $this->verify($email);
return $result['success'] && $result['data']['status'] === 'valid';
}
public function is_disposable($email) {
$result = $this->verify($email);
return $result['success'] && ($result['data']['result']['disposable'] ?? false);
}
}使用方法
$bv = new EmailVerify();
// 基本驗證
$result = $bv->verify('user@example.com');
if ($result['data']['status'] === 'valid') {
// 電子郵件有效
}
// 快速檢查
if ($bv->is_valid('user@example.com')) {
// 處理有效電子郵件
}
if ($bv->is_disposable('user@example.com')) {
// 阻止一次性電子郵件
}WordPress 註冊
在使用者註冊期間驗證電子郵件。
// functions.php
add_filter('registration_errors', 'emailverify_registration_check', 10, 3);
function emailverify_registration_check($errors, $sanitized_user_login, $user_email) {
$bv = new EmailVerify();
$result = $bv->verify($user_email);
if (!$result['success']) {
return $errors; // 如果 API 失敗,允許註冊
}
$data = $result['data'];
if ($data['status'] === 'invalid') {
$errors->add('invalid_email',
'<strong>Error</strong>: Please enter a valid email address.');
}
if ($data['result']['disposable'] ?? false) {
$errors->add('disposable_email',
'<strong>Error</strong>: Temporary email addresses are not allowed.');
}
return $errors;
}AJAX 驗證
使用 JavaScript 新增即時驗證。
PHP 端點
// functions.php
add_action('wp_ajax_verify_email', 'emailverify_ajax_verify');
add_action('wp_ajax_nopriv_verify_email', 'emailverify_ajax_verify');
function emailverify_ajax_verify() {
check_ajax_referer('emailverify_nonce', 'nonce');
$email = sanitize_email($_POST['email'] ?? '');
if (empty($email)) {
wp_send_json_error(['message' => 'Email is required']);
}
$bv = new EmailVerify();
$result = $bv->verify($email);
if ($result['success']) {
wp_send_json_success($result['data']);
} else {
wp_send_json_error(['message' => 'Verification failed']);
}
}
// 加入指令碼
add_action('wp_enqueue_scripts', 'emailverify_enqueue_scripts');
function emailverify_enqueue_scripts() {
wp_enqueue_script('emailverify',
get_template_directory_uri() . '/js/emailverify.js',
['jquery'],
'1.0.0',
true
);
wp_localize_script('emailverify', 'emailverify_ajax', [
'url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('emailverify_nonce'),
]);
}JavaScript
// js/emailverify.js
jQuery(function($) {
$('input[type="email"]').on('blur', function() {
var $input = $(this);
var email = $input.val();
if (!email) return;
$input.addClass('verifying');
$.post(emailverify_ajax.url, {
action: 'verify_email',
nonce: emailverify_ajax.nonce,
email: email
})
.done(function(response) {
$input.removeClass('verifying');
if (response.success) {
var data = response.data;
if (data.status === 'invalid') {
showError($input, 'Please enter a valid email');
} else if (data.result && data.result.disposable) {
showError($input, 'Disposable emails not allowed');
} else {
showSuccess($input);
}
}
})
.fail(function() {
$input.removeClass('verifying');
});
});
function showError($input, message) {
$input.addClass('error').removeClass('valid');
$input.next('.bv-message').remove();
$input.after('<span class="bv-message error">' + message + '</span>');
}
function showSuccess($input) {
$input.addClass('valid').removeClass('error');
$input.next('.bv-message').remove();
$input.after('<span class="bv-message valid">✓</span>');
}
});CSS
/* style.css */
input[type="email"].verifying {
background-image: url('spinner.gif');
background-position: right 10px center;
background-repeat: no-repeat;
}
input[type="email"].error {
border-color: #dc3545;
}
input[type="email"].valid {
border-color: #28a745;
}
.bv-message {
display: block;
font-size: 12px;
margin-top: 4px;
}
.bv-message.error {
color: #dc3545;
}
.bv-message.valid {
color: #28a745;
}快取
快取驗證結果以減少 API 呼叫。
function emailverify_check_email_cached($email) {
$cache_key = 'bv_email_' . md5($email);
$cached = get_transient($cache_key);
if ($cached !== false) {
return $cached;
}
$result = emailverify_check_email($email);
// 快取 24 小時
set_transient($cache_key, $result, DAY_IN_SECONDS);
return $result;
}最佳實務
1. 正常降級
如果 API 失敗,始終允許表單提交:
$result = $bv->verify($email);
if (!$result['success']) {
// 記錄錯誤但不要阻止使用者
error_log('EmailVerify API error: ' . $result['error']);
return; // 允許提交
}2. 速率限制
使用速率限制防止濫用:
function emailverify_rate_limit($email) {
$ip = $_SERVER['REMOTE_ADDR'];
$key = 'bv_rate_' . md5($ip);
$count = get_transient($key) ?: 0;
if ($count >= 10) { // 每分鐘 10 次驗證
return false;
}
set_transient($key, $count + 1, MINUTE_IN_SECONDS);
return true;
}3. 安全性
始終清理和驗證輸入:
$email = sanitize_email($_POST['email']);
if (!is_email($email)) {
// 無效格式,無需呼叫 API
return;
}