Mail Sending in Prestashop 1.4

Today we help developers figure out how to send mails in Prestashop. When you read this post all the way through, you will be able to deliver this kind of messages to customers:

Go read how to do it!

In Prestashop there is the class Mail to work with mails, which is available in a standard folder with all “classes”. Mail is the basic class, but it uses the “Swift” library that is activated in the file with class description. Mail class has only three methods, all being static, i.e you can use them without class instance creation.

The methods are:

  • Mail :: Send – it sends messages using templates.

Method parameters:
($id_lang, $template, $subject, $templateVars, $to,
$toName = null, $from = null, $fromName = null, $fileAttachment = null, $modeSMTP = null, $templatePath = _PS_MAIL_DIR_, $die = false)

  • Mail :: sendMailTest – it sends messages with the SMTP server indication without using templates.

Method parameters:
($smtpChecked, $smtpServer, $content, $subject, $type, $to, $from, $smtpLogin, $smtpPassword, $smtpPort = 25, $smtpEncryption)

  • Mail :: l – it is designed for language translations

Method parameters:
($ string, $ id_lang = null)

Time to send the message.

First, we create a template for it. The template should include two formats: HTML and TXT. Templates are created for each language separately. We create a new folder and name it “mails” in the module folder, then we add extra folders in there for each language and name them: de, en, es, fr, it (Prestashop has 5 languages by default). Each language folder mush have two template files (HTML and TXT). These files names can be arbitrary.
Consider the example of HTML template file:

TXT template file example: Hi, {firstname} {lastname}! “{firstname}” and “{lastname}” – are variables that will be replaced by necessary content (any number can be created). Now let’s write a code that will send a message to the customer with test@test.test email:

Good luck practicing sending Prestashop mails to customers!



7 comments

  1. Hi,

    Is there a chance that you upload the entire module. I’m having troubles creating email module, even-though I follow your instructions.

    Kind regards,

  2. Marko,

    Unfortunately, we can not upload the entire module. But I know for sure that the code is working.

    Other option for you will be to use a standard Prestashop functionality (“Mail alerts” module).

  3. Thank you for this explanation very useful; i like read your tutorials, they are so clean :)

    good job!

  4. Hi,

    I know this post is old but I am trying it on version 1.615 and it gives the following error
    “Parse error: syntax error, unexpected ‘&’ in /home/mydomain.com/public_html/shop/templatesendtest.php on line 5”

    I put the php file in the root of my shop

    line 5 is : $customer->getByEmail(‘test@test.test’); where I changed test@test.test to my email.

    Your help is appreciated.

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *