Support » Plugins » Hacks » Blocking free email accounts with php or Contact Form 7

  • Resolved KB


    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. I am using the plugin Contact Form 7 which doesn’t have the option to block email addresses and I have not found anything in the forum as a solution. So I thought adding some php would resolve the issue, however I don’t even know where to start. Any ideas would be great. Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter KB


    I did some investigation and came up with this:

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    I added if(strpos(strtolower… but I get the error”Parse error: syntax error, unexpected” when using this code.

    Does anyone happen to know why? Or what I am missing?

    Thread Starter KB


    So sorry for not using pastebin. I actually meant to and then spaced. Here is the link to the code I’ve been working with:

    Again, I added the code if(strpos(strtolower… but I get the error “Parse error: syntax error, unexpected” when using this code.

    Hey, I did manage to get something working in theory, but it didn’t work in practice. However, before I had time to get it actually working my boss decided against the mod.

    I can get you exactly what I coded, but it won’t be until Monday.

    Basically, I just had an array of free email domains and created a function that split the email apart to test the domain against the array values.

    I don’t remember where I added the call to this new function, but I think it was right about where you’ve been messing around.

    As for your error, you have the syntax wrong. Try this.
    if( strpos(strtolower( $_POST[$name] ),'') !== false )

    Thread Starter KB


    Thanks for the reply. I’d be interested to see the code you came up with so if you could pass it along, I’d appreciate it. I tried the syntax you provided, but the email end up getting sent as oppose to displaying a message that the email is not valid.

    For some reason my original method of using a separate function to test the domain wasn’t working for some reason. The function just would not work.

    So, I ended up just rewriting the original validation to split the email and check.

    Just replace the old function with my new one and it should work fine. At the top you will notice an array of domains, add/remove as many as you like.
    Then, right after the test for email type it checks that the domain is there, and finally at the bottom it tests whether the user domain is in the array of free domains.

    Thread Starter KB


    Thank you so very very much for your solution. It totally worked.

    I did come across using “explode” and even attempted it, but it was rather advanced for my php skills and I can see from your code where I was going wrong.

    For those who need this, please note that this is for the Contact Form 7 plugin and you’ll need to modify the text.php file under the Validation filter comment (line 113) in the modules folder.

    Thanks, again!

    This is great. I’d like to simplify this a bit to detect certain e-mail addresses (not domains). How would I modify the code here to create an array of e-mail addresses and rather than split the e-mail into a domain and a user, just check the entire thing?

    Thank you!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Blocking free email accounts with php or Contact Form 7’ is closed to new replies.