PHP Function for Sending HTTP Post Request and Extracting Emails

MadMAX777

MadMAX777

Active Member
Joined
October 8, 2025
Messages
51
Reaction score
35
Points
18
The following PHP function can be used to send an HTTP post request and extract emails from an unstructured string. It also includes functions for validating an email address and a domain name.

Code:
<?php
function sendHttpRequest($url, $data) {
// Create the context
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($data)
)
));

// Send the request
return file_get_contents($url, false, $context);
}

function extractEmails($string) {
$emails = array();
preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);
if (isset($matches[0])) {
foreach ($matches[0] as $email) {
if (validateEmail($email)) {
$emails[] = $email;
}
}
}
return $emails;
}

function validateEmail($email) {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Split the email address into the username and domain
list($username, $domain) = explode('@', $email);
// Validate the domain
if (validateDomain($domain)) {
return true;
}
}
return false;
}

function validateDomain($domain) {
if (checkdnsrr($domain, 'MX')) {
return true;
}
return false;
}

?>

That's it! Now you can use these functions to send HTTP post requests, extract emails from unstructured strings, validate email addresses, and validate domain names.
1f913.png
 
  • Tags
    email extraction http post php programming web development
  • Top