Fix PrestaShop contact form not sending mails.

If you are using SMTP in your shop email configuration, then you could encounter the following problem – you are not getting e-mail notifications about new messages from contact form. And all other mails are sending fine.


It occurs because PrestaShop is sending mails from customer’s name, but using your SMTP server and your e-mail account.

You could contact your hosting support or your e-mail provider support, maybe they’ll offer you some solution. If not, you can fix this problem on PrestaShop level.

Solution:

1. The easiest way is sending mail via PHP mail() functon. You can change e-mail settings in the Back Office (Advanced Parameters -> E-mail).
1
Mails sent in this way very often fall into spam. And it is very bad of course.

2. The other way – a small code modification.

All files can be edited via FTP. File paths are relative to the site root.
All changes should be implemented in the override class: /override/controllers/ContactController.php

In the file /controllers/ContactController.php you can find the function postProcess(). And at the end of this function find the following code:

1
2
3
4
5
6
if (!Mail::Send($this->context->language->id, 'contact', Mail::l('Message from contact form').' [no_sync]',
    $var_list, $contact->email, $contact->name, null, null,
            $file_attachment, null,    _PS_MAIL_DIR_, false, null, null, $from) ||
        !Mail::Send($this->context->language->id, 'contact_form', ((isset($ct) && Validate::isLoadedObject($ct)) ? sprintf(Mail::l('Your message has been correctly sent #ct%1$s #tc%2$s'), $ct->id, $ct->token) : Mail::l('Your message has been correctly sent')), $var_list, $from, null, null, null, $file_attachment, null, _PS_MAIL_DIR_, false, null, null, $contact->email)) {
    $this->errors[] = Tools::displayError('An error occurred while sending the message.');
}

This code is for sending mails to a customer and to an admin. This example is given from PrestaShop 1.6, in the other versions it may be slightly different.

You need to replace that code by the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// save the current email sending method to the variable
$ps_mail_method = Configuration::get('PS_MAIL_METHOD');
// change email sending method to PHP mail()
Configuration::updateValue('PS_MAIL_METHOD', '1');
// send email to admin
if (!Mail::Send(
    $this->context->language->id,
    'contact',
    Mail::l('Message from contact form').' [no_sync]',
    $var_list,
    $contact->email,
    $contact->name,
    $from,
    ($customer->id ? $customer->firstname.' '.$customer->lastname : ''),
    $file_attachment
)) {
    $this->errors[] = Tools::displayError('An error occurred while sending the message.');
}

// turn back default method
Configuration::updateValue('PS_MAIL_METHOD', $ps_mail_method);
// send email to a customer
if(!Mail::Send(
    $this->context->language->id,
    'contact_form',
    ((isset($ct) && Validate::isLoadedObject($ct)) ? sprintf(Mail::l('Your message has been correctly sent #ct%1$s #tc%2$s'), $ct->id, $ct->token) : Mail::l('Your message has been correctly sent')),
    $var_list,
    $from,
    null,
    $contact->email,
    $contact->name,
    $file_attachment
)) {
    $this->errors[] = Tools::displayError('An error occurred while sending the message.');
}

The main idea of these changes: we change email sending method to PHP Mail() before sending mail to admin. And then we change it back to default (SMTP) method. So, mail will be sent to customer via SMTP server and will not fall into spam, and admin will be happy enough with PHP Mail().

6 thoughts on “Fix PrestaShop contact form not sending mails.”

  1. Hi
    I am using Prestashop 1.6.1.17 and have just noticed that the contact form (customer service) is not working. It always brings the error “An error occurred while sending the message”. I use the PHP mail function for the shop, all other messages are also sent correctly. If the customer leaves a message when ordering, this will also be transmitted and displayed in the Admin customer service. Only the contact form brings this error. I changed the code as you said, but it gives an error

  2. I wanna do something with the newsletter block module.
    I want it when it gets the submit button with customers $email
    to send a mail to admin’s mail the $email of customer
    I tried doing a mail() right in the specific If statement that returns $this->valid = $this->l(‘You have successfully subscribed to this newsletter.’); althought the site returns error 500 once I edit the blocknewsletter.php and add the mail() into the code described I saw shift mailer and Send::Mail but didnt found a way simple enough to make it work. I’d appreciate some help on this.. Thank you.

    1. Hello George!
      First you need to create new email templates that will be used for sending mails to admin.
      Navigate to “/modules/blocknewsletter/mails/” directory and in each directory (e.g. “en”) create files “newsletter_admin.html” and “newsletter_admin.txt”.
      The simplest content of those files: “New customer subscribed: {email}”.

      Then, modify the blocknewsletter.php.
      Just use this code to send mail to admin:

      1
      Mail::Send($this->context->language->id, 'newsletter_admin', Mail::l('New customer subscribed', $this->context->language->id), array('{email}' => $email), pSQL(Configuration::get('PS_SHOP_EMAIL')), null, null, null, null, null, dirname(__FILE__).'/mails/', false, $this->context->shop->id);

      If you add it inside of one-line “if” statement, don’t forget to add {}. Feel free to post your code here if you need any help 🙂

Leave a Reply to Ioannis Cancel reply

Your email address will not be published. Required fields are marked *