📜 ⬆️ ⬇️

The implementation of e-mail alerts in ImageCMS


In this publication, we will describe how the average lead programmer ImageCMS Andryusha implemented a convenient e-mail notification system for users of the online store. He himself now claims that he is not a programmer, but a fairy.

What was the problem before the implementation of the new functionality?
It saddened a lot that sending e-mail to users did not have a centralized place of management, because it created certain inconveniences for the administrator and the presence of a lot of excess code that was duplicated.
Look, with:

/** * Send email to user. * * @param SOrders $order * @return bool */ protected function _sendMail(SOrders $order) { //Check setting to send message if (ShopCore::app()->SSettings->ordersSendMessage == 'false') return; //Array of parameters to send $replaceData = array( '%userName%' => $order->getUserFullName(), '%userEmail%' => $order->getUserEmail(), '%userPhone%' => $order->getUserPhone(), '%userDeliver%' => $order->getUserDeliverTo(), '%orderId%' => $order->getId(), '%orderKey%' => $order->getKey(), '%orderLink%' => shop_url('cart/view/' . $order->getKey()), ); //Use function encode for every element from $replaceData $replaceData = array_map('encode', $replaceData); //Get settings for sending $fromEmail = ShopCore::app()->SSettings->ordersSenderEmail; $shopName = ShopCore::app()->SSettings->ordersSenderName; $theme = ShopCore::app()->SSettings->ordersMessageTheme; //Formating message $message = str_replace(array_keys($replaceData), $replaceData, ShopCore::app()->SSettings->ordersMessageText); //Load CodeIgniter Email library $this->load->library('email'); $config['mailtype'] = ShopCore::app()->SSettings->ordersMessageFormat; //Initialize library configurations $this->email->initialize($config); //Sending message $this->email->from($fromEmail, $shopName); $this->email->to($order->getUserEmail()); $this->email->subject($theme); $this->email->message($message); $this->email->send(); } protected function _sendNewMail(SOrders $order) { //Check setting to send message if (ShopCore::app()->SSettings->ordersSendMessage == 'false') return; //Array of parameters to send $replaceData['variables'] = array( '%userName%' => $order->getUserFullName(), '%userEmail%' => $order->getUserEmail(), '%userPhone%' => $order->getUserPhone(), '%userDeliver%' => $order->getUserDeliverTo(), '%orderId%' => $order->getId(), '%orderKey%' => $order->getKey(), '%orderLink%' => shop_url('cart/view/' . $order->getKey()), ); $replaceData['to'] = $order->getUserEmail(); //Load CodeIgniter Email library $this->load->library('email'); //Sending message $this->email->sendMail('toUserOrderNotification', $replaceData); } 


What did Andryusha do?
')
To centralize the entire system of creating and sending alerts, Fairy wrote a separate module. Initially, along with the module comes 6 templates integrated into the system that are most frequently used in the work of the site:
  1. User registration;
  2. password recovery;
  3. change Password;
  4. notification of the purchaser of the order;
  5. change order status;
  6. notification of the appearance.



Namespaces were used, which allows calling the method from any place:
  namespace cmsemail\classes; 


The main obstacle to achieving Andrei’s goal was far from the amount of code, namely, the need to find all the places in the code where the e-mail is being sent, and replace them with a new method. Our hero did not despair ... The fairy put his ears to the Korn group and abstracted the processing of all the logic of sending letters to the user and the administrator in a small method that can be seen on the next canvas.
Look, with:

 //Creatind link to check for administrator $checkLink = site_url() . "admin/components/run/shop/orders/createPdf/" . trim($order->getId()); //Array of parameters to send $emailData = array( 'userName' => $order->user_full_name, 'userEmail' => $order->user_email, 'userPhone' => $order->user_phone, 'userDeliver' => $order->user_deliver_to, 'orderLink' => shop_url('cart/view/' . $order->key), 'products' => $productsForEmail, 'deliveryPrice' => $deliveryPrice, 'checkLink' => $checkLink, ); //Sending emeils \cmsemail\email::getInstance()->sendEmail($order->user_email, 'make_order', $emailData); 


So this method looks inside:

 /** * send email * * @param string $send_to - recepient email * @param string $patern_name - email patern name * @param array $variables - variables to raplase in message: * $variables = array('$user$' => 'UserName') * @return bool */ public function sendEmail($send_to, $patern_name, $variables) { //loading CodeIgniter Email library $this->load->library('email'); //Getting settings $patern_settings = $this->cmsemail_model->getPaternSettings($patern_name); $default_settings = $this->cmsemail_model->getSettings(); //Prepare settings into correct array for initialize library if ($patern_settings) { foreach ($patern_settings as $key => $value) { if (!$value) { if ($default_settings[$key]) { $patern_settings[$key] = $default_settings[$key]; } } } } $default_settings['type'] = strtolower($patern_settings['type']); //Initializing library settings $this->_set_config($patern_settings); //Sending user email if active in options if ($patern_settings['user_message_active']) { $this->from_email = $patern_settings['from_email']; $this->from = $patern_settings['from']; $this->send_to = $send_to; $this->theme = $patern_settings['theme']; $this->message = $this->replaceVariables($patern_settings['user_message'], $variables); if (!$this->_sendEmail()) { $this->errors[] = lang('User message doesnt send', 'cmsemail'); } else { //Registering event if success \CMSFactory\Events::create()->registerEvent( array( 'from' => $this->from, 'from_email' => $this->from_email, 'send_to' => $this->send_to, 'theme' => $this->theme, 'message' => $this->message ), 'ParentEmail:userSend'); \CMSFactory\Events::runFactory(); } } //Sending administrator email if active in options if ($patern_settings['admin_message_active']) { $this->from_email = $patern_settings['from_email']; $this->from = $patern_settings['from']; if ($patern_settings['admin_email']) { $this->send_to = $patern_settings['admin_email']; } else { $this->send_to = $default_settings['admin_email']; } $this->theme = $patern_settings['theme']; $this->message = $this->replaceVariables($patern_settings['admin_message'], $variables); if (!$this->_sendEmail()) { $this->errors[] = lang('User message doesnt send', 'cmsemail'); } else { //Registering event if success \CMSFactory\Events::create()->registerEvent( array( 'from' => $this->from, 'from_email' => $this->from_email, 'send_to' => $this->send_to, 'theme' => $this->theme, 'message' => $this->message ), 'ParentEmail:adminSend'); \CMSFactory\Events::runFactory(); } } //Returning status if ($this->errors) { return FALSE; } else { return TRUE; } } 


So the average lead programmer centralized the entire user alert system. The code has become smaller, it has become more elegant, managing alerts is easier.



What problems does this functionality solve?

The updated alert system, localized in a single interface, greatly simplifies the work, especially for an inexperienced user, since all the functionality is assembled in the ImageCMS administrator panel.
The following screenshot shows the dashboard settings dashboard, which is received by the user and the administrator.

An example of setting up a letter to the user about the order made


An example of setting up a letter to the store administrator about the order made.


Can I create my own notification templates?
More experienced users of ImageCMS Shop can create their own alert templates, as well as integrate them into the system. A flexible system of settings allows you to vary the sending components in different combinations. Each notification has its own individual settings for the sending address, sender data, administrator's e-mail address and subject. There are also standard settings, and if the optional fields are not filled, then the data will be taken from there.
For even more convenience, the administrator provides for checking on the server the possibility of sending an alert. The sender will be sure that the recipient is notified of the event:

Source: https://habr.com/ru/post/203274/


All Articles