How to use MailboxValidator API to validate email in PrestaShop

Overview

PrestaShop is one of the popular e-commerce platforms used by many merchants around the globe. It comes with many useful features to assist merchants in the daily business operations. However, some built-in features might be just the basic offering merely to support the PrestaShop operation. Take the email validation for example, PrestaShop only validates certain cases. These cases include whether the email field in a form is empty or not, or if the email address entered has a valid format (e.g. xx@xx.xx). What if you want to block those disposable email from account registration. Registration with such an address is a suspicious act due to the anonymity of the email address.

To solve this problem, PrestaShop allows you to extend or enhance the built-in feature using a functionality called ‘Override’. In this article, we are going to show how to extend the built-in form validation to validate and block invalid email addresses using the MailboxValidator API.

Note: Before making any modification, please backup your project.

Register form

To extend in register form:

  1. Create a new file called CustomerForm.php under your web project root/override/classes/form.
  2. Open the new created file, and save the following code into it:
    <?php
    
    class CustomerForm extends CustomerFormCore
    {
        public function validate()
        {
            $apikey = your_MailboxValidator_API_key;
            $results = file_get_contents('https://api.mailboxvalidator.com/v1/validation/single?key=' . $apikey . '&email=' . $this->getField('email')->getValue() . '&source=prestashop');
            $results = json_decode($results);
            if ($results->status == 'False') {
                $this->getField('email')->addError($this->translator->trans(
                    'Please enter a valid email address.',
                    array(),
                    'Shop.Notifications.Error'
                ));
            }
            parent::validate();
       }
    }

     

  3. Finally, go to your web project root/var/cache/dev if your project is in development, or your web project root/var/cache/prod if your project is in production, and delete a file called class_index.php. This is to manually clear the cache to make the changes take effect immediately.
  4. Now you can go to the register form, type an invalid email address and see the result. You shall see the error message appear.

 

Contact form

To extend in contact form:

  1. Create a new file called contactform.php under your web project root/override/modules/contactform.
  2. Open the new created file, and save the following code into it:
    <?php
    
    Class ContactformOverride extends Contactform
    {
        public function sendMessage()
        {
            if ($from = trim(Tools::getValue('from'))) {
                $apikey = your_MailboxValidator_API_key;
                $results = file_get_contents('https://api.mailboxvalidator.com/v1/validation/single?key=' . $apikey . '&email=' . $from . '&source=prestashop');
                $results = json_decode($results);
                if ($results->status == 'False') {
                    $this->context->controller->errors[] = $this->trans(
                        'Please enter a valid email address.',
                        [],
                        'Shop.Notifications.Error'
                    );
                }
                parent::sendMessage();
            }
        }
    }

     

  3. Finally, go to your web project root/var/cache/dev if your project is in development, or your web project root/var/cache/prod if your project is in production, and delete a file called class_index.php. This is to manually clear the cache to make the changes take effect immediately.
  4. Now you can go to the contact form, type an invalid email address and see the result. You shall see the error message appear.

Was this article helpful?

Related Articles