Search code examples
phpprestashop-1.6

Mail:Send with Custom Module and Email Template


I am currently creating a module to execute a set of codes on hookActionProductCancel. Module is running well and I would like to send out an email after execution.

$template_path = $this->local_path . 'mails/';

Mail::Send((int)(Configuration::get('PS_LANG_DEFAULT')),
 'xxx', //need to change the template directory to point to custom module
 'Subject',
 array(
   '{aaa}' => $bbb,
   '{bbb}' => $ccc,
   '{ccc}' => $ddd,
   '{ddd}' => $eee
 ),
 $to,
 null, null, null);

I have created the templates and place the files as follow:

  1. ../mails/en/xxx.html
  2. ../mails/en/xxx.txt

While I understand the default navigation for email templates are above, how do I use templates placed in my custom modules directory?

I have created a directory - ../modules/custommodule/mails/ and placed both files but am not successful in pointing to it.

Any guidance is appreciated. Thank you.


Solution

  • You can specify template path in Mail::Send() .

    You can see that 11th parameter is $template_path so you just need to specify that (if you're calling send method from main module class you can use $this->local_path . 'mails/'). The $template_path parameter must be server file path not URI because the method uses file_exists() to check if a template exists. Method will extract from your module path that it is in fact a custom module template.

    Now the method will first check if you have mail template in

    themes/shop_theme/modules/mymodule/mails/iso_lang/xxx.html

    then in

    modules/mymodule/mails/iso_lang/xxx.html

    and load the first template it finds. Same goes for txt files.

    Edit:

    How to execute method correctly:

    Mail::Send(
        (int)(Configuration::get('PS_LANG_DEFAULT')),
        'xxx', //need to change the template directory to point to custom module
        'Subject',
        array(
           '{aaa}' => $bbb,
           '{bbb}' => $ccc,
           '{ccc}' => $ddd,
           '{ddd}' => $eee
        ),
        $to,
        null, 
        null, 
        null,
        null,
        null,
        $this->local_path . 'mails/' // 11th parameter is template path
    )