Log in

View Full Version : Resolved Adding valid email into php form



Ryan Fitton
05-10-2009, 06:00 PM
Hi, im wanting how to know how i can add this sort of email validation (http://www.joelbadinas.com/2008/01/how-to-check-if-email-address-is-valid.html) into my form code below.

<?php
// 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;
}

// 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\">";
}

?>

Schmoopy
05-10-2009, 07:07 PM
<?php

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))
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\">";
}

}

?>

You can put the function into a separate page, called functions.php for example but this should work for you.

Ryan Fitton
05-11-2009, 02:32 PM
Thanks, it works great :D

Ryan Fitton
05-11-2009, 02:48 PM
Im also thinking about if it would be a could idea to add this (http://php.dzone.com/news/php-email-validator-email-mx-d), to my form script aswell, it tests to see wether the email address exists or if it doesnt.

How would i add this into my code? :confused:

Schmoopy
05-11-2009, 07:06 PM
<?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.

Ryan Fitton
05-12-2009, 03:20 PM
Thankyou =D