treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Blocking free email accounts for online form

  • I have an online form that allows a user to enter the email address they wish to send a message to and I'd like to block certain email addresses, like: gmail, hotmail, yahoo, etc from using the form. The site is in WP and I am using the plugin Contact Form 7 which doesn't have the option to block email addresses and I have not found anything on WP's forum as a solution. So I thought adding some php would resolve the issuse, however I don't even know where to start. Any ideas would be great. Thanks.
  • If I can ask, what is the purpose of blocking those email hosts? You've listed three very popular domains which could lead to excluding a very large number of potential users. There may be an entirely legitimate reason to do so; I'm just curious.
  • I completley agree. The reason is my client has a site that allows kids to report bulling to a teacher and what is happening is students are sending messages to their friends. Since students use free email services my client would just like them blocked.
  • Gotcha; makes perfect sense. I'm assuming, then, that students would send the messages using a school-based email address (me@mydistrict.edu or somesuch). I'm definitely not a wizard when it comes to regular expressions, but I found this thread:

    http://www.phpfreaks.com/forums/index.php?topic=137584

    which seems to be along the lines of what you're looking for. The final script in that thread excludes a certain domain, but if all the legitimate messages would come from one domain, you could also modify it to only accept that one domain.

    Hope it helps. :)

    Chris
  • For extra security why not only allow from approved domains?
  • Thanks for the help and suggestions. Since this is a public site and there are many schools with their own domain, I am thinking just adding a few common email domains would make the most sense.

    I think I'm getting closer to resolving this. I added "if(strpos(strtolower..." (see code below) but I get the error "Parse error: syntax error, unexpected" when using this code. Any ideas on why? Or what I am missing?

    <?php
    function wpcf7_text_validation_filter( $result, $tag ) {
    $type = $tag['type'];
    $name = $tag['name'];

    $_POST[$name] = trim( strtr( (string) $_POST[$name], "\n", " " ) );
    if ( 'text*' == $type ) {
    if ( '' == $_POST[$name] ) {
    $result['valid'] = false;
    $result['reason'][$name] = wpcf7_get_message( 'invalid_required' );
    }
    }

    if ( 'email' == $type || 'email*' == $type ) {
    if ( 'email*' == $type && '' == $_POST[$name] ) {
    $result['valid'] = false;
    $result['reason'][$name] = wpcf7_get_message( 'invalid_required' );
    }

    if(strpos(strtolower( '' != $_POST[$name] && ! is_email( $_POST[$name] ),'happy@happy.com') ){
    $result['valid'] = false;
    $result['reason'][$name] = wpcf7_get_message( 'invalid_email' );
    }
    }elseif ( '' != $_POST[$name] && ! is_email( $_POST[$name] ) ) {
    $result['valid'] = false;
    $result['reason'][$name] = wpcf7_get_message( 'invalid_email' );
    }
    }
    return $result;
    }
    ?>
  • Has the client considered going the other way around? Instead of having the email address manually input.

    You could have a dropdown list on the form for teachers names (that have the value of their email addresses hidden behind the scenes) that way you can ensure messages will only ever go to staff & teachers?
  • This is a public site and not specific to any one school. Anybody can use the site. To collect and manage every teacher's email address from every school would be very cumbersome. If there is a way to block gmail, yahoo and hotmail email accounts that would be greatly helpful. Here is the link to the site in case anyone wants to check it out. http://bongokidz.com/report-bullying/

    Thanks!
  • This almost seems too easy, but could you adapt the code shown here?

  • I attempted to do something like this but it was rather advanced for my php skills. However, on WP's forum I found a member who was extremely helpful and he provided the coded which was one of the methods I was attempting to use. Here is the link for those who may need it.

    Please note that this is for the Contact Form 7 plugin in WP and you'll need to modify the text.php file under the Validation filter comment (line 113) in the modules folder.

    Thank you ccc630 for all your suggestions, they were very helpful and I learned a little more about php.
  • Glad you were able to get it running -- I was a teacher for quite a few years, so I know first-hand how important a service like this is (sadly enough).