From 70a1b16bd8e9d8f51cb5c71ce5aa6458f5a5a38a Mon Sep 17 00:00:00 2001 From: Gerald Raab Date: Tue, 4 Apr 2017 11:19:46 +0200 Subject: [PATCH] Introduced sender-alias for system-mail-sender --- application/config/mail.php | 3 +- application/libraries/MailLib.php | 52 ++++---- application/libraries/MessageLib.php | 176 +++++++++++++-------------- 3 files changed, 119 insertions(+), 112 deletions(-) mode change 100644 => 100755 application/config/mail.php mode change 100644 => 100755 application/libraries/MailLib.php mode change 100644 => 100755 application/libraries/MessageLib.php diff --git a/application/config/mail.php b/application/config/mail.php old mode 100644 new mode 100755 index 7842ce8c0..7bfe7f132 --- a/application/config/mail.php +++ b/application/config/mail.php @@ -8,6 +8,7 @@ $config['email_number_to_sent'] = 1000; // Number of emails to sent each time se $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['alias_from_system'] = 'No Reply'; // 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 @@ -28,4 +29,4 @@ $config['wordwrap'] = true; // {unwrap}http://example.com/a_long_link_that_shoul $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['validate'] = false; // If true then the email address will be validated \ No newline at end of file +$config['validate'] = false; // If true then the email address will be validated diff --git a/application/libraries/MailLib.php b/application/libraries/MailLib.php old mode 100644 new mode 100755 index 53ecffad2..ee2fa9419 --- a/application/libraries/MailLib.php +++ b/application/libraries/MailLib.php @@ -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); } -} \ No newline at end of file +} diff --git a/application/libraries/MessageLib.php b/application/libraries/MessageLib.php old mode 100644 new mode 100755 index d43201072..88c18e8ab --- a/application/libraries/MessageLib.php +++ b/application/libraries/MessageLib.php @@ -8,15 +8,15 @@ if (! defined('BASEPATH')) exit('No direct script access allowed'); class MessageLib { const MSG_INDX_PREFIX = 'message_'; - + public function __construct() { // Get code igniter instance $this->ci =& get_instance(); - + // Loads message configuration $this->ci->config->load('message'); - + // CI Parser library $this->ci->load->library('parser'); // Loads LogLib @@ -25,22 +25,22 @@ class MessageLib $this->ci->load->library('VorlageLib'); // 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'); // Loads helper message to manage returning messages $this->ci->load->helper('message'); - + // Loads phrases $this->ci->lang->load('message'); } - + /** * getMessage() - returns the spicified received message for a specified person * @@ -54,12 +54,12 @@ class MessageLib return $this->_error('', MSG_ERR_INVALID_MSG_ID); if (empty($person_id)) return $this->_error('', MSG_ERR_INVALID_RECIPIENTS); - + $msg = $this->ci->RecipientModel->getMessage($msg_id, $person_id); - + return $msg; } - + /** * getMessagesByUID() - will return all messages, including the latest status for specified user. It don´t returns Attachments. * @@ -70,12 +70,12 @@ class MessageLib { if (empty($uid)) return $this->_error('', MSG_ERR_INVALID_MSG_ID); - + $msg = $this->ci->RecipientModel->getMessagesByUID($uid, $all); return $msg; } - + /** * getMessagesByPerson() - will return all messages, including the latest status for specified user. It don´t returns Attachments. * @@ -86,12 +86,12 @@ class MessageLib { if (empty($person_id)) return $this->_error('', MSG_ERR_INVALID_MSG_ID); - + $msg = $this->ci->RecipientModel->getMessagesByPerson($person_id, $all); - + return $msg; } - + /** * getSentMessagesByPerson() - Get all sent messages from a person identified by person_id * @@ -102,12 +102,12 @@ class MessageLib { if (empty($person_id)) return $this->_error('', MSG_ERR_INVALID_MSG_ID); - + $msg = $this->ci->MessageModel->getMessagesByPerson($person_id, $all); - + return $msg; } - + /** * getMessageByToken * @@ -118,7 +118,7 @@ class MessageLib { if (empty($token)) return $this->_error('', MSG_ERR_INVALID_TOKEN); - + $result = $this->ci->RecipientModel->getMessageByToken($token); if (hasData($result)) { @@ -132,7 +132,7 @@ class MessageLib break; } } - + // If not found then insert the read status if ($found == -1) { @@ -141,7 +141,7 @@ class MessageLib 'person_id' => $result->retval[0]->receiver_id, 'status' => MSG_STATUS_READ ); - + $resultIns = $this->ci->MsgStatusModel->insert($statusKey); // If an error occured while writing on data base, then return it if (isError($resultIns)) @@ -150,10 +150,10 @@ class MessageLib } } } - + return $result; } - + /** * getCountUnreadMessages * @@ -164,12 +164,12 @@ class MessageLib { if (!is_numeric($person_id)) return $this->_error('', MSG_ERR_INVALID_RECIPIENTS); - + $msg = $this->ci->RecipientModel->getCountUnreadMessages($person_id); - + return $msg; } - + /** * updateMessageStatus() - will change status on message for particular user * @@ -184,18 +184,18 @@ class MessageLib { return $this->_error('', MSG_ERR_INVALID_MSG_ID); } - + if (empty($person_id)) { return $this->_error('', MSG_ERR_INVALID_USER_ID); } - + // Not use empty otherwise if status is 0 it returns an error if (!isset($status)) { return $this->_error('', MSG_ERR_INVALID_STATUS_ID); } - + // Searches if the status is already present $result = $this->ci->MsgStatusModel->load(array($message_id, $person_id, $status)); if (hasData($result)) @@ -210,13 +210,13 @@ class MessageLib 'person_id' => $person_id, 'status' => $status ); - + $result = $this->ci->MsgStatusModel->insert($statusKey); } - + return $result; } - + /** * sendMessage() - sends new internal message. This function will create a new thread * @@ -227,9 +227,9 @@ class MessageLib { $sender_id = $this->ci->config->item('system_person_id'); } - + $receivers = $this->_getReceivers($receiver_id, $oe_kurzbz); - + // If everything went ok if (isSuccess($receivers) && is_array($receivers->retval)) { @@ -238,12 +238,12 @@ class MessageLib { $result = $this->_error($receivers->retval, MSG_ERR_OU_CONTACTS_NOT_FOUND); } - + // Looping on receivers for ($i = 0; $i < count($receivers->retval); $i++) { $receiver_id = $receivers->retval[$i]->person_id; - + // Checks if the receiver exists if ($this->_checkReceiverId($receiver_id)) { @@ -289,10 +289,10 @@ class MessageLib { $result = $receivers; } - + return $result; } - + /** * sendMessageVorlage() - sends new internal message using a template * @@ -309,9 +309,9 @@ class MessageLib { $sender_id = $this->ci->config->item('system_person_id'); } - + $receivers = $this->_getReceivers($receiver_id, $oe_kurzbz); - + // If everything went ok if (isSuccess($receivers) && is_array($receivers->retval)) { @@ -325,12 +325,12 @@ class MessageLib // Load reveiver data to get its relative language $this->ci->load->model('person/Person_model', 'PersonModel'); } - + // Looping on receivers for ($i = 0; $i < count($receivers->retval); $i++) { $receiver_id = $receivers->retval[$i]->person_id; - + $result = $this->ci->PersonModel->load($receiver_id); if (hasData($result)) { @@ -341,7 +341,7 @@ class MessageLib { $sprache = $result->retval[0]->sprache; } - + // Loads template data $result = $this->ci->vorlagelib->loadVorlagetext($vorlage_kurzbz, $oe_kurzbz, $orgform_kurzbz, $sprache); if (isSuccess($result)) @@ -353,7 +353,7 @@ class MessageLib // Parses template text $parsedText = $this->ci->vorlagelib->parseVorlagetext($result->retval[0]->text, $data); $subject = $result->retval[0]->subject; - + // Save message $result = $this->_saveMessage($sender_id, $receiver_id, $subject, $parsedText, $relationmessage_id, $oe_kurzbz); // If no errors were occurred @@ -413,27 +413,27 @@ class MessageLib { $result = $receivers; } - + return $result; } - + /** * Gets all the messages from DB and sends them via email */ public function sendAll($numberToSent = null, $numberPerTimeRange = null, $email_time_range = null, $email_from_system = null) { $sent = true; // optimistic expectation - + // 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( @@ -468,7 +468,7 @@ class MessageLib { $this->ci->loglib->logError('Error while parsing the mail template'); } - + // Using a template for the plain text email body $altBody = $this->ci->parser->parse( 'templates/mailTXT', @@ -483,9 +483,9 @@ class MessageLib { $this->ci->loglib->logError('Error while parsing the mail template'); } - - // If the sender kontakt does not exist, then use system - $sender = $this->ci->maillib->getConfigs()->email_from_system; + + // If the sender kontakt does not exist, then system-sender is used if empty + $sender = ''; if (!is_null($result->retval[0]->sender) && $result->retval[0]->sender != '') { $sender = $result->retval[0]->sender; @@ -557,17 +557,17 @@ class MessageLib $this->ci->loglib->logError('Something went wrong while getting data from DB'); $sent = false; } - + return $sent; } - + /** * Gets one message from DB and sends it via email */ public function sendOne($message_id, $subject = null, $body = null, $multiPartMime = true) { $sent = true; // optimistic expectation - + // Get a specific message from DB having EMAIL_KONTAKT_TYPE as relative contact type $result = $this->ci->RecipientModel->getMessages( EMAIL_KONTAKT_TYPE, @@ -603,7 +603,7 @@ class MessageLib // $body = $result->retval[0]->body; $this->ci->loglib->logError('Error while parsing the html mail template'); } - + // Using a template for the plain text email body $altBody = $this->ci->parser->parse( 'templates/mailTXT', @@ -623,14 +623,14 @@ class MessageLib { $bodyMsg = $altBody = $body; } - - // If the sender kontakt does not exist, then use system - $sender = $this->ci->maillib->getConfigs()->email_from_system; + + // If the sender kontakt does not exist, then system-sender is used if empty + $sender = ''; if (!is_null($result->retval[0]->sender) && $result->retval[0]->sender != '') { $sender = $result->retval[0]->sender; } - + // Sending email $sent = $this->ci->maillib->send( $sender, @@ -697,21 +697,21 @@ class MessageLib $this->ci->loglib->logError('Something went wrong while getting data from DB'); $sent = false; } - + return $sent; } - + // ------------------------------------------------------------------------ // Private Functions from here out! // ------------------------------------------------------------------------ - + /** * Update the table tbl_message_recipient */ private function _updateMessageRecipient($message_id, $receiver_id, $parameters) { $updated = false; - + // Changes the status of the message from unread to read $resultUpdate = $this->ci->RecipientModel->update(array($receiver_id, $message_id), $parameters); // Checks if errors were occurred @@ -719,20 +719,20 @@ class MessageLib { $updated = true; } - + return $updated; } - + /** * Changes the status of the message from unsent to sent */ private function setMessageSent($message_id, $receiver_id) { $parameters = array('sent' => 'NOW()', 'sentinfo' => null); - + return $this->_updateMessageRecipient($message_id, $receiver_id, $parameters); } - + /** * Sets the sentInfo with the error */ @@ -742,12 +742,12 @@ class MessageLib { $sentInfo = $prevSentInfo . SENT_INFO_NEWLINE . $sentInfo; } - + $parameters = array('sent' => null, 'sentinfo' => $sentInfo); - + return $this->_updateMessageRecipient($message_id, $receiver_id, $parameters); } - + /** * Gets the receivers id that are enabled to read messages for that oe_kurzbz */ @@ -763,17 +763,17 @@ class MessageLib ' AND funktion_kurzbz = \'' . $this->ci->config->item('assistent_function') . '\'' . ' AND (NOW() BETWEEN COALESCE(datum_von, NOW()) AND COALESCE(datum_bis, NOW()))' ); - + return $receivers; } - + /** * Gets the receivers id */ private function _getReceivers($receiver_id, $oe_kurzbz = null) { $receivers = null; - + // If no receiver_id is given... if (is_null($receiver_id)) { @@ -793,10 +793,10 @@ class MessageLib $receivers = $this->_success(array(new stdClass())); $receivers->retval[0]->person_id = $receiver_id; } - + return $receivers; } - + /** * Checks if the given receiver id is a valid person */ @@ -809,10 +809,10 @@ class MessageLib { return true; } - + return false; } - + /** * Save a message in DB **/ @@ -820,7 +820,7 @@ class MessageLib { // Starts db transaction $this->ci->db->trans_start(false); - + // Save Message $msgData = array( 'person_id' => $sender_id, @@ -852,9 +852,9 @@ class MessageLib $result = $this->ci->MsgStatusModel->insert($statusData); } } - + $this->ci->db->trans_complete(); - + if ($this->ci->db->trans_status() === false || isError($result)) { $this->ci->db->trans_rollback(); @@ -865,10 +865,10 @@ class MessageLib $this->ci->db->trans_commit(); $result = $this->_success($msg_id); } - + return $result; } - + /** * Wrapper for function error */ @@ -876,7 +876,7 @@ class MessageLib { return error($retval, $code, MessageLib::MSG_INDX_PREFIX); } - + /** * Wrapper for function success */ @@ -884,12 +884,12 @@ class MessageLib { return success($retval, $code, MessageLib::MSG_INDX_PREFIX); } - + /** - * + * */ public function parseMessageText($text, $data = array()) { return $this->ci->parser->parse_string($text, $data, true); } -} \ No newline at end of file +}