From d6664e74c52352eabee955741110a293c908ceeb Mon Sep 17 00:00:00 2001 From: bison Date: Wed, 24 Aug 2016 13:38:47 +0200 Subject: [PATCH] - Added element email_send_immediately in configuration array in mail.php - Renamed sendOne to sendEmail - Added new method sendOne - Added $message_id parameter to method getMessages of model Recipient_model --- application/config/mail.php | 1 + application/libraries/MessageLib.php | 133 +++++++++++++++++- application/models/system/Recipient_model.php | 9 +- 3 files changed, 137 insertions(+), 6 deletions(-) diff --git a/application/config/mail.php b/application/config/mail.php index 59a77755a..8774642b0 100644 --- a/application/config/mail.php +++ b/application/config/mail.php @@ -10,6 +10,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["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 diff --git a/application/libraries/MessageLib.php b/application/libraries/MessageLib.php index b783c77c3..bfc91a62e 100644 --- a/application/libraries/MessageLib.php +++ b/application/libraries/MessageLib.php @@ -275,7 +275,7 @@ class MessageLib $this->ci->db->trans_complete(); - if ($this->ci->db->trans_status() === FALSE || (is_object($result) && $result->error != EXIT_SUCCESS)) + if ($this->ci->db->trans_status() === false || (is_object($result) && $result->error != EXIT_SUCCESS)) { $this->ci->db->trans_rollback(); return $this->_error($result->msg, EXIT_ERROR); @@ -325,12 +325,13 @@ class MessageLib { // Parses template text $parsedText = $this->ci->vorlagelib->parseVorlagetext($result->retval[0]->text, $data); + $subject = $result->retval[0]->subject; $this->ci->db->trans_start(false); // Save Message $msgData = array( "person_id" => $sender_id, - "subject" => $result->retval[0]->subject, + "subject" => $subject, "body" => $parsedText, "priority" => PRIORITY_NORMAL, "relationmessage_id" => $relationmessage_id, @@ -356,12 +357,23 @@ class MessageLib "status" => MSG_STATUS_UNREAD ); $result = $this->ci->MsgStatusModel->insert($statusData); + + // 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) + { + // Send message by email! + $result = $this->sendOne($msg_id, $subject, $parsedText); + } + } } } $this->ci->db->trans_complete(); - if ($this->ci->db->trans_status() === FALSE || (is_object($result) && $result->error != EXIT_SUCCESS)) + if ($this->ci->db->trans_status() === false || (is_object($result) && $result->error != EXIT_SUCCESS)) { $this->ci->db->trans_rollback(); return $this->_error($result->msg, EXIT_ERROR); @@ -451,7 +463,7 @@ class MessageLib } // Sending email - $sent = $this->sendOne( + $sent = $this->sendEmail( $sender, $result->retval[$i]->receiver, $result->retval[$i]->subject, @@ -533,6 +545,117 @@ class MessageLib return $sent; } + + /** + * One messages from DB and sends it via email + */ + public function sendOne($message_id, $subject = null, $body = null) + { + $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, + null, + null, + $message_id + ); + // Checks if errors were occurred + if (is_object($result) && $result->error == EXIT_SUCCESS) + { + // If data are present + if (is_array($result->retval) && count($result->retval) > 0) + { + // If the person has an email account + if (!is_null($result->retval[0]->receiver) && $result->retval[0]->receiver != "") + { + // Using a template as email body if it is not given as method parameter + if (is_null($body)) + { + $bodyMsg = $this->ci->parser->parse("templates/mail", array("body" => $result->retval[0]->body), true); + if (is_null($bodyMsg) || $bodyMsg == "") + { + // $body = $result->retval[0]->body; + $this->ci->loglib->logError("Error while parsing the mail template"); + } + } + else + { + $bodyMsg = $body; + } + + // If the sender kontakt does not exist, then use system + $sender = $this->getEmailCfgItem("email_from_system"); + if (!is_null($result->retval[0]->sender) && $result->retval[0]->sender != "") + { + $sender = $result->retval[0]->sender; + } + + // Sending email + $sent = $this->sendEmail( + $sender, + $result->retval[0]->receiver, + is_null($subject) ? $result->retval[0]->subject : $subject, // if parameter subject is not null, use it! + $bodyMsg + ); + // If errors were occurred while sending the email + if (!$sent) + { + $this->ci->loglib->logError("Error while sending an email"); + // Writing errors in tbl_message_status + $sme = $this->setMessageError( + $result->retval[0]->message_id, + $result->retval[0]->receiver_id, + "Error while sending an email", + $result->retval[0]->sentinfo + ); + if (!$sme) + { + $this->ci->loglib->logError("Error while updating DB"); + } + } + else + { + // Setting the message as sent in DB + $sent = $this->setMessageSent($result->retval[0]->message_id, $result->retval[0]->receiver_id); + // If the email has been sent and the DB updated + if (!$sent) + { + $this->ci->loglib->logError("Error while updating DB"); + } + } + } + else + { + $this->ci->loglib->logError("This person does not have an email account"); + // Writing errors in tbl_message_status + $sme = $this->setMessageError( + $result->retval[0]->message_id, + $result->retval[0]->receiver_id, + "This person does not have an email account", + $result->retval[0]->sentinfo + ); + if (!$sme) + { + $this->ci->loglib->logError("Error while updating DB"); + } + $sent = true; // Non blocking error + } + } + else + { + $this->ci->loglib->logInfo("There are no email to be sent"); + $sent = false; + } + } + else + { + $this->ci->loglib->logError("Something went wrong while getting data from DB"); + $sent = false; + } + + return $sent; + } // ------------------------------------------------------------------------ // Private Functions from here out! @@ -594,7 +717,7 @@ class MessageLib /** * Sends a single email */ - private function sendOne($from, $to, $subject, $message, $alias = "", $cc = null, $bcc = null) + private function sendEmail($from, $to, $subject, $message, $alias = "", $cc = null, $bcc = null) { $this->ci->email->from($from, $alias); $this->ci->email->to($to); diff --git a/application/models/system/Recipient_model.php b/application/models/system/Recipient_model.php index 6e6352c93..136156cde 100644 --- a/application/models/system/Recipient_model.php +++ b/application/models/system/Recipient_model.php @@ -21,8 +21,9 @@ class Recipient_model extends DB_Model * @param kontaktType specifies the type of the kontakt to get * @param sent specifies the status of the messages to get (NULL never sent, otherwise the shipping date) * @param limit specifies the number of messages to get + * @param message_id specifies a single message */ - public function getMessages($kontaktType, $sent, $limit = null) + public function getMessages($kontaktType, $sent, $limit = null, $message_id = null) { // Check rights if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz("public.tbl_msg_recipient"), "s")) @@ -59,6 +60,12 @@ class Recipient_model extends DB_Model $query .= " WHERE mr.sent = ?"; } + if (!is_null($message_id)) + { + array_push($parametersArray, $message_id); + $query .= " AND mm.message_id = ?"; + } + $query .= " ORDER BY mr.insertamum ASC"; if (!is_null($limit))