Advertisement
swte

Email checker

Oct 17th, 2023
1,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.41 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name: Email Checker
  4.  * Description: Checks the billing email address on order created and displays status in the admin order page.
  5.  * Version: 1.0
  6.  * Author: Your Name
  7.  */
  8.  
  9. // Check Email Existence
  10. function check_email_existence($email) {
  11.     list($user, $domain) = explode('@', $email);
  12.     $host = gethostbyname($domain);
  13.     $port = 25;
  14.     $timeout = 10;
  15.  
  16.     $socket = fsockopen($host, $port, $errno, $errstr, $timeout);
  17.     if (!$socket) {
  18.         return false;
  19.     }
  20.  
  21.     $response = fread($socket, 1024);
  22.     if (!preg_match('/^220/', $response)) return false;
  23.    
  24.     // EHLO request
  25.     fwrite($socket, "EHLO $host\r\n");
  26.     $response = fread($socket, 1024);
  27.     if (!preg_match('/^250/', $response)) return false;
  28.  
  29.     // MAIL FROM request
  30.     fwrite($socket, "MAIL FROM: <check@$host>\r\n");
  31.     $response = fread($socket, 1024);
  32.     if (!preg_match('/^250/', $response)) return false;
  33.  
  34.     // RCPT TO request
  35.     fwrite($socket, "RCPT TO: <$email>\r\n");
  36.     $response = fread($socket, 1024);
  37.     if (!preg_match('/^250/', $response)) return false;
  38.  
  39.     // QUIT request
  40.     fwrite($socket, "QUIT\r\n");
  41.     fclose($socket);
  42.  
  43.     return true;
  44. }
  45.  
  46. // Verify email on order creation and update post meta
  47. add_action('woocommerce_new_order', 'verify_email_on_order_created');
  48.  
  49. function verify_email_on_order_created($order_id) {
  50.     $order = wc_get_order($order_id);
  51.     $email = $order->get_billing_email();
  52.     $email_exists = check_email_existence($email);
  53.  
  54.     if (!$email_exists) {
  55.         update_post_meta($order_id, '_billing_email_invalid', 'yes');
  56.     } else {
  57.         update_post_meta($order_id, '_billing_email_valid', 'yes');
  58.     }
  59. }
  60.  
  61. // Add meta box to display email verification status in the admin order edit page
  62. add_action('add_meta_boxes', 'add_email_checker_meta_box');
  63.  
  64. function add_email_checker_meta_box() {
  65.     add_meta_box('email-checker-meta-box', 'Email Verification', 'email_checker_meta_box_content', 'shop_order', 'side', 'high');
  66. }
  67.  
  68. function email_checker_meta_box_content($post) {
  69.     $email_valid = get_post_meta($post->ID, '_billing_email_valid', true);
  70.     $email_invalid = get_post_meta($post->ID, '_billing_email_invalid', true);
  71.  
  72.     if ($email_valid) {
  73.         echo "Email is valid.";
  74.     } elseif ($email_invalid) {
  75.         echo "Email is invalid.";
  76.     } else {
  77.         echo "Email hasn't been checked yet.";
  78.     }
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement