script execution

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gecko
Forum Commoner
Posts: 34
Joined: Thu Oct 24, 2002 3:45 am

script execution

Post 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
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post 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)
&#123;
	if (!(/^\w+(&#1111;\.-]?\w+)*@\w+(&#1111;\.-]?\w+)*(\.\w&#123;2,3&#125;)+$/.test(name_of_your_form.name_of_your_field.value)))&#123;
	alert("msg output.")
	return (false)
	&#125;
return (true)
&#125;
gecko
Forum Commoner
Posts: 34
Joined: Thu Oct 24, 2002 3:45 am

Post 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
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post 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 8)

cheers
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

reply

Post 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
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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...
gecko
Forum Commoner
Posts: 34
Joined: Thu Oct 24, 2002 3:45 am

script execution

Post 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.
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.'!';
}
?>
Last edited by JAM on Thu Dec 18, 2003 3:37 pm, edited 2 times in total.
gecko
Forum Commoner
Posts: 34
Joined: Thu Oct 24, 2002 3:45 am

script execution

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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... =/
gecko
Forum Commoner
Posts: 34
Joined: Thu Oct 24, 2002 3:45 am

script execution

Post by gecko »

thanks jam i noticed the same. it is working fine now.

thanks a lot for your help
Post Reply