...

View Full Version : How to add email validation to existing code?



forgottenglory
08-23-2007, 01:08 PM
I have a mailing list script that I have been using for some years now. I got it from the web and thought that it validated the email (Note: I am not very fluent in php) until I started receiving all sort of garbage through the mailing list which were not valid email addresses. Can somebody please tell me how to amend the code in such a way that it prevents people from sending invalid emails.



<?php
if(isset($_REQUEST['add_email'])){//if the form has been submitted, then process the e-mail address.
if(isset($_REQUEST['email'])){//check that an email address has been entered.
$email= $_REQUEST['email'];//assign the email address to the $email variable.
}else{
$email= NULL; // if there is no email address then make the $email variable blank/ NULL.
}
function email($to, $from, $subject, $message){//A little function to properly format the email (should work without)
$lb="\r\n";
$header = "From: ".$from;
$header.= $lb;
$header.='MIME-Version: 1.0';
$header.='Content-type: text/html; charset=iso-8859-1';
mail($to, $subject, $message, $header);
}
$to= 'info@yourhost.com';//Email address to send details to.
$subject= 'Mailing list submission';
$message_client='You have signed up to the mailing list to receive updates';
$message_admin="The following email address has signed up to the mailing list: $email ";

if($email){//if there is an email address
$email_admin= email($to, $email, $subject, $message_admin);//send the email to the site admin.
$email_client= email($email, $to, $subject, $message_client);//Send a confirmation to the client.

if(!$email_admin){//if the email has been sent, display a message.
echo '<P class="quotesMain">Thank you, your e-mail has been sent. You will receive a confirmation message via the e-mail you address provided.</P>';
}else{
echo '<P class="quotesMain">There seems to have been a system error, please go back and try again. Sorry for any inconvenience caused.</P>';
}
}else{
echo '<P class="quotesMain">There has been a problem, please click back and try again. Sorry for any inconvenience caused.</P>';
}
}else{//If the form has not been submitted, display a message.
echo '<P class="quotesMain">Enter your email address to receive updates.
Your information will not be shared with third parties.</P>';
}
?>
<!--end of PHP code for the mailing list form -->
<!-- Display the form -->
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" class="quotesMain">
<INPUT name="email" type="text" class="form" value="<?php echo $_REQUEST['email'];?>" />
<INPUT name="add_email" type="submit" class="formbutton" value="submit" />
</form>

Pennimus
08-23-2007, 03:18 PM
I uses this excellent tutorial and script - http://www.ilovejackdaniels.com/php/email-address-validation/

fl00d
08-23-2007, 07:39 PM
$checkfrom = "^.+@.+\.com$|.ca$|.net$|.info$|.org$";
if(!ereg($check_email, $email))
{
echo "<br>Please enter a valid email address";
}

Fumigator
08-23-2007, 08:50 PM
I second the ILoveJackDaniels script, I use it and it works fine. Though, there's no way you can avoid someone just making up an email address such as aaaaaaa@aaaaaaaaaaa.com-- to validate the email address you have to send an email to it with some sort of "click here to verify" link in it, or something similar, depending on your application.

StupidRalph
08-23-2007, 08:54 PM
I second the ILoveJackDaniels script, I use it and it works fine. Though, there's no way you can avoid someone just making up an email address such as aaaaaaa@aaaaaaaaaaa.com-- to validate the email address you have to send an email to it with some sort of "click here to verify" link in it, or something similar, depending on your application.

I believe someone has actually posted that as a snippet in the PHP snippet section. I too recommend the ILoveJackDaniels script :D

forgottenglory
09-04-2007, 03:15 PM
Hi guys,

I have tried to add the email validator to my existing code but somehow can't get it to work. Can anybody please tell me where I should put the validator script in my existing script and also do I need to delete part of my script. Thanks

Fumigator
09-04-2007, 05:24 PM
If you are trying out the ILoveJackDaniels script, simply copy/paste the script from the link posted above into a separate PHP file. Include that file in your main PHP script. Then check an email address by calling that function:


if (!check_email_address($emailString)) {
echo "email is invalid!";
}

forgottenglory
09-05-2007, 12:15 PM
I am using the JackDaniels script. I have placed it in an external php file. However, I don't know the syntax to insert an external php file into an existing script. I have looked around for tutorials that could show me how to do that but couldn't find any. Could you please show me how to write the correct syntax.

Thanks

forgottenglory
09-05-2007, 03:44 PM
OK...I have tried to incorporate the JackDaniels script directly into my existing script and when I test it within an invalid email address such as 'tommy', the message "Email is invalid" is displayed together with the following message "Thank you, your e-mail has been sent. You will receive a confirmation message via the e-mail you address provided."!!!

Actually irrespective of whether I put a valid address or not, a message still gets sent. Can somebody please tell me what I'm doing wrong and what do I need to change in the existing script.

Below is how I have added the JD script to my existing script:


<?php
function check_email_address($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}

if(isset($_REQUEST['add_email'])){//if the form has been submitted, then process the e-mail address.
if(isset($_REQUEST['email'])){//check that an email address has been entered.
$email= $_REQUEST['email'];//assign the email address to the $email variable.
}else{
$email= NULL; // if there is no email address then make the $email variable blank/ NULL.
}


function email($to, $from, $subject, $message){//A little function to properly format the email (should work without)
$lb="\r\n";
$header = "From: ".$from;
$header.= $lb;
$header.='MIME-Version: 1.0';
$header.='Content-type: text/html; charset=iso-8859-1';
mail($to, $subject, $message, $header);
}

$to= 'info@yourhost.com';//Email address to send details to.
$subject= 'Mailing list submission';
$message_client='You have signed up to the mailing list to receive updates to courses timetable';
$message_admin="The following email address has signed up to the mailing list: $email ";

if($email){//if there is an email address
if (!check_email_address($emailString)) {
echo "email is invalid!";
}
$email_admin= email($to, $email, $subject, $message_admin);//send the email to the site admin.
$email_client= email($email, $to, $subject, $message_client);//Send a confirmation to the client.

if(!$email_admin){//if the email has been sent, display a message.
echo '<P class="quotesMain">Thank you, your e-mail has been sent. You will receive a confirmation message via the e-mail you address provided.</P>';
}else{
echo '<P class="quotesMain">There seems to have been a system error, please go back and try again. Sorry for any inconvenience caused.</P>';
}
}else{
echo '<P class="quotesMain">There has been a problem, please click back and try again. Sorry for any inconvenience caused.</P>';
}
}else{//If the form has not been submitted, display a message.
echo '<P class="quotesMain">Enter your email address to receive updates about new courses.
Your information will not be shared with third parties.</P>';
}
?>
<!--end of PHP code for the mailing list form -->
<!-- Display the form -->
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" class="quotesMain">
<INPUT name="email" type="text" class="form" value="<?php echo $_REQUEST['email'];?>" />
<INPUT name="add_email" type="submit" class="formbutton" value="submit" />
</form>

rafiki
09-05-2007, 03:49 PM
add an else statement to this block


if (!check_email_address($emailString)) {
echo "email is invalid!";
}else{ //add else
//send the email....
}

forgottenglory
09-05-2007, 04:18 PM
I've added it as you suggested (see code below). An invalid email is not sent. However, as soon as the page loads the message "email is invalid" displays on top of the field. Any suggestions how to fix this?


<?php
function check_email_address($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}

if(isset($_REQUEST['add_email'])){//if the form has been submitted, then process the e-mail address.
if(isset($_REQUEST['email'])){//check that an email address has been entered.
$email= $_REQUEST['email'];//assign the email address to the $email variable.
}else{
$email= NULL; // if there is no email address then make the $email variable blank/ NULL.
}


function email($to, $from, $subject, $message){//A little function to properly format the email (should work without)
$lb="\r\n";
$header = "From: ".$from;
$header.= $lb;
$header.='MIME-Version: 1.0';
$header.='Content-type: text/html; charset=iso-8859-1';
mail($to, $subject, $message, $header);
}

$to= 'info@yourhost.com';//Email address to send details to.
$subject= 'Mailing list submission';
$message_client='You have signed up to the mailing list to receive updates to courses timetable';
$message_admin="The following email address has signed up to the mailing list: $email ";

if (!check_email_address($emailString)) {
echo "email is invalid!";
}else{ //add else
//send the email....

$email_admin= email($to, $email, $subject, $message_admin);//send the email to the site admin.
$email_client= email($email, $to, $subject, $message_client);//Send a confirmation to the client.

if(!$email_admin){//if the email has been sent, display a message.
echo '<P class="quotesMain">Thank you, your e-mail has been sent. You will receive a confirmation message via the e-mail you address provided.</P>';
}else{
echo '<P class="quotesMain">There seems to have been a system error, please go back and try again. Sorry for any inconvenience caused.</P>';
}
}else{
echo '<P class="quotesMain">There has been a problem, please click back and try again. Sorry for any inconvenience caused.</P>';
}
}else{//If the form has not been submitted, display a message.
echo '<P class="quotesMain">Enter your email address to receive updates about new courses.
Your information will not be shared with third parties.</P>';
}
}
?>
<!--end of PHP code for the mailing list form -->
<!-- Display the form -->
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" class="quotesMain">
<INPUT name="email" type="text" class="form" value="<?php echo $_REQUEST['email'];?>" />
<INPUT name="add_email" type="submit" class="formbutton" value="submit" />
</form>

rafiki
09-05-2007, 04:25 PM
because your not checking to see if the $email variable is set.


$email = $_POST['email']; // not sure if you set email before :)
if($email){//if there is an email address
if (!check_email_address($email)) {
echo "email is invalid!";
}else{ //send mail
}

your also using to different variable names for email, you should just use one.

moos3
09-05-2007, 05:16 PM
heres how I validate emails on my site.



function checkEmail($email){
// checks proper syntax
if(preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" , $email)) {
// gets domain name
list($username,$domain)=split('@',$email);
// checks for if MX records in the DNS
if(!checkdnsrr($domain, 'MX')) {
return false;
}
// attempts a socket connection to mail server
if(!fsockopen($domain,25,$errno,$errstr,30)) {
//if Domain false
return false;
}
//If Domain vaild
return true;
}
else{
//not correct format
return false;
}
}

forgottenglory
09-05-2007, 05:33 PM
@moo3: where in my original script would you fit your validation script for it to work properly? Thanks.

rafiki
09-05-2007, 05:44 PM
<?php
function checkEmail($email){
// checks proper syntax
if(preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" , $email)) {
// gets domain name
list($username,$domain)=split('@',$email);
// checks for if MX records in the DNS
if(!checkdnsrr($domain, 'MX')) {
return false;
}
// attempts a socket connection to mail server
if(!fsockopen($domain,25,$errno,$errstr,30)) {
//if Domain false
return false;
}
//If Domain vaild
return true;
}
else{
//not correct format
return false;
}
}
if(isset($_REQUEST['add_email'])){//if the form has been submitted, then process the e-mail address.
if(isset($_REQUEST['email'])){//check that an email address has been entered.
$email= $_REQUEST['email'];//assign the email address to the $email variable.
}else{
$email= NULL; // if there is no email address then make the $email variable blank/ NULL.
}
if (isset(['add_email']) && !checkEmail($email)){
$error = "No email address entered";
die($error);
}else{
function email($to, $from, $subject, $message){//A little function to properly format the email (should work without)
$lb="\r\n";
$header = "From: ".$from;
$header.= $lb;
$header.='MIME-Version: 1.0';
$header.='Content-type: text/html; charset=iso-8859-1';
mail($to, $subject, $message, $header);
}
$to= 'info@yourhost.com';//Email address to send details to.
$subject= 'Mailing list submission';
$message_client='You have signed up to the mailing list to receive updates';
$message_admin="The following email address has signed up to the mailing list: $email ";

if($email){//if there is an email address
$email_admin= email($to, $email, $subject, $message_admin);//send the email to the site admin.
$email_client= email($email, $to, $subject, $message_client);//Send a confirmation to the client.

if(!$email_admin){//if the email has been sent, display a message.
echo '<P class="quotesMain">Thank you, your e-mail has been sent. You will receive a confirmation message via the e-mail you address provided.</P>';
}else{
echo '<P class="quotesMain">There seems to have been a system error, please go back and try again. Sorry for any inconvenience caused.</P>';
}
}else{
echo '<P class="quotesMain">There has been a problem, please click back and try again. Sorry for any inconvenience caused.</P>';
}
}else{//If the form has not been submitted, display a message.
echo '<P class="quotesMain">Enter your email address to receive updates.
Your information will not be shared with third parties.</P>';
}
}
?>
<!--end of PHP code for the mailing list form -->
<!-- Display the form -->
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" class="quotesMain">
<INPUT name="email" type="text" class="form" value="<?php echo $_REQUEST['email'];?>" />
<INPUT name="add_email" type="submit" class="formbutton" value="submit" />
</form>

try that

forgottenglory
09-05-2007, 06:13 PM
I got the following error message on load:

Parse error: parse error, unexpected '[', expecting T_VARIABLE or '$' on line 38

rafiki
09-05-2007, 06:16 PM
sorry forgot to add $_REQUEST


<?php
function checkEmail($email){
// checks proper syntax
if(preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" , $email)) {
// gets domain name
list($username,$domain)=split('@',$email);
// checks for if MX records in the DNS
if(!checkdnsrr($domain, 'MX')) {
return false;
}
// attempts a socket connection to mail server
if(!fsockopen($domain,25,$errno,$errstr,30)) {
//if Domain false
return false;
}
//If Domain vaild
return true;
}
else{
//not correct format
return false;
}
}
if(isset($_REQUEST['add_email'])){//if the form has been submitted, then process the e-mail address.
if(isset($_REQUEST['email'])){//check that an email address has been entered.
$email= $_REQUEST['email'];//assign the email address to the $email variable.
}else{
$email= NULL; // if there is no email address then make the $email variable blank/ NULL.
}
if (isset($_REQUEST['add_email']) && !checkEmail($email)){
$error = "No email address entered";
die($error);
}else{
function email($to, $from, $subject, $message){//A little function to properly format the email (should work without)
$lb="\r\n";
$header = "From: ".$from;
$header.= $lb;
$header.='MIME-Version: 1.0';
$header.='Content-type: text/html; charset=iso-8859-1';
mail($to, $subject, $message, $header);
}
$to= 'info@yourhost.com';//Email address to send details to.
$subject= 'Mailing list submission';
$message_client='You have signed up to the mailing list to receive updates';
$message_admin="The following email address has signed up to the mailing list: $email ";

if($email){//if there is an email address
$email_admin= email($to, $email, $subject, $message_admin);//send the email to the site admin.
$email_client= email($email, $to, $subject, $message_client);//Send a confirmation to the client.

if(!$email_admin){//if the email has been sent, display a message.
echo '<P class="quotesMain">Thank you, your e-mail has been sent. You will receive a confirmation message via the e-mail you address provided.</P>';
}else{
echo '<P class="quotesMain">There seems to have been a system error, please go back and try again. Sorry for any inconvenience caused.</P>';
}
}else{
echo '<P class="quotesMain">There has been a problem, please click back and try again. Sorry for any inconvenience caused.</P>';
}
}else{//If the form has not been submitted, display a message.
echo '<P class="quotesMain">Enter your email address to receive updates.
Your information will not be shared with third parties.</P>';
}
}
?>
<!--end of PHP code for the mailing list form -->
<!-- Display the form -->
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" class="quotesMain">
<INPUT name="email" type="text" class="form" value="<?php echo $_REQUEST['email'];?>" />
<INPUT name="add_email" type="submit" class="formbutton" value="submit" />
</form>

forgottenglory
09-05-2007, 07:02 PM
Hmmm...now I got another error:

Parse error: parse error, unexpected T_ELSE in on line 67

rafiki
09-05-2007, 07:04 PM
no offence but you should be able to fix the code yourself, your recieving enough for free any how, you should be gratefull and learn to debug the small errors your self
try
googling the error

forgottenglory
09-05-2007, 07:10 PM
None taken...except that I don't code in php. I just happen to have used a mailing list script some time back that I thought had email validation inbuilt until I started receiving all sorts of junk through it. That's why I was asking for help.

forgottenglory
09-06-2007, 05:56 PM
I have been able to lay my hands on a piece of code (which works with my script without throwing any errors on load), that checks whether the email is valid. The only problem I have now is if somebody puts an invalid email address, the message

"that is an invalid format of email address."

is displayed. What I would want the script to do is to return to the original page so that the user can see the form and enter their email address again. How can this be acheived? Thanks


<?php
if(isset($_REQUEST['add_email'])){//if the form has been submitted, then process the e-mail address.
if(isset($_REQUEST['email'])){//check that an email address has been entered.
$email= $_REQUEST['email'];//assign the email address to the $email variable.
}else{
$email= NULL; // if there is no email address then make the $email variable blank/ NULL.
}

//email validation code starts here
if(!eregi('^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $email))
{
echo 'that is an invalid format of email address.';
exit;
}
else
{
echo' valid email address';
}

//email validation code ends here

function email($to, $from, $subject, $message){//A little function to properly format the email (should work without)
$lb="\r\n";
$header = "From: ".$from;
$header.= $lb;
$header.='MIME-Version: 1.0';
$header.='Content-type: text/html; charset=iso-8859-1';
mail($to, $subject, $message, $header);
}
$to= 'info@yourhost.com';//Email address to send details to.
$subject= 'Mailing list submission';
$message_client='You have signed up to the mailing list to receive updates';
$message_admin="The following email address has signed up to the mailing list: $email ";

if($email){//if there is an email address
$email_admin= email($to, $email, $subject, $message_admin);//send the email to the site admin.
$email_client= email($email, $to, $subject, $message_client);//Send a confirmation to the client.

if(!$email_admin){//if the email has been sent, display a message.
echo '<P class="quotesMain">Thank you, your e-mail has been sent. You will receive a confirmation message via the e-mail you address provided.</P>';
}else{
echo '<P class="quotesMain">There seems to have been a system error, please go back and try again. Sorry for any inconvenience caused.</P>';
}
}else{
echo '<P class="quotesMain">There has been a problem, please click back and try again. Sorry for any inconvenience caused.</P>';
}
}

else{//If the form has not been submitted, display a message.
echo '<P class="quotesMain">Enter your email address to receive updates.
Your information will not be shared with third parties.</P>';
}
?>
<!--end of PHP code for the mailing list form -->
<!-- Display the form -->
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" class="quotesMain">
<INPUT name="email" type="text" class="form" value="<?php echo $_REQUEST['email'];?>" />
<INPUT name="add_email" type="submit" class="formbutton" value="submit" />
</form>

Pennimus
09-06-2007, 06:18 PM
Where the message is being echoed, instead send a header redirect.

http://uk3.php.net/header

If you want to retain variables from this page back to the original page, you'll need to add those in the URL as get variables.

forgottenglory
09-07-2007, 01:02 PM
OK, I've done as you said and replaced the echo with a header redirection as follows:

header("Location: http://www.myserver.com/mailingTest.php");

But when I put an invalid email address, I have the following warning returned:

Warning: Cannot modify header information - headers already sent by (output started at G:\Websites\xxxxxx\htdocs\mailingTest.php:9)

What am I doing wrong?

Pennimus
09-07-2007, 04:57 PM
You are outputting some HTML or other headers before this point in the script. If you want to send headers, it must be the first thing you output to the client.

forgottenglory
09-10-2007, 01:35 PM
I have spent quite some time going through the link (http://uk3.php.net/header) posted earlier and tried the code there, but still I have been unsuccessful in getting the script to redirect the browser to the original page containing the mailing list form after an invalid email address has been input. No matter what I do I still get the same message

Warning: Cannot modify header information - headers already sent by (output started at G:\Websites\xxxxxx\htdocs\mailingTest.php:9)

Can you please tell me what to put in the header such that when an invalid email is input the user is redirected to the page with the form? This is the header in my test page:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

abduraooft
09-10-2007, 01:47 PM
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

Means, all the code to check the email format and redirect should be placed before the <!DOCTYPE declaration line (in your case). There should not be any echo/print statements before it as it will also cause the same error.

Inigoesdr
09-10-2007, 06:42 PM
You can use ob_start() (http://www.php.net/ob_start) to buffer the output if you're going to use header() later in the page.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum