I have stuck in sending mail to store owner in the below code, but I don't how to programmatically send email to store owner.
Please check my below code and help me solve out.
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Cron;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Filesystem\DirectoryList;
use Psr\Log\LoggerInterface;
use KiwiCommerce\AdminActivity\Helper\Data as Helper;
use KiwiCommerce\AdminActivity\Api\ActivityRepositoryInterface;
/**
* Class ClearLog
* @package KiwiCommerce\AdminActivity\Cron
*/
class ClearLog extends \Magento\Framework\App\Action\Action
{
/**
* Default date format
* @var string
*/
const DATE_FORMAT = 'Y-m-d H:i:s';
/**
* @var LoggerInterface
*/
protected $fileFactory;
protected $csvProcessor;
protected $directoryList;
public $logger;
/**
* @var \Magento\Framework\Stdlib\DateTime\DateTime
*/
public $dateTime;
/**
* @var \KiwiCommerce\AdminActivity\Helper\Data
*/
public $helper;
/**
* @var ActivityRepositoryInterface
*/
public $activityRepository;
/**
* @var \KiwiCommerce\AdminActivity\Api\LoginRepositoryInterface
*/
public $loginRepository;
/**
* ClearLog constructor.
* @param LoggerInterface $logger
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
* @param Helper $helper
* @param ActivityRepositoryInterface $activityRepository
* @param \KiwiCommerce\AdminActivity\Api\LoginRepositoryInterface $loginRepository
*/
public function __construct(
LoggerInterface $logger,
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
Helper $helper,
ActivityRepositoryInterface $activityRepository,
\KiwiCommerce\AdminActivity\Api\LoginRepositoryInterface $loginRepository
) {
$this->logger = $logger;
$this->dateTime = $dateTime;
$this->helper = $helper;
$this->activityRepository = $activityRepository;
$this->loginRepository = $loginRepository;
$this->_fileFactory = $fileFactory;
$this->directory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
parent::__construct($context);
}
/**
* Return cron cleanup date
* @return null|string
*/
public function __getDate()
{
$timestamp = $this->dateTime->gmtTimestamp();
$day = $this->helper->getConfigValue('CLEAR_LOG_DAYS');
if ($day) {
$timestamp -= $day * 24 * 60 * 60;
return $this->dateTime->gmtDate(self::DATE_FORMAT, $timestamp);
}
return null;
}
/**
* Delete record which date is less than the current date
* @return $this|null
*/
public function execute()
{
try {
if (!$this->helper->isEnable()) {
return $this;
}
if ($date = $this->__getDate()) {
$activities = $this->activityRepository->getListBeforeDate($date);
$name = date('m_d_Y_H_i_s');
$filepath = 'export/Adminactivity/admin_activity_' . $name . '.csv';
$loginfilepath = 'export/Loginactivity/login_activity_' . $name . '.csv';
$this->directory->create('export');
/* Open file */
$stream = $this->directory->openFile($filepath, 'w+');
$stream->lock();
$columns = $this->getColumnHeader();
foreach ($columns as $column) {
$header[] = $column;
}
/* Write Header */
$stream->writeCsv($header);
if (!empty($activities)) {
foreach ($activities as $activity) {
$activityArray = [];
$activityArray[] = $activity['entity_id'];
$activityArray[] = $activity['username'];
$activityArray[] = $activity['name'];
$activityArray[] = $activity['admin_id'];
$activityArray[] = $activity['store_id'];
$activityArray[] = $activity['scope'];
$activityArray[] = $activity['action_type'];
$activityArray[] = $activity['remote_ip'];
$activityArray[] = $activity['forwarded_ip'];
$activityArray[] = $activity['user_agent'];
$activityArray[] = $activity['module'];
$activityArray[] = $activity['fullaction'];
$activityArray[] = $activity['item_name'];
$activityArray[] = $activity['item_url'];
$activityArray[] = $activity['is_revertable'];
$activityArray[] = $activity['revert_by'];
$activityArray[] = $activity['created_at'];
$activityArray[] = $activity['updated_at'];
$stream->writeCsv($activityArray);
$activity->delete($activity['entity_id']);
}
}
//TODO: Remove login activity detail
if ($this->helper->isLoginEnable()) {
$streamLogin = $this->directory->openFile($loginfilepath, 'w+');
$streamLogin->lock();
$loginColumns = $this->getLoginColumnHeader();
foreach ($loginColumns as $columnLog) {
$headerLogin[] = $columnLog;
}
$streamLogin->writeCsv($headerLogin);
$loginactivities = $this->loginRepository->getListBeforeDate($date);
if (!empty($loginactivities)) {
foreach ($loginactivities as $activity) {
$loginArray = [];
$loginArray[] = $activity['entity_id'];
$loginArray[] = $activity['username'];
$loginArray[] = $activity['name'];
$loginArray[] = $activity['remote_ip'];
$loginArray[] = $activity['forwarded_ip'];
$loginArray[] = $activity['user_agent'];
$loginArray[] = $activity['type'];
$loginArray[] = $activity['status'];
$loginArray[] = $activity['remarks'];
$loginArray[] = $activity['created_at'];
$streamLogin->writeCsv($loginArray);
$activity->delete($activity['entity_id']);
}
}
}
}
}
catch (\Exception $e) {
$this->logger->debug($e->getMessage());
}
return null;
}
/* Header Columns */
public function getColumnHeader() {
$headers = ['entity_id','username','name','admin_id','store_id','scope','action_type','remote_ip','forwarded_ip','user_agent','module','fullaction','item_name','item_url','is_revertable','revert_by','created_at','updated_at'];
return $headers;
}
/* Header Columns */
public function getLoginColumnHeader() {
$headers = ['entity_id','username','name','remote_ip','forwarded_ip','user_agent','type','status','remarks','created_at'];
return $headers;
}
}
Thanks For Advanced...