Jump to content

integrating send mail form with email validation


drfill

Recommended Posts

Hello

I am somewhat of a newbie when it comes to php, so I appreciate this question is probably very elementary.

I currently have a form

[code]$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$name = $_POST['name'] ;
$from = $_POST['from'] ;
$location = $_POST['location'] ;
$interested = $_POST['interested'] ;
$budget = $_POST['budget'] ;
$distance = $_POST['distance'] ;
$progress = $_POST['progress'] ;
$comments = $_POST['comments'] ;
$http_referrer = getenv( "HTTP_REFERER" );
 
if (!isset($_POST['from'])) {
header( "Location: $formurl" );
exit ;
}
if (empty($name) || empty($from)) {
  header( "Location: $errorurl" );
  exit ;
}

if ( ereg( "[\r\n]", $name ) || ereg( "[\r\n]", $from ) ) {
header( "Location: $errorurl" );
exit ;
}

if (get_magic_quotes_gpc()) {
$comments = stripslashes( $comments );
}


$messageproper =

"Enquiry from:\n" .[/code]

etc that is a regular run of the mill 'send a email from form' script

However, I get a lot of people entering wrong email addresses, e.g. using commas, not putting a suffix on, etc - so I'm wanting to be able to validate that the email address entered is in the correct format.

So far I've tried several bits of code out, e.g.

[code]function valid_email($from)
{
  // check an email address is possibly valid
  if (ereg('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$', $from))
  return true;
  else
header( "Location: $errorurl" );
exit ;
}[/code]

but they all return one error or another. Unfortunately given my very basic knowledge of how php works I can't integrate the latter code with the above code. If anyone can give me any pointers on how to do this it would be greatly appreciated.
Link to comment
Share on other sites

ok here it is

[code]
<?

$mailto = email@address.com;
$subject = "Feedback" ;
$formurl = "index.html" ;
$errorurl = "application-error.html" ;
$thankyouurl = "_application.html" ;

$uself = 0;

$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$name = $_POST['name'] ;
$from = $_POST['from'] ;
$location = $_POST['location'] ;
$http_referrer = getenv( "HTTP_REFERER" );
 
if (!isset($_POST['from'])) {
header( "Location: $formurl" );
exit ;
}
if (empty($name) || empty($from)) {
  header( "Location: $errorurl" );
  exit ;
}

function valid_email($from)
{
  // check an email address is possibly valid
  if (ereg('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$', $from))
  return true;
  else
header( "Location: $errorurl" );
exit ;
}

if ( ereg( "[\r\n]", $name ) || ereg( "[\r\n]", $from ) ) {
header( "Location: $errorurl" );
exit ;
}

if (get_magic_quotes_gpc()) {
$comments = stripslashes( $comments );
}


$messageproper =

"Enquiry from:\n" .
"$http_referrer\n" .
"------------------------------------------------------------\n" .
"Name: $name\n" .
"Email: $from\n" .
"Location: $location\n" .
"Interested in: $interested\n" .
"\n\n------------------------------------------------------------\n" ;

mail($mailto, $subject, $messageproper,
"From: \"$from\" <$from>" . $headersep . "Return-Path: \"$name\" <$from>" . $headersep . "Reply-To: \"$from\" <$from>" . $headersep . "X-Mailer: chfeedback.php 2.07" );
header( "Location: $thankyouurl" );
exit ;

?>

[/code]

as you can see, the code is just sitting there. I've tried it in a few different places, but nought works
Link to comment
Share on other sites

Since the email verification is a function you have to call it. Functions don't do anything unless you say hey, I need you to work! :)

[code]
$mailto = email@address.com;
$subject = "Feedback" ;
$formurl = "index.html" ;
$errorurl = "application-error.html" ;
$thankyouurl = "_application.html" ;

$uself = 0;

$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$name = $_POST['name'] ;
$from = $_POST['from'] ;
$location = $_POST['location'] ;
$http_referrer = getenv( "HTTP_REFERER" );
 
if (!isset($_POST['from'])) {
header( "Location: $formurl" );
exit ;
}
if (empty($name) || empty($from)) {
  header( "Location: $errorurl" );
  exit ;
}

function valid_email($from)
{
  // check an email address is possibly valid
  if (ereg('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$', $from))
  return true;
  else
header( "Location: $errorurl" );
exit ;
}

$email_err = valid_email($from);

if ( ereg( "[\r\n]", $name ) || ereg( "[\r\n]", $from ) ) {
header( "Location: $errorurl" );
exit ;
}

if (get_magic_quotes_gpc()) {
$comments = stripslashes( $comments );
}


$messageproper =

"Enquiry from:\n" .
"$http_referrer\n" .
"------------------------------------------------------------\n" .
"Name: $name\n" .
"Email: $from\n" .
"Location: $location\n" .
"Interested in: $interested\n" .
"\n\n------------------------------------------------------------\n" ;

mail($mailto, $subject, $messageproper,
"From: \"$from\" <$from>" . $headersep . "Return-Path: \"$name\" <$from>" . $headersep . "Reply-To: \"$from\" <$from>" . $headersep . "X-Mailer: chfeedback.php 2.07" );
header( "Location: $thankyouurl" );
exit ;[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.