Search
Close this search box.
Search
Close this search box.

How to block spam emails in PrestaShop

Step 1

Create a new php file in the override/controllers/front folder, and name it:

ContactController.php

Step 2

Insert this code inside the file:

class ContactController extends ContactControllerCore
{
    public function postProcess()
    {
        if(Tools::isSubmit('submitMessage')) {
 
            $message = Tools::getValue('message');
            $from = Tools::getValue('from');
 
            $banned_in_email = ['.ru', 'qq.com', '.vn']; // Make sure that you add your own extensions here
            $banned_content = ['email marketing'];
 
            foreach ($banned_in_email as $string) {
                if(strstr($from, $string))
                    $this->errors[] = Tools::displayError('This email address is not allowed');
            }
 
            foreach ($banned_content as $string) {
                if(strstr($message, $string))
                    $this->errors[] = Tools::displayError('Invalid Content');
            }
        }
        parent::postProcess();
    }
}

Step 3

Upload the file to the site with FTP and clear PrestaShop Cache. To do so go to admin panel -> Advanced Parameters -> Performance and click the clear cache button.

Step 4

Go to your shop contact form and try to send a message by using the form and a test email (eg. test@qq.com). Make sure that you cannot do so.

Learning Resources