- 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
+12 -15
View File
@@ -2,32 +2,29 @@
// Define constants
define("EMAIL_CONFIG_INDEX", "mail");
define("EMAIL_KONTAKT_TYPE", "email");
define("SENT_INFO_NEWLINE", "\n");
// Define configuration parameters
$config["email_number_to_sent"] = 1000; // Number of emails to sent each time sendAll is called
$config["email_number_per_time_range"] = 1; // Number of emails to sent before pause
$config["email_time_range"] = 1; // Length of the pause in seconds
$config["email_from_system"] = "no-reply@technikum-wien.at";
$config["email_send_immediately"] = false;
// Smtp: if the CI email library has to connect to a smtp server
// Mail: if the system is setup to send emails with the standard php mail function
// Sendmail: if the system is setup to send email via Sendmail (or similar)
$config['protocol'] = 'smtp'; // mail, sendmail, or smtp
$config["protocol"] = ""; // mail, sendmail, or smtp
// If protocol is set to sendmail
$config['mailpath'] = ''; // SThe server path to Sendmail (or similar)
$config["mailpath"] = ""; // SThe server path to Sendmail (or similar)
// If protocol is set to smtp
$config['smtp_host'] = 'localhost'; // SMTP Server Address
$config['smtp_port'] = 25;
$config['smtp_timeout'] = 5; // in seconds
$config['smtp_keepalive'] = false; // Enable persistent SMTP connections
$config['smtp_user'] = '';
$config['smtp_pass'] = '';
$config['wordwrap'] = true; // {unwrap}http://example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap}
$config['wrapchars'] = 76; // Character count to wrap at.
$config['mailtype'] = 'html'; // html or text
$config['priority'] = 3; // Email Priority. 1 = highest. 5 = lowest. 3 = normal
$config["smtp_host"] = "localhost"; // SMTP Server Address
$config["smtp_port"] = 25;
$config["smtp_timeout"] = 5; // in seconds
$config["smtp_keepalive"] = false; // Enable persistent SMTP connections
$config["smtp_user"] = "";
$config["smtp_pass"] = "";
$config["wordwrap"] = true; // {unwrap}http://example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap}
$config["wrapchars"] = 76; // Character count to wrap at.
$config["mailtype"] = "html"; // html or text
$config["priority"] = 3; // Email Priority. 1 = highest. 5 = lowest. 3 = normal
+30 -26
View File
@@ -1,9 +1,13 @@
<?php
if ( ! defined('BASEPATH'))
exit('No direct script access allowed');
<?php
if (! defined("BASEPATH")) exit("No direct script access allowed");
$config["msg_delivery"] = true;
$config["send_immediately"] = false; // If the message should be sent immediately
define("EMAIL_KONTAKT_TYPE", "email"); // Email kontakt type
define("SENT_INFO_NEWLINE", "\n"); // tbl_msg_recipient->sentInfo separator
$config['msg_delivery'] = true;
/*
|--------------------------------------------------------------------------
| Constants for Messaging System
@@ -15,29 +19,29 @@ $config['msg_delivery'] = true;
|
*/
// message statuses
define('MSG_STATUS_UNREAD', 0);
define('MSG_STATUS_READ', 1);
define('MSG_STATUS_ARCHIVED', 2);
define('MSG_STATUS_DELETED', 3);
define("MSG_STATUS_UNREAD", 0);
define("MSG_STATUS_READ", 1);
define("MSG_STATUS_ARCHIVED", 2);
define("MSG_STATUS_DELETED", 3);
// priority
define('PRIORITY_LOW', 1);
define('PRIORITY_NORMAL', 2);
define('PRIORITY_HIGH', 3);
define('PRIORITY_URGENT', 4);
define("PRIORITY_LOW", 1);
define("PRIORITY_NORMAL", 2);
define("PRIORITY_HIGH", 3);
define("PRIORITY_URGENT", 4);
// status return message codes
define('MSG_SUCCESS', 0);
define('MSG_ERROR', 1);
define('MSG_ERR_INVALID_USER_ID', 2);
define('MSG_ERR_INVALID_MSG_ID', 3);
define('MSG_ERR_INVALID_THREAD_ID', 4);
define('MSG_ERR_INVALID_STATUS_ID', 5);
define('MSG_ERR_INVALID_SENDER_ID', 6);
define('MSG_ERR_INVALID_RECIPIENTS', 7);
define('MSG_MESSAGE_SENT', 8);
define('MSG_STATUS_UPDATE', 9);
define('MSG_PARTICIPANT_ADDED', 10);
define('MSG_ERR_PARTICIPANT_EXISTS', 11);
define('MSG_ERR_PARTICIPANT_NONSYSTEM', 12);
define('MSG_PARTICIPANT_REMOVED', 13);
define("MSG_SUCCESS", 0);
define("MSG_ERROR", 1);
define("MSG_ERR_INVALID_USER_ID", 2);
define("MSG_ERR_INVALID_MSG_ID", 3);
define("MSG_ERR_INVALID_THREAD_ID", 4);
define("MSG_ERR_INVALID_STATUS_ID", 5);
define("MSG_ERR_INVALID_SENDER_ID", 6);
define("MSG_ERR_INVALID_RECIPIENTS", 7);
define("MSG_MESSAGE_SENT", 8);
define("MSG_STATUS_UPDATE", 9);
define("MSG_PARTICIPANT_ADDED", 10);
define("MSG_ERR_PARTICIPANT_EXISTS", 11);
define("MSG_ERR_PARTICIPANT_NONSYSTEM", 12);
define("MSG_PARTICIPANT_REMOVED", 13);
+4 -4
View File
@@ -12,7 +12,7 @@
*/
// ------------------------------------------------------------------------
if (!defined('BASEPATH')) exit('No direct script access allowed');
if (!defined("BASEPATH")) exit("No direct script access allowed");
class MailJob extends FHC_Controller
{
@@ -24,11 +24,11 @@ class MailJob extends FHC_Controller
parent::__construct();
// Loads MessageLib
$this->load->library('MessageLib');
$this->load->library("MessageLib");
}
public function sendMessages($numberToSent = null, $numberPerTimeRange = null, $email_time_range = null)
public function sendMessages($numberToSent = null, $numberPerTimeRange = null, $email_time_range = null, $email_from_system = null)
{
$this->messagelib->sendAll($numberToSent, $numberPerTimeRange, $email_time_range);
$this->messagelib->sendAll($numberToSent, $numberPerTimeRange, $email_time_range, $email_from_system);
}
}
+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
*/