Files
Testing/include/class_he_mass_mailing.php

247 lines
9.9 KiB
PHP
Raw Permalink Normal View History

2026-11-17 16:15:01 -05:00
<?php
if( !class_exists('he_mass_mailing') ) {
/**
* Mass mailing tools is intended to mass messaging and mass emailing. Also you can create campaigns for them easily manage them via this class methods.
*
* @author Eldar
* @copyright Hire-Experts LLC
* @version Mass Mailing 1.01
*/
class he_mass_mailing
{
var $cron_period;
/**
* Constructor
* @return object
*/
function __construct()
{
$this->cron_period = 60;
$this->check_db_structure();
}
/**
* Creates campaign and return its ID. Admin is -1;
* @param $subject
* @param $message
* @param $sender
* @return int OR false
*/
function create_campaign($subject, $message, $sender, $is_email = true)
{
if( !strlen($message) || !intval($sender) ) return false;
he_database::query(he_database::placeholder("INSERT INTO se_he_mass_mailing_campaign SET subject='?', message='?', sender=?, sent_time=UNIX_TIMESTAMP(), is_email=?", $subject, $message, intval($sender), intval($is_email)));
return he_database::insert_id();
}
/**
* Returns campaign
* @param $id
* @return array
*/
function get_campaign($id)
{
return he_database::fetch_row(he_database::placeholder("SELECT * FROM se_he_mass_mailing_campaign WHERE id=?", intval($id)));
}
/**
* Returns campaingns
* @param $from
* @param $count
* @return array of array
*/
function get_campaigns($from, $count)
{
return he_database::fetch_array(he_database::placeholder("SELECT * FROM se_he_mass_mailing_campaign ORDER BY sent_time DESC LIMIT ?, ?", intval($from), intval($count)));
}
/**
* Adds a message into queue for user
* @param $campaign_id
* @param $recipient
* @param array $replace
* @return int OR false
*/
function add_message_into_queue_for_user($campaign_id, $recipient, $replace = array())
{
$replace_str = ( $replace && is_array($replace) ) ? serialize($replace) : '';
he_database::query(he_database::placeholder("INSERT INTO se_he_mass_mailing_queue SET campaign_id=?, recipient=?, `replace`='?'", intval($campaign_id), intval($recipient), $replace_str));
return he_database::insert_id();
}
/**
* Adds a message into queue for email. Recipient's full name is optional
* @param $campaign_id
* @param string $email
* @param string $full_name
* @param array $replace
* @return int OR false
*/
function add_message_into_queue_for_email($campaign_id, $email, $full_name='', $replace = array())
{
$replace_str = ( $replace && is_array($replace) ) ? serialize($replace) : '';
he_database::query(he_database::placeholder("INSERT INTO se_he_mass_mailing_queue SET campaign_id=?, email='?', full_name='?', `replace`='?'", intval($campaign_id), $email, $full_name, $replace_str));
return he_database::insert_id();
}
/**
* Deletes message from queue
* @param $id
* @return boolean
*/
function delete_message_from_queue($id)
{
he_database::query(he_database::placeholder("DELETE FROM se_he_mass_mailing_queue WHERE id=?", intval($id)));
return ( he_database::affected_rows() );
}
/**
* Sends messages from queue
* @param $count
* @return void
*/
function send_messages_from_queue($count)
{
global $setting;
$count = intval($count) ? intval($count) : 10;
$campaigns = array();
$senders = array();
$messages = he_database::fetch_array("SELECT * FROM se_he_mass_mailing_queue ORDER BY id LIMIT $count");
foreach ($messages as $message)
{
if( !$this->delete_message_from_queue($message['id']) ) continue;
//get campaign
if( !$campaigns[$message['campaign_id']] )
$campaigns[$message['campaign_id']] = $this->get_campaign($message['campaign_id']);
$campaign = $campaigns[$message['campaign_id']];
//get sender if it is not email and sender is not admin
if( !$campaign['is_email'] && $campaign['sender'] ) {
if( !$senders[$campaign['sender']] )
$senders[$campaign['sender']] = new se_user(array($campaign['sender'], ''));
$sender = $senders[$campaign['sender']];
if( !$sender->user_exists ) continue;
}
//get recipient
$recipient = null;
if( $message['recipient'] ) {
$recipient = new se_user(array($message['recipient'], ''));
if( !$recipient->user_exists ) continue;
}
else {
if( !$message['email'] ) continue;
}
//set subject and message
$subject = $message['replace'] ? vsprintf($campaign['subject'], unserialize($message['replace'])) : $campaign['subject'];
$body = $message['replace'] ? vsprintf($campaign['message'], unserialize($message['replace'])) : $campaign['message'];
//send email/message
if( $campaign['is_email'] )
{
$mail = new PHPMailer();
$body.="
BateWorld sent this e-mail to you at ".$message['email']." because your Notification Preferences indicate that you want to receive this information.
<br /><br />
If you do not wish to receive further communications like this you can change your Notification Preferences by <a href='http://bateworld.com/user_account_privacy.php'>clicking here</a>. Please note that it may take up to 10 days to process your request and you must be logged into your account. Alternatively, you can disable your BateWorld account by <a href='http://bateworld.com/user_account_delete.php'>clicking here</a>.
<br /><br />
This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you are not the named addressee or you believe you have received this email by mistake, you should not disseminate, distribute or copy this email. Please notify the sender immediately by email if you have received this email by mistake and delete this email from your system. If you are not the intended recipient, you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.<br /><br />
Visit our <a href='https://bateworld.com/privacy-policy/'>Privacy Policy</a> and <a href='https://bateworld.com/terms-of-service/'>Terms of Service</a> if you have any questions.";
$mail->CharSet = 'UTF-8';
$mail->From = $setting['setting_email_fromemail'];
$mail->FromName = $setting['setting_email_fromname'];
$mail->Subject = $subject;
$mail->AltBody = $body;//'To view the message, please use an HTML compatible email viewer.';
$mail->MsgHTML($body);
if( $recipient )
$mail->AddAddress($recipient->user_info['user_email'], $recipient->user_displayname);
else
$mail->AddAddress($message['email'], $message['full_name']);
$mail->Send();
}
else {
if( !$recipient->user_exists || !$sender->user_exists ) continue;
$sender->user_message_send($recipient->user_info['user_username'], $subject, $body);
}
}
}
/**
* Should be called by cron or in header file
* @return void
*/
function cron()
{
global $setting;
if( $setting['he_mass_mailing_last_execute'] > time()-$this->cron_period ) return;
$setting['he_mass_mailing_last_execute'] = time()+5;
he_database::query("UPDATE se_settings SET he_mass_mailing_last_execute={$setting['he_mass_mailing_last_execute']}");
$this->send_messages_from_queue($setting['he_mass_mailing_limit_per_execute']);
}
/**
* Checks if module has db structure and if it is needed upgrade or not
*
* @return void;
*/
function check_db_structure()
{
global $settings;
$file_version = $this->get_version();
//check if db structure is ready
if( !$settings['he_mass_mailing_version'] )
{
/*if( !he_database::fetch_row("SHOW COLUMNS FROM se_settings LIKE 'he_mass_mailing_version'") )
{
he_database::query("ALTER TABLE `se_settings` ADD `he_mass_mailing_version` int(10) NOT NULL DEFAULT $file_version");
he_database::query("ALTER TABLE `se_settings` ADD `he_mass_mailing_last_execute` int(10) NOT NULL");
he_database::query("ALTER TABLE `se_settings` ADD `he_mass_mailing_limit_per_execute` int(10) NOT NULL DEFAULT 30");
he_database::query("CREATE TABLE `se_he_mass_mailing_campaign` (`id` int(11) NOT NULL AUTO_INCREMENT,`subject` varchar(255) NOT NULL,`message` text NOT NULL,`sender` int(10) NOT NULL,`sent_time` int(10) unsigned NOT NULL,`is_email` tinyint(1) NOT NULL,PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
he_database::query("CREATE TABLE `se_he_mass_mailing_queue` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`campaign_id` int(11) NOT NULL,`recipient` int(10) NOT NULL,`email` varchar(64) NOT NULL,`full_name` varchar(128) NOT NULL,`replace` text NOT NULL,PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
$settings['he_mass_mailing_version'] = $file_version;
}*/
}
//check db and file version
if( $file_version > $settings['he_mass_mailing_version'] )
{
//db version is older than file so we have to upgrade db version
switch( $settings['he_mass_mailing_version'] )
{
case 101:
break;
}
}
}
/**
* Returns module version
* @return int
*/
function get_version()
{
return 101;
}
}
}
?>