mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-06-01 20:29:29 +00:00
0ef94aacda
- Changed interface method sendMessages of job jobs/MailJob.php - Added function terminateWithError to helper helpers/hlp_common_helper.php - Changed method overrideConfigs of library MailLib - Removed method getConfigs from library MailLib - Added public method getEmailNumberToSent to library MailLib - Changed method sendAllNotices of library MessageLib - Renamed private method _sendNotice to _sendOneNotice in library MessageLib - Added new private methods _loadDbNoticeEmailTemplate, _loadFsNoticeEmailTemplate and _sendNotice to library MessageLib - Changed method _getNoticeBody in library MessageLib - Removed methods _getNoticeHTMLBody and _getNoticeTXTBody from in library MessageLib
177 lines
4.7 KiB
PHP
177 lines
4.7 KiB
PHP
<?php
|
|
|
|
if (! defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
/**
|
|
* Library to manage the sending of the email
|
|
*/
|
|
class MailLib
|
|
{
|
|
private $sended; // Sended email counter
|
|
|
|
// Properties for storing the configuration
|
|
private $email_number_to_sent;
|
|
private $email_number_per_time_range;
|
|
private $email_time_range;
|
|
private $email_from_system;
|
|
|
|
/**
|
|
* Class constructor
|
|
*/
|
|
public function __construct()
|
|
{
|
|
// Set the counter to 0
|
|
$this->sended = 0;
|
|
|
|
// Get CI instance
|
|
$this->ci =& get_instance();
|
|
|
|
// The second parameter is used to avoiding name collisions in the config array
|
|
$this->ci->config->load('mail', true);
|
|
|
|
// CI Email library
|
|
$this->ci->load->library('email');
|
|
|
|
// Initializing email library with the loaded configurations
|
|
$this->ci->email->initialize($this->ci->config->config['mail']);
|
|
|
|
// Set the configuration properties with the standard configuration values
|
|
$this->email_number_to_sent = $this->getEmailCfgItem('email_number_to_sent');
|
|
$this->email_number_per_time_range = $this->getEmailCfgItem('email_number_per_time_range');
|
|
$this->email_time_range = $this->getEmailCfgItem('email_time_range');
|
|
$this->email_from_system = $this->getEmailCfgItem('email_from_system');
|
|
$this->alias_from_system = $this->getEmailCfgItem('alias_from_system');
|
|
}
|
|
|
|
/**
|
|
* Sends a single email
|
|
*/
|
|
public function send($from, $to, $subject, $message, $alias = '', $cc = null, $bcc = null, $altMessage = '', $bulk = false, $autogenerated = false)
|
|
{
|
|
// If from is not specified then use the standard one
|
|
if (is_null($from) || $from == '')
|
|
{
|
|
$from = $this->email_from_system;
|
|
// If alias is not specified then use the standard one
|
|
if (is_null($alias) || $alias == '')
|
|
{
|
|
$alias = $this->alias_from_system;
|
|
}
|
|
}
|
|
if (defined('MAIL_FROM') && MAIL_FROM != '')
|
|
{
|
|
$from = MAIL_FROM;
|
|
if (is_null($alias) || $alias == '')
|
|
{
|
|
$alias = $this->alias_from_system;
|
|
}
|
|
}
|
|
|
|
$this->ci->email->from($from, $alias);
|
|
|
|
// Check if the email address of the debug recipient is a valid one
|
|
$recipient = $to;
|
|
$recipientCC = $cc;
|
|
$recipientBCC = $bcc;
|
|
if (defined('MAIL_DEBUG') && MAIL_DEBUG != '')
|
|
{
|
|
// if is it valid use it!!!
|
|
$recipient = MAIL_DEBUG;
|
|
if ($recipientCC != '')
|
|
$recipientCC = MAIL_DEBUG;
|
|
if ($recipientBCC != '')
|
|
$recipientBCC = MAIL_DEBUG;
|
|
}
|
|
|
|
$this->ci->email->to($recipient);
|
|
if (!is_null($recipientCC)) $this->ci->email->cc($recipientCC);
|
|
if (!is_null($recipientBCC)) $this->ci->email->bcc($recipientBCC);
|
|
$this->ci->email->subject($subject);
|
|
$this->ci->email->message($message);
|
|
if (!isEmptyString($altMessage)) $this->ci->email->set_alt_message($altMessage);
|
|
|
|
if($bulk)
|
|
$this->ci->email->set_header('Precedence', 'bulk');
|
|
if($autogenerated)
|
|
$this->ci->email->set_header('Auto-Submitted', 'auto-generated');
|
|
|
|
// Avoid printing on standard output ugly error messages
|
|
$result = @$this->ci->email->send();
|
|
|
|
// If the email was succesfully sended then increment the counter
|
|
// and checks if it has to wait until the sending of the next
|
|
if ($result)
|
|
{
|
|
$this->sended++;
|
|
$this->wait();
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Overrides configuration parameters
|
|
* If the given parameters are not null or empty strings then they are used to override
|
|
* the following properties of this object:
|
|
* - email_number_to_sent
|
|
* - email_number_per_time_range
|
|
* - email_time_range
|
|
* - email_from_system
|
|
*/
|
|
public function overrideConfigs($numberToSent, $numberPerTimeRange, $emailTimeRange, $emailFromSystem)
|
|
{
|
|
if (is_numeric($numberToSent)) $this->email_number_to_sent = $numberToSent;
|
|
|
|
if (is_numeric($numberPerTimeRange)) $this->email_number_per_time_range = $numberPerTimeRange;
|
|
|
|
if (is_numeric($emailTimeRange)) $this->email_time_range = $emailTimeRange;
|
|
|
|
if (!isEmptyString($emailFromSystem) && filter_var($emailFromSystem, FILTER_VALIDATE_EMAIL))
|
|
{
|
|
$this->email_from_system = $emailFromSystem;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns value of property email_number_to_sent
|
|
*/
|
|
public function getEmailNumberToSent()
|
|
{
|
|
return $this->email_number_to_sent;
|
|
}
|
|
|
|
/**
|
|
* Validates an email address
|
|
*/
|
|
public function validateEmailAddress($emailAddress)
|
|
{
|
|
$valid = false;
|
|
|
|
if (!isEmptyString($emailAddress))
|
|
{
|
|
$valid = filter_var($emailAddress, FILTER_VALIDATE_EMAIL);
|
|
}
|
|
|
|
return $valid;
|
|
}
|
|
|
|
/**
|
|
* Checks if it has to wait until the sending of the next
|
|
*/
|
|
private function wait()
|
|
{
|
|
if ($this->sended == $this->email_number_per_time_range)
|
|
{
|
|
sleep($this->email_time_range); // Wait!!!
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets an item from the email configuration array
|
|
*/
|
|
private function getEmailCfgItem($itemName)
|
|
{
|
|
return $this->ci->config->item($itemName, EMAIL_CONFIG_INDEX);
|
|
}
|
|
}
|