PHP Code:
<?php
function domain_exists($email,$record = 'MX')
{
list($user,$domain) = split('@',$email);
return checkdnsrr($domain,$record);
}
function isValidEmail($address) {
return preg_match("/^[a-z0-9._\-]+[@]([a-z0-9\-]+[.])+([a-z]{2,4})\$/i",
$address);
}
// get posted data into local variables
$EmailFrom = "ryan.fitton@hotmail.co.uk";
$EmailTo = "ryan.fitton@hotmail.co.uk";
$Subject = "Email From Personal Website";
$YourName = Trim(stripslashes($_POST['YourName']));
$YourEmail = Trim(stripslashes($_POST['YourEmail']));
$YourMessage = Trim(stripslashes($_POST['YourMessage']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
exit;
}
// Check if required fields are filled in
if(trim($YourName) == '' || trim($YourEmail) == '' || trim($YourMessage) == '') {
print "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
exit;
}
// check for a valid email
if(!isValidEmail($YourEmail) || !domain_exists($YourEmail))
die("<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">");
else {
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $YourName;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $YourEmail;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $YourMessage;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contact_sent.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
}
}
?>
This will now check that the email is valid in a string, like does it contain "@" etc... Then if it passes that it then checks for an actual valid email by checking the domain, if it passes that then the email is sent.
If this doesn't work for you then you may need to find an alternative. checkdnsrr() function does not work on windows.
Bookmarks