Overview #
PrestaShop is a popular e-commerce platform used by merchants worldwide. It comes packed with features to assist merchants in their daily business operations. However, some built-in features offer only basic functionality. Take email validation, for example: PrestaShop only checks basic rules, such as whether the email field is empty or if the address follows a valid format (e.g., user@example.com).
What if you want to block disposable email addresses during account registration? Registering with a temporary email address often indicates suspicious activity due to the anonymity it provides.
To solve this problem, PrestaShop allows you to extend or enhance built-in features using a mechanism called Override. In this article, we will show you how to extend the default form validation to block invalid and disposable email addresses using the MailboxValidator API.
Note: Before making any modifications, please back up your project.
Registration form #
To extend the registration form:
- Create a new file named CustomerForm.php under web project root/override/classes/form.
- Open the newly created file and paste 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();
}
}
- Finally, navigate to web project root/var/cache/dev (if in development mode), or your web project root/var/cache/prod (if in production mode)and delete the class_index.php file. This manually clears the cache so your changes take effect immediately.
- Go to the registration form, enter an invalid email address, and test the result. You should see the error message appear.
Contact form #
To extend the contact form:
- Create a new file named contactform.php under web project root/override/modules/contactform.
- Open the newly created file and paste 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();
}
}
}
- Finally, navigate to web project root/var/cache/dev (if in development mode), or your web project root/var/cache/prod (if in production mode)and delete the class_index.php file. This manually clears the cache so your changes take effect immediately.
- Go to the registration form, enter an invalid email address, and test the result. You should see the error message appear.
#
Get started with MailboxValidator #
Improve your email deliverability and sender reputation in email marketing.
Register today and clean your email lists for FREE!
