Introduced sender-alias for system-mail-sender

This commit is contained in:
Gerald Raab
2017-04-04 11:19:46 +02:00
parent 7594cf82a0
commit 70a1b16bd8
3 changed files with 119 additions and 112 deletions
+29 -23
View File
@@ -8,13 +8,13 @@ if (! defined("BASEPATH")) exit("No direct script access allowed");
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
*/
@@ -22,26 +22,27 @@ class MailLib
{
// 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
*/
@@ -51,10 +52,15 @@ class MailLib
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;
}
}
$this->ci->email->from($from, $alias);
// Check if the email address of the debug recipient is a valid one
$recipient = $to;
$recipientCC = $cc;
@@ -66,17 +72,17 @@ class MailLib
$recipientCC = MAIL_DEBUG;
$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 (!empty($altMessage)) $this->ci->email->set_alt_message($altMessage);
// 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)
@@ -84,10 +90,10 @@ class MailLib
$this->sended++;
$this->wait();
}
return $result;
}
/**
* To ovveride the configurations
*/
@@ -113,7 +119,7 @@ class MailLib
}
}
}
/**
* Returns the current configuration
*/
@@ -124,25 +130,25 @@ class MailLib
$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;
}
/**
* Validates an email address
*/
public function validateEmailAddress($emailAddress)
{
$valid = false;
if (!empty($emailAddress))
{
$valid = filter_var($emailAddress, FILTER_VALIDATE_EMAIL);
}
return $valid;
}
/**
* Checks if it has to wait until the sending of the next
*/
@@ -153,7 +159,7 @@ class MailLib
sleep($this->email_time_range); // Wait!!!
}
}
/**
* Gets an item from the email configuration array
*/
@@ -161,4 +167,4 @@ class MailLib
{
return $this->ci->config->item($itemName, EMAIL_CONFIG_INDEX);
}
}
}