When using email addresses for digital marketing, it’s crucial to ensure their validity. If you don’t verify the email addresses you collect, your marketing campaigns might fail. Email remains a popular communication tool, so validating addresses is essential for better business results.
This guide will show you how to validate email addresses in PHP. You can use these methods in PHP-based web applications that handle email addresses from forms or other sources. Also Check If Email is Valid Reddit
Before starting, make sure you have a development environment with PHP8. You should also have an API testing tool like Postman or cURL to test the code.
Using PHP’s Built-In Functions
- Create a PHP File: Open your IDE and create a file named `email.php`.
- Define a Class: Define an `EmailValidation` class with a public property `$email` to store the email address. The email is passed via a POST API call.
- Add Methods:
– `getRequest()`: Sends an API request.
– `withPHPFilter()`: Uses `filter_var()` to validate the email format.
Here’s the complete code for the `EmailValidation` class:
“`php
class EmailValidation {
public $email;
public function getRequest() {
$this->email = $_POST[’email’];
$this->withPHPFilter();
private function withPHPFilter() {
if (filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
echo “Valid email”;
} else {
echo “Invalid email”;
}
$validation = new EmailValidation();
$validation->getRequest();
“`
Run this PHP code from a command line terminal to start a web server. The URL will be `http://127.0.0.1:8000/email.php`. Use Postman to test the API with an email address.
Using Regular Expressions
While `filter_var()` checks the standard email format, regular expressions (regex) allow for more custom rules. PHP’s `preg_match()` function can help with this.
Default PHP Email Regex Validation
Modify the `EmailValidation` class to include a regex-based validation:
“`php
private function withRegEx() {
$pattern = “/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/”;
if (preg_match($pattern, $this->email)) {
echo “Valid email”;
} else {
echo “Invalid email”;
}
}
“`
Custom PHP Email Regex Validation
For custom rules, like excluding underscores, adjust the regex pattern:
“`php
$pattern = “/^[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/”;
“`
Update the `getRequest()` method to call `withRegEx()` instead of `withPHPFilter()` and test it.
Validating with an API
For more advanced validation, use an API like Abstract’s Email Validation API. This API checks not just the format but also the deliverability, MX records, and whether the email is temporary.
- Sign Up: Create an Abstract account to get an API key.
- Update Class: Add a method to call the Abstract API.
“`php
private function withAbstract() {
$apiKey = ‘your_api_key_here’;
$url = “https://emailvalidation.abstractapi.com/v1/?api_key=$apiKey&email=” . $this->email;
$response = file_get_contents($url);
$result = json_decode($response);
if ($result->deliverability == “DELIVERABLE”) {
echo “Valid email”;
} else {
echo “Invalid email”;
}
}
“`
Update `getRequest()` to call `withAbstract()`.
Combining Methods
For robust validation, combine regex and API methods. Define another method to first use regex and then the Abstract API for comprehensive validation.
“`php
private function combinedValidation() {
if ($this->withRegEx()) {
$this->withAbstract();
} else {
echo “Invalid email”;
}
}
“`