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:
Hi, {firstname} {lastname}!
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 [email protected] email:
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 |
require(dirname(__FILE__).'/config/config.inc.php'); require_once(dirname(__FILE__).'/init.php'); $customer = new Customer(); //Set the variables for the template: $templateVars['{firstname}'] = $customer->firstname; $templateVars['{lastname}'] = $customer->lastname; $templateVars['{src_img}'] = _PS_BASE_URL_.__PS_BASE_URI__.'download/blog_belvg.png'; //Image to be displayed in the message $id_land = Language::getIdByIso('en'); //Set the English mail template $template_name = 'template'; //Specify the template file name $title = Mail::l('Test Mail'); //Mail subject with translation $from = Configuration::get('PS_SHOP_EMAIL'); //Sender's email $fromName = Configuration::get('PS_SHOP_NAME'); //Sender's name $mailDir = dirname(__FILE__).'/mails/'; //Directory with message templates $toName = $customer->firstname.' '.$customer->lastname; //Customer name $fileAttachment['content'] = file_get_contents(_PS_BASE_URL_.__PS_BASE_URI__.'download/fb.zip'); //File path $fileAttachment['name'] = 'fileAttachment'; //Attachment filename $fileAttachment['mime'] = 'application/zip'; //mime file type $send = Mail::Send($id_land, $template_name, $title, $templateVars, $customer->email, $toName, $from, $fromName, $fileAttachment, NULL, $mailDir); if ($send) echo 'Done'; else echo 'Error'; |
Good luck practicing sending Prestashop mails to customers!
@Eric, there was an error in code formatting, just fixed it
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(‘[email protected]’); where I changed [email protected] to my email.
Your help is appreciated.
Thk for your tips, are greats!
Greets
Many thanks! It work also on 1.5
Thank you for this explanation very useful; i like read your tutorials, they are so clean :)
good job!
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).
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,