Block disposable email domains from registering

Hi,

I have a simple subscription form on my website’s footer. Here is the code:


<form id="subscription" method="POST" action="subscribe.php" target="_blank" class="validate" novalidate>
                      <h3 class="form-subscribe-heading">Risk Alerts!</h3>
                      <h4>Subscribe to our Risk Alerts and stay updated!</h4>

                      <div class="row">
                        <div class="col-xs-6 col-md-6">
                            <input type="text" name="firstname" value="" class="form-control input-md" placeholder="your first name"  />                             </div>
                        <div class="col-xs-6 col-md-6">
                            <input type="text" name="lastname" value="" class="form-control input-md" placeholder="your last name"  />                        </div>
                      </div><p></p>
                      <input type="email" value="" name="Email" class="form-control" placeholder="your email address" required><br />
                      <div class="clear"><input type="submit" value="Subscribe" name="subscribe" class="btn btn-md btn-primary button"></div>
                    </form>

Now I wish to know two things:

  1. How can I forward this form once the user has filled it to two different email recipients?
  2. I wish to disallow people from subscribing using disposable/free email addresses such as gmail, yahoo, hotmail. People can use only their company/official email address to subscribe such as xyz@salesforce.com . How can I make my form validate the email addresses and prompt to the visitor to use only official address in case he tries to subscribe using a disposable email address?
  3. Once the form has been successfully submitted I want the thank you message to pop up on the same page and disappear once the user clicks anywhere on the page.

Please can anyone help me with the php handler for this form. It’s very urgent.

Thank you.

First, my 2 cents :wink:

I wish to disallow people from subscribing using disposable/free email addresses such as gmail, yahoo, hotmail.

This is not a good idea. Do you know mailinator? They offer free disposable emails also (you don,t even need an account, just enter the email you want, no password). with a couple of other domain names. And there are hundred of services like this over the net. Why waste time to try to prevent people from subscribing? If they want to enter a fake email, they’ll find a fake email to enter. And personally, I use my gmail everywhere, it’s not a “disposable” email. Somebody telling me that my gmail account is invalid won’t get my business. So my advice: don’t lose time trying to prevent this kind of thing.

Now, for the PHP part… well, you just pasted an HTML form. No PHP anywhere? It’s like saying: “Hey, I have 4 wheels here, how do I make a car with that?” :wink:
Anyway, I’ll do my best to help you, but you’ll have to work a little bit too.

If you want to send secure emails and add multiple recipient, you could try out http://swiftmailer.org/
Or look on Google for “php contact form”, there are tons of examples out there (even if it’s not really a contact form, it’ll be easy to figure it out).

For your “popup” requirement, you’ll need JavaScript. Jquery would help also. Check this out: http://stackoverflow.com/questions/11344237/better-way-to-show-hide-div-element-after-click-on-input-field

Been through this already. What’s not working?

Hi Drummin & xMog,

Thank you for replying.

Drummin: I was using your code that you helped me with but after I click submit, it keeps showing me the failure message even if I am using a company/official id (which is not in the “notallowedclients” list) to subscribe as a test mail. Sorry, I am new to php and forums…so I posted my query again…failing to find your previous reply from couple of days back. I am pasting my HTML and PHP code here which are two different pages…so I couldn’t figure where should I be pasting “$success” and “$failure” message before the code is called.

XMog: Thank you for your reply. I have to do this for a client who has a specific requirement and wants only official mail ID’s to be able to subscribe. Here are my HTML and PHP code I am working with.

HTML:

              <div class="thumbnail center well well-small text-center">
                    <form id="subscription" method="POST" action="subscribe.php" class="validate" novalidate>
                      <h3 class="form-subscribe-heading">Solaron's Risk Alerts!</h3>
                      <h4>Subscribe to our ESG Risk Alerts and stay updated!</h4>

                      <div class="row">
                        <div class="col-xs-6 col-md-6">
                            <input type="text" name="firstname" value="" class="form-control input-md" placeholder="your first name"  />                        </div>
                        <div class="col-xs-6 col-md-6">
                            <input type="text" name="lastname" value="" class="form-control input-md" placeholder="your last name"  />                        </div>
                      </div><p></p>
                      <input type="email" value="" name="email" class="form-control" placeholder="your email address" required><br />
                      <div class="clear"><input type="submit" value="Subscribe" name="subscribe" class="btn btn-md btn-primary button"></div>
                    </form>
                  </div>

PHP:

<?php

/* Configuration */
$subject = 'Please subscribe me to your Risk Alerts. Thank you.'; // Set email subject line here
$mailto  = 'anushree@solaron.in'; // Email address to send form submission to
/* END Configuration */

$firstname      = $_POST['firstname'];
$lastname       = $_POST['lastname'];
$email          = $_POST['email'];

// Success Message
$success = "<html><body>
<div class=\\"row-fluid\\">
    <div class=\\"span12\\">
        <h3>Subscription successful!</h3>
        <p>Thank you for taking the time to subscribe to our weekly Risk Alerts.</p>
    </div>
</div>
";

// Failure Message
$failed = "<html>
<div class=\\"row-fluid\\">
    <div class=\\"span12\\">
        <h3>Subscription Failed!</h3>
        <p>Please use an official/company email address to subscribe.</p>
    </div>
</div>
";


// HTML for email to send submission details
$body = "
<br>
<p>The following information was submitted through the contact form on your website:</p>
<p><b>Name</b>: $firstname $lastname<br>
<b>Email</b>: $email<br>
";


$headers = "From: $firstname $lastname <$email> \\r\
";
$headers .= "Reply-To: $email \\r\
";
$headers .= "MIME-Version: 1.0\\r\
";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\\r\
";
$message = "<html><body>$body</body></html>";

//build list of not allowed providers as lowercase
$NotAllowedClients = array("aol","applemail","comcast","entourage","gmail","hotmail","outlook","iphone","thunderbird","yahoo");

preg_match_all('/\\@(.*?)\\./',$Email,$clientarr);
$client = strtolower($clientarr[1][0]);

if(!in_array($client,$NotAllowedClients)){
    //Failed
    echo "$failed"; }
else{
    //Passed
    echo "$success";
}

?>

Thank you both for your help in advance.

Well there is no reason the form HAS to post to a different page, and if you wish to show messages related to the form then you might as well stay on the same page. Noticed a few errors with the code posted. Give this a try.

<?php
if(isset($_POST['subscribe'])){ 
	/* Configuration */
	$subject = 'Please subscribe me to your Risk Alerts. Thank you.'; // Set email subject line here
	$mailto  = 'anushree@solaron.in'; // Email address to send form submission to
	/* END Configuration */
	if(empty($_POST['firstname'])){
		$error = "Please add your first name";
	}elseif(empty($_POST['lastname'])){
		$error = "Please add your last name";
	}elseif(empty($_POST['email'])){
		$error = "Please add your business email";	
	}else{ 
		$firstname      = $_POST['firstname'];
		$lastname       = $_POST['lastname'];
		$email          = $_POST['email'];
			
		// HTML for email to send submission details
		$body = "
		<br>
		<p>The following information was submitted through the contact form on your website:</p>
		<p><b>Name</b>: $firstname $lastname<br>
		<b>Email</b>: $email<br>
		";
		 
		 
		$headers = "From: $firstname $lastname <$email> \\r\
";
		$headers .= "Reply-To: $email \\r\
";
		$headers .= "MIME-Version: 1.0\\r\
";
		$headers .= "Content-Type: text/html; charset=ISO-8859-1\\r\
";
		$message = "<html><body>$body</body></html>";
		
		//build list of not allowed providers as lowercase
		$NotAllowedClients = array("aol","applemail","comcast","entourage","gmail","hotmail","outlook","iphone","thunderbird","yahoo","email");
		   
		preg_match_all('/\\@(.*?)\\./',$email,$clientarr);            
		$client = strtolower($clientarr[1][0]);            
		
		if(in_array($client,$NotAllowedClients)){
		    //Failed
			$notice = "<div class=\\"row-fluid\\">
		    <div class=\\"span12\\">
		        <h3>Subscription Failed!</h3>
		        <p>Please use an official/company email address to subscribe.  <a href=\\"?\\">Try again</a></p>
		    </div>
		</div>"; 
		}else{
		    //Passed
			//echo $message;
			mail($mailto, $subject, $message, $headers);
			$notice = "<div class=\\"row-fluid\\">
		    <div class=\\"span12\\">
		        <h3>Subscription successful!</h3>
		        <p>Thank you for taking the time to subscribe to our weekly Risk Alerts.</p>
		    </div>
		</div>";
		}
	}
} 
?> 
<!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>
	<title>Subscribe to our ESG Risk Alerts</title>
</head>
<body>
<?php
if(isset($notice)){
echo $notice;
}else{
	//Show error for missing field
	if(isset($error)){echo $error;}
?>
	<div class="thumbnail center well well-small text-center">
		<form id="subscription" method="post" action="" class="validate" novalidate>
			<h3 class="form-subscribe-heading">Solaron's Risk Alerts!</h3>
			<h4>Subscribe to our ESG Risk Alerts and stay updated!</h4>
			
			<div class="row">
				<div class="col-xs-6 col-md-6">
					<input type="text" name="firstname" value="" class="form-control input-md" placeholder="your first name"  />
				</div>
					<div class="col-xs-6 col-md-6">
				<input type="text" name="lastname" value="" class="form-control input-md" placeholder="your last name"  />
				</div>
			</div>
			<input type="text" value="" name="email" class="form-control" placeholder="your email address" required /><br />
			<div class="clear">
				<input type="submit" value="Subscribe" name="subscribe" class="btn btn-md btn-primary button" />
			</div>
		</form>
	</div> 

<?php
}
?>
</body>
</html>

Hi Drummin,

Thank you soooo much for the help!! It works!!!

However, I have all my site pages as .html and the subscription form link is present in all the pages footer section and opens into bootstrap modal when one presses on the “Subscribe” link. So for this to work…I need to change extension of all 8-9 site pages to .php or is there any other way to include the php code?

Also, after clicking on submit button, I want the success or failure message to appear within the modal only. Right now it takes me to a different page. Here is the example http://thecafecircle.com/test.php You can try to fill the dummy form and see what I am talking about.

Also when I receive the email…in my inbox…it shows the senders name as “me” instead of his first and last name. I am attaching an image to show what I mean.

Well then just remove the html form section from the posted code above, keeping it where you have it and change the form action back to subscribe.php. Notice I added the “try again” links to other messages. You’ll need to modify the link path.

subscribe.php

<?php
if(isset($_POST['subscribe'])){
	/* Configuration */
	$subject = 'Please subscribe me to your Risk Alerts. Thank you.'; // Set email subject line here
	$mailto  = 'anushree@solaron.in'; // Email address to send form submission to
	/* END Configuration */
	if(empty($_POST['firstname'])){
		$error = "Please add your first name  <a href=\\"?\\">Try again</a>";
	}elseif(empty($_POST['lastname'])){
		$error = "Please add your last name  <a href=\\"?\\">Try again</a>";
	}elseif(empty($_POST['email'])){
		$error = "Please add your business email  <a href=\\"?\\">Try again</a>";	
	}else{
		$firstname      = $_POST['firstname'];
		$lastname       = $_POST['lastname'];
		$email          = $_POST['email'];
			
		// HTML for email to send submission details
		$body = "
		<br>
		<p>The following information was submitted through the contact form on your website:</p>
		<p><b>Name</b>: $firstname $lastname<br>
		<b>Email</b>: $email<br>
		";
		
		
		$headers = "From: $firstname $lastname <$email> \\r\
";
		$headers .= "Reply-To: $email \\r\
";
		$headers .= "MIME-Version: 1.0\\r\
";
		$headers .= "Content-Type: text/html; charset=ISO-8859-1\\r\
";
		$message = "<html><body>$body</body></html>";
		
		//build list of not allowed providers as lowercase
		$NotAllowedClients = array("aol","applemail","comcast","entourage","gmail","hotmail","outlook","iphone","thunderbird","yahoo","email");
		
		preg_match_all('/\\@(.*?)\\./',$email,$clientarr);
		$client = strtolower($clientarr[1][0]);
		
		if(in_array($client,$NotAllowedClients)){
		    //Failed
			$notice = "<div class=\\"row-fluid\\">
		    <div class=\\"span12\\">
		        <h3>Subscription Failed!</h3>
		        <p>Please use an official/company email address to subscribe.  <a href=\\"?\\">Try again</a></p>
		    </div>
		</div>";
		}else{
		    //Passed
			//echo $message;
			mail($mailto, $subject, $message, $headers);
			$notice = "<div class=\\"row-fluid\\">
		    <div class=\\"span12\\">
		        <h3>Subscription successful!</h3>
		        <p>Thank you for taking the time to subscribe to our weekly Risk Alerts.</p>
		    </div>
		</div>";
		}
	}
}
?>
<!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>
	<title>Subscribe to our ESG Risk Alerts</title>
</head>
<body>
<?php
if(isset($notice)){
echo $notice;
}else{
	//Show error for missing field
	if(isset($error)){echo $error;}
}
?>
</body>
</html>

Hi,

I’m curious, why on earth would this be a valid business requirement?
If there isn’t a good reason for such an absurd request, I would go as far to say that it’s your job as a web developper to spell out to the client what a dumb idea this is.

Hi Pullo,

I did explain it to the client. However, their company wants to deal with only other businesses and not individuals. It is how it is. Cannot disclose more about their company here…just that it has to do with the service they offer.

Thank you so much drummin for all your help! Bless! :slight_smile:

Hi Drummin or anyone reading this post,

My form has been working perfect as per Drummin’s solution but I tried to AJAXify the form because of two reasons:

  1. On clicking the Subscribe button, my bootstrap modal closes leaving the user clueless as to the result of the form submission. The user cannot find out whether the form was submitted successfully or not until he clicks on the subscribe link again and the modal is opened again where the message is displayed. I want the notification to appear simultaneously to the form submission in the same modal window…without it closing.

  2. Clicking on the refresh button would resubmit the form!

…however I am sure I am missing something because of which the form freezes with a faded white screen inside the modal…and the form never sends then. Can you help? Here’s my code:

index.php

<div id="SubscribeModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&#10005;</button>
                  </div>
                  <div class="modal-body">
                    <form id="SubForm" name="SubForm" method="post" novalidate="novalidate">
                        <h3 class="form-subscribe-heading">Solaron's Risk Alerts!</h3>
                        <h4>Subscribe to our ESG Risk Alerts and stay updated!</h4>
                        <p>Please use a company/official address to subscribe</p>

                        <div class="row">
                            <div class="col-xs-6 col-md-6">
                                <input type="text" name="firstname" id="firstname" value="" class="form-control input-md" placeholder="your first name"  />
                            </div>
                                <div class="col-xs-6 col-md-6">
                            <input type="text" name="lastname" id="lastname" value="" class="form-control input-md" placeholder="your last name"  />
                            </div>
                        </div><p></p>
                        <input type="text" value="" name="email" id="email" class="form-control" placeholder="your email address" required /><br />
                        <div class="clear">
                            <input id="submit" type="submit" name="submit" value="Subscribe" class="btn btn-md btn-primary" />
                        </div>
                    </form>

                    <div id="alert" class="row-fluid">
                        <div class="span12">
                            <?php
                            if(isset($notice)){
                            echo $notice;
                            }else{
                                //Show error for missing field
                                if(isset($error)){echo $error;}
                            ?>
                            <?php
                            }
                            ?>
                        </div>
                    </div>
                  </div>
                  <div class="modal-footer"></div>
                </div><!-- /.modal-content -->
              </div><!-- /.modal-dalog -->
            </div><!-- /.modal -->

            <div class="alignright">
            <a data-toggle="modal" href="#SubscribeModal" class="text-muted">Subscribe</a>
          </div>

    <!-- Core JavaScript
    ================================================== -->
<script src="/solaron/js/script.min.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.form/3.32/jquery.form.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.11.1/jquery.validate.min.js"></script>
<script type="text/javascript">
// validate contact form
$(function() {
    $('#SubForm').validate({
        rules: {
            firstname: {
                required: true,
                minlength: 2
            },
            lastname: {
                required: true,
                minlength: 2
            },
            email: {
                required: true,
                email: true
            }
        },
        messages: {
            name: {
                required: "Please enter your first name",
                minlength: "your name must consist of at least 2 characters"
            },
            email: {
                required: "Please enter a valid official/company email address to subscribe"
            }
        },
        submitHandler: function(form) {
            $(form).ajaxSubmit({
                type:"POST",
                data: $(form).serialize(),
                url:"subscribe.php",
                success: function() {
                    $('#SubForm :input').attr('disabled', 'disabled');
                    $('#SubForm').fadeTo( "slow", 0.15, function() {
                        $(this).find(':input').attr('disabled', 'disabled');
                        $(this).find('label').css('cursor','default');
                        $('#success').fadeIn();
                    });
                },
                error: function() {
                    $('#SubForm').fadeTo( "slow", 0.15, function() {
                        $('#error').fadeIn();
                    });
                }
            });
        }
    });
});
</script>

subscribe.php


<?php

    $mailto = "abc@company.com";
    $from = $_REQUEST['email'];
    $firstname = $_REQUEST['firstname'];
    $lastname = $_REQUEST['lastname'];
    $subject = "Please subscribe me to your Risk Alerts. Thank you.";

    $body = "
        <br>
        <p>The following information was submitted through the Risk Alert's subscription form on your website:</p>
        <p><b>Name</b>: $firstname $lastname<br>
        <b>Email</b>: $email<br>
        ";

    $headers = "From: $firstname $lastname <$email> \\r\
";
    $headers .= "Reply-To: $from \\r\
";
    $headers .= "MIME-Version: 1.0\\r\
";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\\r\
";
    $message = "<html><body>$body</body></html>";

    //build list of not allowed providers as lowercase
        $NotAllowedClients = array("aol","applemail","comcast","entourage","gmail","hotmail","outlook","iphone");

        preg_match_all('/\\@(.*?)\\./',$email,$clientarr);
        $client = strtolower($clientarr[1][0]);

        if(in_array($client,$NotAllowedClients)){
            //Failed
            $notice = "<div class=\\"row-fluid\\">
            <div class=\\"span12\\">
                <h3>Subscription Failed!</h3>
                <p>Please use an official/company email address to subscribe. <a href=\\"?\\">Try again</a></p>
            </div>
        </div>";
        }else{
            //Passed
            //echo $message;
            mail($mailto, $subject, $message, $headers);
            $notice = "<div class=\\"row-fluid\\">
            <div class=\\"span12\\">
                <h3>Subscription successful!</h3>
                <p>Thank you for subscribing to our Weekly Risk Alerts. <br />
                Your filled subscription form has been received and is pending approval.<br />
                You will receive login instructions upon approval of your account.</p>
            </div>
        </div>";
        }
    }
}

?>

I don’t know much about Ajax and have been playing with this for a couple of hours. I don’t even know if the subscribe.php page is being processed, let alone how to get a message from that page to the form. I wish I could be more help, but like I said, I don’t know much about Ajax. Sorry. It almost seems like we’d want to include subscribe.php in the success: function() so $notice variable would be on the page, instead of POSTing to it… I just don’t know. Maybe the JavaScript-jQuery forum can help.

I would suggest you make sure it works properly without JavaScript first. Not everyone has JavaScript enabled and the purpose of JavaScript for form processing is simply to improve the user experience by slightly speeding the responses.

Ajax calls should only ever be added to an already working site.

The problem I was having is even detecting a POST event. Even if I changed the the URL to my test page, e.g. url:“testpage.php”, (where the form and javascript is) and had print_r($_POST); at the top of the page, the success: function() never sends POST values. The success: function() does work in that I can fade in a success message with $(‘#success’).fadeIn(); Just not sure if it is POSTing to the url:

The best bet would be to make the pop a php file and have all the validation on that page.

If it’s going to be html adding the bad email validation to your JS might be the way to go. I’m not too good at JS but it might be something like this.

$.verify.addRules({
 isAcceptable: function() {
	var email = "id=" + document.getElementById("email").value;
	var badEmails = ["aol","applemail","comcast","entourage","gmail","hotmail","outlook","iphone","thunderbird","yahoo","email"];
	var pattern = new RegExp(badEmails.join("|"), "i");
	if(!pattern.test(email);
 	  result = true;
      message: = "passed";
	  else
      message: = "Not a company email address";
}

});

… but I can’t get this to work.