PrestaCraft on Facebook

Please like our page on Facebook to get notifications about new tutorials :)


PrestaCraft

PrestaCraft on Facebook

Please like our page on Facebook to get notifications about new tutorials :)


PrestaCraft

Many times there is necessity to send custom email anywhere before or after some action in your store. I’ll show you how to use PrestaShop function to send mail with your custom attachments and variables.

Works with PrestaShop 1.6 and 1.7.

There is a code you can use in any PHP file of your store if you want to send an email. This is a call to a static function Send of Mail class which has 5 required parameters and 10 optional parameters.
Each line has its own comment with suggested ways of getting the variable.

Attachment (optional)

If you want to add an attachment, start with this (example of adding PDF file):

$attach=array();
$content = file_get_contents(_PS_ROOT_DIR_.'/upload/random_pdf.pdf');
$attach['content'] = $content;
$attach['name'] ='Random PDF';
$attach['mime'] = 'application/pdf';

This can be also array of array (for multiple files).

For other format mime types check: https://www.sitepoint.com/mime-types-complete-list/

Send an email

Mail::Send(

    // == REQUIRED FIELDS ARE BELOW ==

    /* -- Language id --
    Basic context (if available): $this->context->language->id
    Alternative context: Context::getContext()->language->id
    Default store language: Configuration::get('PS_LANG_DEFAULT') */

    Context::getContext()->language->id,
    // --------

    /* -- Template name --
    Put your mail template into each language folder of /mails/{lang_iso}/ in .html and .txt format. 
    Ex.: my_mail_template.html and my_mail_template.txt.
    You can make your own /mails/ directory with subdirectories with all your language names (just look into /mails/ directory) anywhere you want to - The path will be specified later. */

    'my_mail_template',
    // --------

    /* -- Topic -- */

    'Hello there!',
    // --------

    /* -- Variables --
    Put null if you don't want to send any. Example of array: */

    array(
        '{your_email_variable}' => $mail_variable,
        '{another_mail_variable}' =>  $another_mail_variable
    ),
    // --------

    /* -- Receiver email address --
    It can be customer email or your email - depending on your needs. 
    Basic context (if available): $this->context->customer->email
    Alternative context: Context::getContext()->language->email
    Your main (BackOffice) email: Configuration::get("PS_SHOP_EMAIL") */
  
    $customer->email,
    // --------

    // == OPTIONAL FIELDS ARE BELOW ==

    /* -- Receiver name --
    This could be firstname and lastname of a customer.
    You can get customer context and just put ->firstname , ->lastname.
    Or just type any name you want to. */

    null,
    // --------

    /* -- Sender email --
    Could be your store email: Configuration::get("PS_SHOP_EMAIL")
    but better put the null on this */

    null,
    // --------

    /* -- Sender name --
    Could be Your firstname and lastname, shopname or both. 
    Get shop name: Configuration::get("PS_SHOP_NAME") */

    null,
    // --------

    /* -- Attachment -- */

    null, // replace with $attach variable if you want to send an attachment,
    // --------

    /* -- SMTP mode -- */

    null, // just put null here
    // --------

    /* -- Mails directory -- 
    Path to /mails/ directory with languages iso codes and with your templates. */

    _PS_ROOT_DIR_.'/mails/',
    // --------

    /* -- Die after error? --  */

    false,
    // --------

    /* -- ID Shop -- 
    Basic context (if available):$this->context->shop->id
    Alternative context: Context::getContext()->shop->id
    */

    null,
    // --------

    /* -- BCC -- 
    Bcc recipient(s) (email address). */
    null,

    // --------

    /* -- Reply to --
    Email address for setting the Reply-To header. */

    null
    // --------
);

Mail template

Just open and edit 2 specified files (my_mail_template.txt, my_mail_template.html) and style it as you want to! Rembemer you can use your variables {your_email_variable} {another_mail_variable}.

Thanks for reading 🙂

Leave a comment

Your email address will not be published.

7 comments

Leave a comment

Your email address will not be published.