Integrated a system to send messages via email based on CI

This commit is contained in:
paolo
2016-07-22 16:09:04 +02:00
parent 3ee3282e5a
commit ec4b13f100
5 changed files with 294 additions and 12 deletions
+31
View File
@@ -0,0 +1,31 @@
<?php
// Define constants
define("EMAIL_CONFIG_INDEX", "mail");
define("EMAIL_KONTAKT_TYPE", "email");
// 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";
// 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
// If protocol is set to sendmail
$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
+34
View File
@@ -0,0 +1,34 @@
<?php
/**
* FH-Complete
*
* @package FHC-API
* @author FHC-Team
* @copyright Copyright (c) 2016, fhcomplete.org
* @license GPLv3
* @link http://fhcomplete.org
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MailJob extends FHC_Controller
{
/**
* API constructor
*/
public function __construct()
{
parent::__construct();
// Loads MessageLib
$this->load->library('MessageLib');
}
public function sendMessages($numberToSent = null, $numberPerTimeRange = null, $email_time_range = null)
{
$this->messagelib->sendAll($numberToSent, $numberPerTimeRange, $email_time_range);
}
}
+155 -8
View File
@@ -8,21 +8,34 @@ if (! defined('BASEPATH')) exit('No direct script access allowed');
*/
class MessageLib
{
private $recipients = array();
private $recipients = array(); // not used anymore
public function __construct()
{
$this->ci =& get_instance();
$this->ci->config->load('message');
// 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");
// CI Parser library
$this->ci->load->library("parser");
// Loads VorlageLib
$this->ci->load->library('VorlageLib');
// Initializing email library with the loaded configurations
$this->ci->email->initialize($this->ci->config->config["mail"]);
// 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');
$this->ci->load->library('VorlageLib');
// Loads fhc helper
$this->ci->load->helper('fhc');
//$this->ci->load->helper('language');
@@ -185,7 +198,7 @@ class MessageLib
// ------------------------------------------------------------------------
/**
* add_participant() - adds user to existing thread
* add_participant() - adds user to existing thread - NOT used anymore
*
* @param integer $thread_id REQUIRED
* @param integer $user_id REQUIRED
@@ -425,4 +438,138 @@ class MessageLib
'msg' => lang('message_'.$error)
);
}
}
/**
* 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 sendOne($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 ugly error messages
return @$this->ci->email->send();
}
/**
* Gets all the messages from DB and sends them via email
*/
public function sendAll($numberToSent = null, $numberPerTimeRange = null, $email_time_range = null)
{
$sent = true; // optimistic expectation
// Gets a number (email_number_to_sent) of unread messages from DB
// having EMAIL_KONTAKT_TYPE as relative contact type
$result = $this->ci->MsgStatusModel->getMessages(
EMAIL_KONTAKT_TYPE,
MSG_STATUS_UNREAD,
$this->getEmailCfgItem("email_number_to_sent")
);
// Checks if errors were occurred
if (is_object($result) && $result->error == EXIT_SUCCESS && is_array($result->retval))
{
// If data are present
if (count($result->retval) > 0)
{
// Iterating through the result set, if no errors occurred in the previous iteration
for ($i = 0; $i < count($result->retval) && $sent; $i++)
{
// If the person has an email account
if (!is_null($result->retval[$i]->receiver) && $result->retval[$i]->receiver != "")
{
// Using a template as email body
$body = $this->ci->parser->parse("templates/mail", array("body" => $result->retval[$i]->body), true);
if (is_null($body) || $body == "")
{
// Error while parsing the mail template
// $body = $result->retval[$i]->body;
}
// 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 = $result->retval[$i]->sender;
}
// Sending email
$sent = $this->sendOne(
$sender,
$result->retval[$i]->receiver,
$result->retval[$i]->subject,
$body
);
// If errors were occurred while sending the email
if (!$sent)
{
// Error while sending emails
}
else
{
// Changes the status of the message from unread to read
$resultUpdate = $this->ci->MsgStatusModel->update(
array($result->retval[$i]->message_id, $result->retval[$i]->person_id),
array("status" => MSG_STATUS_READ)
);
// Checks if errors were occurred
if (is_object($resultUpdate) && $resultUpdate->error == EXIT_SUCCESS && is_array($resultUpdate->retval))
{
// Error while updating DB
$sent = false;
}
// If the email has been sent
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
{
// This person does not have an email account
$sent = false;
}
}
}
else
{
// No emails to send
$sent = false;
}
}
else
{
// Something went wrong while getting data from DB
$sent = false;
}
return $sent;
}
}
+56 -4
View File
@@ -1,6 +1,6 @@
<?php
if ( ! defined('BASEPATH'))
exit('No direct script access allowed');
if ( ! defined("BASEPATH")) exit("No direct script access allowed");
class MsgStatus_model extends DB_Model
{
@@ -14,5 +14,57 @@ class MsgStatus_model extends DB_Model
$this->pk = array('message_id', 'person_id');
$this->hasSequence = false;
}
}
/**
* getMessages
*
* Gets all the messages to be sent
*
* @param kontaktType specifies the type of the kontakt to get
* @param messageStatus specifies the status of the messages to get
* @param limit specifies the number of messages to get
*/
public function getMessages($kontaktType, $messageStatus, $limit = null)
{
// Check wrights
if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz('public.tbl_msg_recipient'), 's'))
return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz('public.tbl_msg_recipient'), FHC_MODEL_ERROR);
if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz('public.tbl_msg_message'), 's'))
return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz('public.tbl_msg_message'), FHC_MODEL_ERROR);
if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz('public.tbl_msg_status'), 's'))
return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz('public.tbl_msg_status'), FHC_MODEL_ERROR);
if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz('public.tbl_kontakt'), 's'))
return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz('public.tbl_kontakt'), FHC_MODEL_ERROR);
$query = "SELECT ms.message_id,
ks.kontakt as sender,
kr.kontakt as receiver,
ms.person_id,
mm.subject,
mm.body
FROM public.tbl_msg_status ms INNER JOIN public.tbl_msg_recipient mr USING (message_id)
INNER JOIN public.tbl_msg_message mm USING (message_id)
LEFT JOIN (
SELECT person_id, kontakt FROM public.tbl_kontakt WHERE kontakttyp = ?
) ks ON (ks.person_id = mm.person_id)
LEFT JOIN (
SELECT person_id, kontakt FROM public.tbl_kontakt WHERE kontakttyp = ?
) kr ON (kr.person_id = mr.person_id)
WHERE ms.status = ?";
$parametersArray = array($kontaktType, $kontaktType, $messageStatus);
if (!is_null($limit))
{
$query .= " LIMIT ?";
array_push($parametersArray, $limit);
}
// Get data of the messages to sent
$result = $this->db->query($query, $parametersArray);
if (is_object($result))
return $this->_success($result->result());
else
return $this->_error($this->db->error(), FHC_DB_ERROR);
}
}
+18
View File
@@ -0,0 +1,18 @@
<html>
<head>
<title>This is not the email template, this is a tribute</title>
</head>
<body>
<div class="header">
This is the header!
</div>
<div class="container">
<div class="body">
{body}
</div>
</div>
<div class="footer">
This is the footer!
</div>
</body>
</html>