Sending Emails
\Opencart\Extension\KaExtensions\System\Mail
sends emails rendered from twig templates with the
store information (name, email, logo, URL) in the language of the recipient. The email is sent with
the mail engine and the SMTP settings of the store (System > Settings > Mail
). When the mail engine
is not set, the PHP mail()
function is used.
Basic Usage
use \Opencart\Extension\KaExtensions\System\Mail;
$mail = new Mail($language_id, $store_id);
$mail->data['order_id'] = $order_id;
$mail->send('', $email, 'Order is shipped', 'extension/my_module/mail/order_shipped');
Constructor parameters:
$language_id- id or code ('en-gb') of the language the email is rendered in. The store language is used when it is empty;$store_id- store whose name, email, logo and URL are used (default0).
send($from, $to, $subject, $tpl, $extra = [])
:
$from- sender email, the store email when empty;$to- recipient email;$subject- subject of the email, translated if it is a key of the mail language. It is also passed to the templates assubject;$tpl- template route without the extension, like in$this->load->view().<tpl>.twigis the HTML part,<tpl>_txt.twigis the text part. Forextension/my_module/mail/order_shippedcalled in the storefront these areextension/my_module/catalog/view/template/mail/order_shipped.twigandorder_shipped_txt.twig. One of the templates may be missing: without the text template the text part is made from the HTML, when both are missing the email is not sent andsend()returnsfalse;$extra- options:reply_tosets the Reply-To header.
Data available in templates: store_name
, sender
, store_url
(the customer login page of
the store), subject
, logo
(URL of the store logo) and everything added to $mail->data
.
The email has its own language object, $mail->language
, in the language of the email. It contains the
default
language file. Load the language files of your module into it, then their texts are
available in the templates with t()
and in your code with $mail->language->get()
:
$mail = new Mail($language_id);
$mail->language->load('extension/my_module/mail/order_shipped');
$mail->send('', $email, 'text_subject', 'extension/my_module/mail/order_shipped');
<p>{{ t('text_greeting') }}</p>
The file is taken from the language directory of the current area, for example
extension/my_module/catalog/language/de-de/mail/order_shipped.php
in the storefront. When it does
not exist for the language, the en-gb
file is used.
Templates per Language
A template with the suffix of the language is used first if it exists, otherwise the template without
suffix. The suffix is the code
value of the default.php
language file (en
for English,
de
for German), e.g. order_shipped_de.twig
and order_shipped.twig
.
Attachments
$mail->addAttachment(DIR_DOWNLOAD . $filename, 'invoice.pdf');
The second parameter is the file name shown in the email (optional). Ka Extensions adds named attachments
to the standard OpenCart mail class too: $mail->mail->addNamedAttachment($filename, $name)
.
Logging
When the KA_EXTENSIONS_LOG_EMAILS
constant is defined (see Useful Constants), every email sent
through OpenCart, not only through this class, is written to ka_mail_log.log
in the logs directory
with the result: success or the error message.
Without this constant, errors of sending are suppressed, so a broken mail server does not break the page.