- Added new parameter $email_from_system to MailJob controller

- Added library MailLib to manage the sending of the email
- Changed configuration file mail.php
- Changed configuration file message.php
- Changed library MessageLib to get a better separation between the messaging
system and sending e-mail
This commit is contained in:
bison
2016-09-06 11:10:48 +02:00
parent 451c1ec541
commit 9f919e6e7b
5 changed files with 214 additions and 115 deletions
+135
View File
@@ -0,0 +1,135 @@
<?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");
}
/**
* Sends a single email
*/
public function send($from, $to, $subject, $message, $alias = "", $cc = null, $bcc = null)
{
// If from is not specified then use the standard one
if (is_null($from) || $from == "")
{
$from = $this->email_from_system;
}
$this->ci->email->from($from, $alias);
$this->ci->email->to($to);
if (!is_null($cc)) $this->ci->email->cc($cc);
if (!is_null($bcc)) $this->ci->email->bcc($bcc);
$this->ci->email->subject($subject);
$this->ci->email->message($message);
// 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;
}
/**
* To ovveride the configurations
*/
public function overrideConfigs($cfg)
{
if (!is_null($cfg))
{
if (isset($cfg->email_number_to_sent) && is_numeric($cfg->email_number_to_sent))
{
$this->email_number_to_sent = $cfg->email_number_to_sent;
}
if (isset($cfg->email_number_per_time_range) && is_numeric($cfg->email_number_per_time_range))
{
$this->email_number_per_time_range = $cfg->email_number_per_time_range;
}
if (isset($cfg->email_time_range) && is_numeric($cfg->email_time_range))
{
$this->email_time_range = $cfg->email_time_range;
}
if (isset($cfg->email_from_system) && filter_var($cfg->email_from_system, FILTER_VALIDATE_EMAIL))
{
$this->email_from_system = $cfg->email_from_system;
}
}
}
/**
* Returns the current configuration
*/
public function getConfigs()
{
$cfg = new stdClass();
$cfg->email_number_to_sent = $this->email_number_to_sent;
$cfg->email_number_per_time_range = $this->email_number_per_time_range;
$cfg->email_time_range = $this->email_time_range;
$cfg->email_from_system = $this->email_from_system;
return $cfg;
}
/**
* 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);
}
}
+33 -70
View File
@@ -9,33 +9,28 @@ class MessageLib
{
private $recipients = array(); // not used anymore
public function __construct()
{
public function __construct()
{
$this->ci =& get_instance();
// Loads message configuration
$this->ci->config->load('message');
// 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");
$this->ci->config->load("message");
// CI Parser library
$this->ci->load->library("parser");
// Loads LogLib
$this->ci->load->library("LogLib");
// Loads VorlageLib
$this->ci->load->library("VorlageLib");
// Initializing email library with the loaded configurations
$this->ci->email->initialize($this->ci->config->config["mail"]);
// Loads Mail library
$this->ci->load->library("MailLib");
// Loading models
$this->ci->load->model("system/Message_model", "MessageModel");
$this->ci->load->model("system/MsgStatus_model", "MsgStatusModel");
$this->ci->load->model("system/Recipient_model", "RecipientModel");
$this->ci->load->model("system/Attachment_model", "AttachmentModel");
// Loads fhc helper
$this->ci->load->helper("fhc");
@@ -361,8 +356,8 @@ class MessageLib
// If no errors were occurred
if (is_object($result) && $result->error == EXIT_SUCCESS)
{
// If the system is configured to send emails immediately
if ($this->getEmailCfgItem("email_send_immediately") === true)
// If the system is configured to send messages immediately
if ($this->ci->config->item("send_immediately") === true)
{
// Send message by email!
$resultSendEmail = $this->sendOne($msg_id, $subject, $parsedText);
@@ -424,16 +419,26 @@ class MessageLib
/**
* Gets all the messages from DB and sends them via email
*/
public function sendAll($numberToSent = null, $numberPerTimeRange = null, $email_time_range = null)
public function sendAll($numberToSent = null, $numberPerTimeRange = null, $email_time_range = null, $email_from_system = null)
{
$sent = true; // optimistic expectation
// Gets a number (email_number_to_sent) of unsent messages from DB
// Gets standard configs
$cfg = $this->ci->maillib->getConfigs();
$cfg->email_number_to_sent = $numberToSent;
$cfg->email_number_per_time_range = $numberPerTimeRange;
$cfg->email_time_range = $email_time_range;
$cfg->email_from_system = $email_from_system;
// Overrides configs with the parameters
$this->ci->maillib->overrideConfigs($cfg);
// Gets a number ($this->ci->maillib->getMaxEmailToSent()) of unsent messages from DB
// having EMAIL_KONTAKT_TYPE as relative contact type
$result = $this->ci->RecipientModel->getMessages(
EMAIL_KONTAKT_TYPE,
null,
$this->getEmailCfgItem("email_number_to_sent")
EMAIL_KONTAKT_TYPE,
null,
$this->ci->maillib->getConfigs()->email_number_to_sent
);
// Checks if errors were occurred
if (is_object($result) && $result->error == EXIT_SUCCESS)
@@ -456,14 +461,13 @@ class MessageLib
}
// If the sender kontakt does not exist, then use system
$sender = $this->getEmailCfgItem("email_from_system");
if (!is_null($result->retval[$i]->sender) && $result->retval[$i]->sender != "")
$sender = $this->ci->maillib->getConfigs()->email_from_system;
if (!is_null($result->retval[0]->sender) && $result->retval[0]->sender != "")
{
$sender = $result->retval[$i]->sender;
$sender = $result->retval[0]->sender;
}
// Sending email
$sent = $this->sendEmail(
$sent = $this->ci->maillib->send(
$sender,
$result->retval[$i]->receiver,
$result->retval[$i]->subject,
@@ -489,25 +493,8 @@ class MessageLib
{
// Setting the message as sent in DB
$sent = $this->setMessageSent($result->retval[$i]->message_id, $result->retval[$i]->receiver_id);
// If the email has been sent and the DB updated
if ($sent)
{
// If it has been sent a specified number of emails, then it has to wait
if ((is_numeric($numberPerTimeRange) && $numberPerTimeRange == $i + 1) ||
$this->getEmailCfgItem("email_number_per_time_range") == $i + 1)
{
// Gets the number of seconds to wait until the next send
$seconds = 0;
if (is_numeric($email_time_range))
$seconds = $email_time_range;
else
$seconds = $this->getEmailCfgItem("email_time_range");
sleep($seconds); // Wait!!!
}
}
else
// If some errors occurred
if (!$sent)
{
$this->ci->loglib->logError("Error while updating DB");
}
@@ -585,14 +572,14 @@ class MessageLib
}
// If the sender kontakt does not exist, then use system
$sender = $this->getEmailCfgItem("email_from_system");
$sender = $this->ci->maillib->getConfigs()->email_from_system;
if (!is_null($result->retval[0]->sender) && $result->retval[0]->sender != "")
{
$sender = $result->retval[0]->sender;
}
// Sending email
$sent = $this->sendEmail(
$sent = $this->ci->maillib->send(
$sender,
$result->retval[0]->receiver,
is_null($subject) ? $result->retval[0]->subject : $subject, // if parameter subject is not null, use it!
@@ -706,30 +693,6 @@ class MessageLib
);
}
/**
* Gets an item from the email configuration array
*/
private function getEmailCfgItem($itemName)
{
return $this->ci->config->item($itemName, EMAIL_CONFIG_INDEX);
}
/**
* Sends a single email
*/
private function sendEmail($from, $to, $subject, $message, $alias = "", $cc = null, $bcc = null)
{
$this->ci->email->from($from, $alias);
$this->ci->email->to($to);
if (!is_null($cc)) $this->ci->email->cc($cc);
if (!is_null($bcc)) $this->ci->email->bcc($bcc);
$this->ci->email->subject($subject);
$this->ci->email->message($message);
// Avoid printing on standard output ugly error messages
return @$this->ci->email->send();
}
/**
* Update the table tbl_message_recipient
*/