Validating and verifying email addresses in PHP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
often we need a user to provide an email address for example when they sign up for an account fill in an order form and so on how do we make sure that the user has entered a valid email address using php one that's a valid format but also one that isn't fake let's start with this code here we have some simple html for the form we just saw in the browser we have a form with a label a text input and a submit button the form uses the post method and submits to the validate email.php script in that script let's start by getting the value of the input from the post array and the first thing we can do to validate this value is validate its format we can do this using the filter var function with the validate email filter this will validate a value to see if it's a valid email address this returns false if the filter fails in other words if the value isn't a valid email address so we'll call the filter var function passing in the value we want to validate and the id of the email validation filter if this returns boolean false we'll exit the script with a simple message obviously as part of a larger application at this point you do something like redisplay the form with a validation message but for the purposes of this example we'll just keep it simple if the email address is valid we'll output a message saying so let's give that a try if i enter a valid email address and submit the form we get the is valid message if i enter an invalid address we get the invalid format message all this does though is validate the format so most commonly a string an at sign and a domain this doesn't prevent the user from entering a fake email address though just to pass the validation so once we've validated the format how do we then check that it's not a fake address one way to do this is to use an email validation api there are many of these available as a quick search reveals most of these have a free tier for this example i'm going to use the abstract email validation api to use this you need to sign up for a free account to get an api key once you have the api key we'll use curl to make the api request we'll make the api request after we've validated the format so we're not wasting api requests on an email address that's not even in a valid format first let's create a variable containing the api key and obviously here you put your own api key value then we'll initialize a curl session and get a curl handle to set the various options we need we'll call the curl set opt array function passing in the curl handle followed by an array of options the endpoint url for this api is shown here in the documentation to this we add the api key and the email address we want to validate passing them in in the query string so let's copy this url then the first curl option we'll set is this url value to which will append the api key and the email address we'll also set the return transfer option to true to get the response from the api as a string instead of outputting it directly we also need to set the follow location option to true to follow any redirects that the api responds with then we'll execute the request getting the response into a variable and then we'll close the curl session the response is returned as json so we'll call the json decode method setting the second parameter to true to get the data as an associative array for now let's just print out this data and exit the script note for the purposes of this example we're keeping it simple and not checking for an invalid response from the api but normally you would do this to make sure you'd authenticated okay the response was valid and so on let's give that a try if i enter an email address and submit the form we get an array of data these data contain the results of the email validation for example an evaluation of the deliverability of the email based on factors like if the domain part of the email actually exists if it's an email address from a free service like gmail if it's a disposable email address and so on there are several different aspects of the email address reported by the api so instead of just printing this data out let's check some of these values first let's check to see if an email sent to this address would be delivered or not by checking the deliverability value if this is set to the string undeliverable then let's exit the script with a suitable message next let's check to see if the email address comes from a disposable email provider by checking the is disposable email value if this is true again we'll exit the script with a message depending on what type of email addresses you're willing to accept in your application you might want to allow disposable email addresses or conversely you might not allow free email providers it's up to you let's give that a try if i enter an email address that's a valid format but it's just a load of random letters then we get the message that this is undeliverable if someone is filling in a form and wants to bypass the validation of the email format this is the type of address that they'll probably enter this also works however with addresses that use a domain that exists as with example.com this is still undeliverable as this domain isn't set up to receive emails let's try this with a disposable email for example from mailinator this email address exists and can receive emails however when we submit the form this is recognized as such by the api and you might not want to allow addresses like this in your application finally let's try this with a non-disposable email address and we get the email address is valid message printed out so in addition to checking the format of an email address you can use an external api like this to check other aspects of the email address to see if it's one that you really want having valid email addresses will avoid your emails being marked as spam all this code shown in the video is linked to in the description and if you'd like to see more videos like this please consider subscribing to my channel as always thank you for watching and i'll see you in the next video
Info
Channel: Dave Hollingworth
Views: 25,585
Rating: undefined out of 5
Keywords:
Id: JvGFlAK2fg4
Channel Id: undefined
Length: 10min 41sec (641 seconds)
Published: Mon Mar 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.