Page 1 of 1
script execution
Posted: Thu Dec 18, 2003 9:02 am
by gecko
i would like the below script first to check if an emai field is empty. and i only want the script to execute further if the field is not empty. this is working.
then i would like the script to check the format, and only continue exectuing when the format is a valid email address format.
however whether the format is correct or not, my script just continues to send the mail to an invalid email address.
any ideas on what i need to do.
<?php
if ($email=="")echo 'whatever';
else {
function emailcheck($intext) {
$theresults = ereg("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $intext, $trashed);
if ($theresults) { $isamatch = ""; } else { $isamatch = 'whatever'; }
echo("$isamatch<br>\n");
}
emailcheck("$email");
$from = "whatever> ";
$subject = "whatever";
$message = 'whatever'
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: ".$from."\r\n";
mail($email, $subject, $message, $headers);
echo 'whatever';
?>
thanks
Posted: Thu Dec 18, 2003 9:05 am
by Draco_03
Hey i don't know but you can always out a small js script in your head
Code: Select all
function checkform(name of your form)
{
if (!(/^\w+(ї\.-]?\w+)*@\w+(ї\.-]?\w+)*(\.\w{2,3})+$/.test(name_of_your_form.name_of_your_field.value))){
alert("msg output.")
return (false)
}
return (true)
}
Posted: Thu Dec 18, 2003 9:22 am
by gecko
thanks, but i actually need to go to an other page when the email format is not correct, so i think i need to solve this in php coding. i need to put in a condition for when the format is correct, but i do not know how to do that
Posted: Thu Dec 18, 2003 9:30 am
by Draco_03
Ok well i'm a noob but you could try that..
use this as an exemple
Code: Select all
if (!$checkmail) {
//include the code you want to execute when the email is WRONG
exit;
}else{
//include the code when the email is good
}
with the "!" you can chek when a function "is not equals to"
dunno if i helped..
but hey i tried
cheers
reply
Posted: Thu Dec 18, 2003 10:01 am
by dull1554
try this
and i'm assuming your using a form to send the mail, this script is just to process the form
Code: Select all
<?php
function emailcheck($intext) {
$theresults = ereg("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $intext, $trashed);
if ($theresults) { $isamatch = ""; } else { $isamatch = 'whatever'; }
echo("$isamatch<br>\n");
}
if (!isset($_POST['email'])){//checks to see if the form field 'email' is not set
echo 'please enter a email address';
exit;
}
elseif(emailcheck("$email")){//uses your function to see if its a valid email address
exit;
}
else{//if everything is ok it sends the mail
$from = "whatever> ";
$subject = "whatever";
$message = 'whatever';
// Added a ; --JAM
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: ".$from."\r\n";
mail($email, $subject, $message, $headers);
echo 'whatever';
}
?>
i'm pretty sure this will work, but i can't rey it b/c im in school right no
Posted: Thu Dec 18, 2003 10:07 am
by Saethyr
try this. changed it to what I think you were trying to do.
Code: Select all
function ValidateMail($Email) {
$result = array();
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)) {
$result[0]="TRUE";
$result[1]="FALSE";
return $result;
}
$newEmail = ValidateMail($email);
if ($newEmail == "FALSE")
{
echo "email is not valid!";
exit;
}
else
{
$from = "fake@email.com";
$subject = "whatever";
$message = 'whatever';
// Added a ; --JAM
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: ".$from."\r\n";
mail($email, $subject, $message, $headers);
echo 'Email Sent To $email!';
}
Saethyr
Posted: Thu Dec 18, 2003 2:54 pm
by JAM
Just adding some input about redirects:
Code: Select all
header("Location: http://www.example.com/");
By using the header() function you can send the user to somewhere else. So if we take
Saethyr's example above;
Code: Select all
/// cut...
if ($newEmail == "FALSE")
{
header("Location: bad_email.php"); // ...example
exit;
}
// cut...
script execution
Posted: Thu Dec 18, 2003 2:58 pm
by gecko
still not getting there. it still goes on with sending the mail if the email format is not correct.
thanks for trying though.
Posted: Thu Dec 18, 2003 3:05 pm
by Draco_03
btw the code you posted isn't correct
gecko wrote:
however whether the format is correct or not, my script just continues to send the mail to an invalid email address.
any ideas on what i need to do.
Code: Select all
if ($email=="")echo 'whatever';//this isn't good
else {
replace with
Code: Select all
if ($email==""){ //see the opening bracket
echo "whatever";
}else{
//whatever code you want
cheers
Posted: Thu Dec 18, 2003 3:08 pm
by JAM
Code: Select all
<?php
$email = 'foo@example.com';
function ValidateMail($email) {
if (eregi("^[\w.-]+@[\w.-]+\.(com|net|org|edu|mil|gov|gob|info|tv|biz) +(\.[A-Za-z]{2}|\s*)$", $email)) {
/*
^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$
...might also be worth looking at as expression (?)
*/
return 1;
} else {
return 0;
} }
// validate $email...
$newEmail = ValidateMail($email);
// act upon result...
// forgot the !
if (!$newEmail) {
header("Location: bad_page.php");
exit;
} else {
$from = "fake@email.com";
$subject = "whatever";
$message = "whatever";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: ".$from."\r\n";
mail($email, $subject, $message, $headers);
echo 'Email Sent To '.$email.'!';
}
?>
script execution
Posted: Thu Dec 18, 2003 3:23 pm
by gecko
jam i tried your script.
the result = Email Sent To ff!
this happened when i filled in ff in the form, so it is still accepting a non valid email address.
thanks anyway
Posted: Thu Dec 18, 2003 3:36 pm
by JAM
Yah, I forgot a exlamation mark in the code ( if (!$newEmail) { ).
Corrected that. Other suggestions on the 'perfect' regular expression is welcome as I personally don't know to much about those... =/
script execution
Posted: Thu Dec 18, 2003 3:50 pm
by gecko
thanks jam i noticed the same. it is working fine now.
thanks a lot for your help