Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Sending mail

The shop sends letters itself: about a new order, a registration, a password reset. Your extension can send its own — for example, tell the owner about a new callback request. The class \Opencart\System\Library\Mail does this, and the sending parameters are taken from the shop settings.

Where it lives

WhatWhere
The classsystem/library/mail.php
SettingsSystem → Settings, edit the store, the Mail tab

On the Mail tab the engine is chosen — the PHP mail function or SMTP — and the SMTP server, login and password are entered.

Sending a letter

Add the sending to the storefront model or controller of the module, right after the callback request is saved:

// extension/acme_hello/catalog/controller/module/acme_hello.php, in send()
if ($this->config->get('config_mail_engine')) {
    $option = [
        'parameter' => $this->config->get('config_mail_parameter'),
        'smtp_hostname' => $this->config->get('config_mail_smtp_hostname'),
        'smtp_username' => $this->config->get('config_mail_smtp_username'),
        'smtp_password' => html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8'),
        'smtp_port' => $this->config->get('config_mail_smtp_port'),
        'smtp_timeout' => $this->config->get('config_mail_smtp_timeout'),
    ];
 
    $mail = new \Opencart\System\Library\Mail($this->config->get('config_mail_engine'), $option);
    $mail->setTo($this->config->get('config_email'));
    $mail->setFrom($this->config->get('config_email'));
    $mail->setSender(html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'));
    $mail->setSubject('New callback request');
    $mail->setText('Name: ' . $name . "\n" . 'Phone: ' . $phone);
    $mail->send();
}

Send the callback form on the storefront — a letter with the name and the phone arrives at the shop address from System → Settings.

This is the form used in releases 4.0.2 and later, where the parameters are passed to the constructor as an array. In 4.0.0 and 4.0.1 they were assigned to properties of the object after creation. Take the exact code from a core file that sends mail, for example catalog/controller/mail/order.php of your copy.

Methods

MethodWhat it sets
setTo($email)Recipient
setFrom($email)Sender address
setSender($name)Sender name
setReplyTo($email)Address for the reply
setSubject($text)Subject
setText($text)Plain text body
setHtml($html)HTML body
send()Sends the letter

An HTML letter

The body of an HTML letter is rendered from a Twig template, like a page:

$data['name'] = $name;
$data['phone'] = $phone;
 
$mail->setHtml($this->load->view('extension/acme_hello/mail/callback', $data));
{# extension/acme_hello/catalog/view/template/mail/callback.twig #}
<h2>New callback request</h2>
<p>Name: {{ name }}</p>
<p>Phone: {{ phone }}</p>

Testing locally

A local server usually cannot send real mail. Install a mail catcher such as Mailpit and enter its address and port in the SMTP fields on the Mail tab — all letters of the shop are shown in its web interface.

Common mistake

The shop name or a value from a form is put into the subject as is, and the letter arrives with Tom &amp; Anna in the subject. Strings from the settings and the request are stored escaped. In a plain text letter and in the subject decode them with html_entity_decode($value, ENT_QUOTES, 'UTF-8').

Thanks for reading!

Donate to the site — we work hard for you.