Solved

Email submition with the @domain.com already filled in.

Posted on 2011-03-24
2
256 Views
Last Modified: 2012-05-11
I want to have a fixed email domain name on a form I have created for my website. This is to prevent any non-derby students submitting their email addresses. The purpose of the form is for the user to request an invite to my website as it is exclusive to Derby University.

Example: studentname@unimail.derby.ac.uk

So the user can only type in the first part of their student email address. When they press submit, the whole form, including the @unimail.derby.ac.uk goes to my specified email address.


Here is my code, (I have removed my email for privacy reasons):

inviteme.php:

<table width="450px" position="left">
      <td width="100"></tr>
      <form name="contact" method="post" action="send_form_email.php">
        <tr>
          <td valign="top">
            <label for="first_name">First Name *</label>
            </td>
          <td width="338" valign="top">
            <input  type="text" name="first_name" maxlength="50" size="50">
            </td>
          </tr>
        
        <tr>
          <td valign="top"">
            <label for="last_name">Last Name *</label>
            </td>
          <td valign="top">
            <input  type="text" name="last_name" maxlength="50" size="50">
            </td>
          </tr>
        <tr>
          <td valign="top">
            <label for="email">Email Address *</label>
            </td>
          <td valign="top">
            <input  type="text" name="email" maxlength="80" size="50">
            </td>
          
          </tr>
        
        <tr>
          <td valign="top">
            <label for="comments">Comment *</label>
            </td>
          <td valign="top">
            <textarea  name="comments" cols="47" rows="3"></textarea>
            </td>
          
          </tr>
        <tr>
          <td colspan="2">
            <span style="text-align: right"></span>
            <input type="submit" value="Submit"> 
            *All fields are required</td>
          </tr></form>
</table>

Open in new window


send_form_email.php:

<?php
if(isset($_POST['email'])) {
	
	
	$email_to = "REMOVED";
	$email_subject = "Invite Me";
	
	
	function died($error) {
		
		echo "We are very sorry, but there were error(s) found with the form your submitted. ";
		echo "These errors appear below.<br /><br />";
		echo $error."<br /><br />";
		echo "Please go back and fix these errors.<br /><br />";
		die();
	}
	
	// validation expected data exists
	if(!isset($_POST['first_name']) ||
		!isset($_POST['last_name']) ||
		!isset($_POST['email']) ||
		!isset($_POST['comments'])) {
		died('We are sorry, but there appears to be a problem with the form your submitted.');		
	}
	
	$first_name = $_POST['first_name']; // required
	$last_name = $_POST['last_name']; // required
	$email_from = $_POST['email']; // required
    $comments = $_POST['comments']; // required
	
	$error_message = "";
	$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
  if(!eregi($email_exp,$email_from)) {
  	$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
	$string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$first_name)) {
  	$error_message .= 'You must enter a First Name.<br />';
  }
  if(!eregi($string_exp,$last_name)) {
  	$error_message .= 'You must enter a Last Name.<br />';
  }
  if(strlen($comments) < 2) {
  	$error_message .= 'You must enter a comment.<br />';
  
  }
  if(strlen($error_message) > 0) {
  	died($error_message);
  }
	$email_message = "Form details below.\n\n";
	
	function clean_string($string) {
	  $bad = array("content-type","bcc:","to:","cc:","href");
	  return str_replace($bad,"",$string);
	}
	
	$email_message .= "First Name: ".clean_string($first_name)."\n";
	$email_message .= "Last Name: ".clean_string($last_name)."\n";
	$email_message .= "Email: ".clean_string($email_from)."\n";
	$email_message .= "Comments: ".clean_string($comments)."\n";
	
	
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

Open in new window

0
Comment
Question by:PSTCAT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Accepted Solution

by:
stergium earned 500 total points
ID: 35205559
you could do something like this
$validmail =$_POST['email'];
$finaldomaincheck=$substr($validmail,strpost($validmail,"@"),strlen($validmail)-strpost($validmail,"@"));

this should give you at @finaldomaincheckl the chars after "@ " and with an
if ($finaldomaincheck =="unimail.derby.ac.uk") you could get your check going.
hope that helps
0
 

Author Comment

by:PSTCAT
ID: 35205592
I'm not sure I understand where to put that in the code...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question