mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-06-01 20:29:29 +00:00
f21d5a87b1
- application/libraries/MailLib.php now set the default encoding to UTF-8
241 lines
7.3 KiB
PHP
241 lines
7.3 KiB
PHP
<?php
|
|
|
|
if (! defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
use PHPMailer\PHPMailer\SMTP;
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
/**
|
|
* Library to manage the sending of the email
|
|
*/
|
|
class MailLib
|
|
{
|
|
const ENABLE_DEBUG = 'enable_debug';
|
|
|
|
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;
|
|
|
|
private $_ci; // Codeigniter instance
|
|
private $_mail; // PHPMailer instance
|
|
|
|
/**
|
|
* 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);
|
|
|
|
try
|
|
{
|
|
// PHPMailer configuration
|
|
$this->_mail = new PHPMailer(true);
|
|
$this->_mail->isSMTP(); // Send using SMTP
|
|
$this->_mail->Host = $this->_getEmailCfgItem('smtp_host'); // Set the SMTP server to send through
|
|
$this->_mail->Port = $this->_getEmailCfgItem('smtp_port'); // TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
|
|
$this->_mail->SMTPAuth = $this->_getEmailCfgItem('smtp_auth'); // Enable SMTP authentication
|
|
$this->_mail->Username = $this->_getEmailCfgItem('smtp_user'); // SMTP username
|
|
$this->_mail->Password = $this->_getEmailCfgItem('smtp_pass'); // SMTP password
|
|
$this->_mail->SMTPSecure = $this->_getEmailCfgItem('smtp_encryption'); // Enable implicit TLS encryption
|
|
$this->_mail->Timeout = $this->_getEmailCfgItem('smtp_timeout'); // set the timeout (seconds)
|
|
$this->_mail->SMTPKeepAlive = $this->_getEmailCfgItem('smtp_keepalive'); // Persistent SMTP connection
|
|
$this->_mail->Priority = $this->_getEmailCfgItem('priority'); // 1 = High, 3 = Normal, 5 = low. When null, the header is not set at all.
|
|
$this->_mail->WordWrap = $this->_getEmailCfgItem('wordwrap');
|
|
$this->_mail->isHTML($this->_getEmailCfgItem('is_html')); // html or text
|
|
$this->_mail->SMTPDebug = $this->_getEmailCfgItem('enable_debug');
|
|
$this->_mail->CharSet = 'UTF-8';
|
|
|
|
// 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');
|
|
}
|
|
catch(Exception $e)
|
|
{
|
|
error_log($e->errorMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sends a single email
|
|
*/
|
|
public function send($from, $to, $subject, $message, $alias = '', $cc = null, $bcc = null, $altMessage = '', $bulk = false, $autogenerated = false, $embeddedImages = array())
|
|
{
|
|
$result = false;
|
|
|
|
try
|
|
{
|
|
// If it is configured then log mail info into the CI error logs
|
|
if ($this->_getEmailCfgItem(self::ENABLE_DEBUG) === true)
|
|
{
|
|
$this->_ci->load->library('LogLib'); // Loads logging library
|
|
|
|
// Log them all!
|
|
$this->_ci->loglib->logError('From: '.$from);
|
|
$this->_ci->loglib->logError('To: '.$to);
|
|
$this->_ci->loglib->logError('Subject: '.$subject);
|
|
$this->_ci->loglib->logError('Message: '.$message);
|
|
$this->_ci->loglib->logError('Alias: '.$alias);
|
|
$this->_ci->loglib->logError('CC: '.$cc);
|
|
$this->_ci->loglib->logError('BCC: '.$bcc);
|
|
$this->_ci->loglib->logError('Alternative message: '.$altMessage);
|
|
}
|
|
|
|
// If from is not specified then use the standard one
|
|
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;
|
|
}
|
|
}
|
|
if (defined('MAIL_FROM') && MAIL_FROM != '')
|
|
{
|
|
$from = MAIL_FROM;
|
|
if (is_null($alias) || $alias == '')
|
|
{
|
|
$alias = $this->alias_from_system;
|
|
}
|
|
}
|
|
|
|
$this->_mail->setFrom($from, $alias);
|
|
|
|
// Check if the email address of the debug recipient is a valid one
|
|
$recipient = $to;
|
|
$recipientCC = $cc;
|
|
$recipientBCC = $bcc;
|
|
|
|
// If we the an email for debugging has been set
|
|
if (defined('MAIL_DEBUG') && MAIL_DEBUG != '')
|
|
{
|
|
// if is it valid use it!!!
|
|
$recipient = MAIL_DEBUG;
|
|
if ($recipientCC != '') $recipientCC = MAIL_DEBUG;
|
|
if ($recipientBCC != '') $recipientBCC = MAIL_DEBUG;
|
|
}
|
|
|
|
// Recipients
|
|
$this->_mail->addAddress($recipient);
|
|
if (!is_null($recipientCC)) $this->_mail->addCC($recipientCC);
|
|
if (!is_null($recipientBCC)) $this->_mail->addBCC($recipientBCC);
|
|
|
|
// Subject & body & alternative body
|
|
$this->_mail->Subject = $subject;
|
|
$this->_mail->Body = $message;
|
|
if (!isEmptyString($altMessage)) $this->_mail->AltBody = $altMessage;
|
|
|
|
// Custom headers
|
|
if ($bulk) $this->_mail->addCustomHeader('Precedence', 'bulk');
|
|
if ($autogenerated) $this->_mail->addCustomHeader('Auto-Submitted', 'auto-generated');
|
|
|
|
// Embedded images
|
|
foreach ($embeddedImages as $embeddedImage)
|
|
{
|
|
if (!$this->_mail->addEmbeddedImage($embeddedImage['file_name'], $embeddedImage['cid'])) return false;
|
|
}
|
|
|
|
$result = $this->_mail->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 != false)
|
|
{
|
|
$this->_sended++;
|
|
$this->_wait();
|
|
}
|
|
|
|
// Clear the properties for the next message
|
|
$this->_mail->clearAddresses();
|
|
$this->_mail->clearAllRecipients();
|
|
$this->_mail->clearAttachments();
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
error_log($e->errorMessage());
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Overrides configuration parameters
|
|
* If the given parameters are not null or empty strings then they are used to override
|
|
* the following properties of this object:
|
|
* - email_number_to_sent
|
|
* - email_number_per_time_range
|
|
* - email_time_range
|
|
* - email_from_system
|
|
*/
|
|
public function overrideConfigs($numberToSent, $numberPerTimeRange, $emailTimeRange, $emailFromSystem)
|
|
{
|
|
if (is_numeric($numberToSent)) $this->email_number_to_sent = $numberToSent;
|
|
|
|
if (is_numeric($numberPerTimeRange)) $this->email_number_per_time_range = $numberPerTimeRange;
|
|
|
|
if (is_numeric($emailTimeRange)) $this->email_time_range = $emailTimeRange;
|
|
|
|
if (!isEmptyString($emailFromSystem) && filter_var($emailFromSystem, FILTER_VALIDATE_EMAIL))
|
|
{
|
|
$this->email_from_system = $emailFromSystem;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns value of property email_number_to_sent
|
|
*/
|
|
public function getEmailNumberToSent()
|
|
{
|
|
return $this->email_number_to_sent;
|
|
}
|
|
|
|
/**
|
|
* Validates an email address
|
|
*/
|
|
public function validateEmailAddress($emailAddress)
|
|
{
|
|
$valid = false;
|
|
|
|
if (!isEmptyString($emailAddress))
|
|
{
|
|
$valid = filter_var($emailAddress, FILTER_VALIDATE_EMAIL);
|
|
}
|
|
|
|
return $valid;
|
|
}
|
|
|
|
/**
|
|
* Checks if it has to _wait until the sending of the next
|
|
*/
|
|
private function _wait()
|
|
{
|
|
if ($this->_sended == $this->email_number_per_time_range)
|
|
{
|
|
$this->_sended = 0;
|
|
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);
|
|
}
|
|
}
|