Your IP : 216.73.217.79


Current Path : /var/www/cesa.co.za/Mailer/
Upload File :
Current File : /var/www/cesa.co.za/Mailer/sendMIS.php

<?php
// ini_set('display_errors', 1);
// error_reporting(E_ALL);
// echo "Disabled";
// exit;

require_once($_SERVER['DOCUMENT_ROOT'] . '/Mailer/includes/globals.php');
set_time_limit(0);
error_reporting(E_ALL); // optional for debugging
require_once($_SERVER['DOCUMENT_ROOT'] . '/Mailer/includes/XPertMailer.php');

// ************** CHANGE THESE EACH TIME ******************
$subject_heading = "CESA Bi-Annual Economic and Capacity Report - January to June 2026";
$filename = $_SERVER['DOCUMENT_ROOT'] . "/misletter.html";
$fromaddress = "ann-marie@cesa.co.za";

//	$subject_heading = "CESA - Compulsory Completion of 2015 Annual Declaration";
//	$filename = "../adecletter.html";
//	$fromaddress = "gillian@cesa.co.za";

/**
  Validate an email address.
  Provide email address (raw input)
  Returns true if the email address has the email
  address format and the domain exists.
 */
function isEmail($email)
{
    $isValid = true;
    $atIndex = strrpos($email, "@");
    if (is_bool($atIndex) && !$atIndex) {
        $isValid = false;
    } else {
        $domain = substr($email, $atIndex + 1);
        $local = substr($email, 0, $atIndex);
        $localLen = strlen($local);
        $domainLen = strlen($domain);
        if ($localLen < 1 || $localLen > 64) {
            // local part length exceeded
            $isValid = false;
        } else if ($domainLen < 1 || $domainLen > 255) {
            // domain part length exceeded
            $isValid = false;
        } else if ($local[0] == '.' || $local[$localLen - 1] == '.') {
            // local part starts or ends with '.'
            $isValid = false;
        } else if (preg_match('/\\.\\./', $local)) {
            // local part has two consecutive dots
            $isValid = false;
        } else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
            // character not valid in domain part
            $isValid = false;
        } else if (preg_match('/\\.\\./', $domain)) {
            // domain part has two consecutive dots
            $isValid = false;
        } else if (!preg_match(
            '/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
            str_replace("\\\\", "", $local)
        )) {
            // character not valid in local part unless
            // local part is quoted
            if (!preg_match(
                '/^"(\\\\"|[^"])+"$/',
                str_replace("\\\\", "", $local)
            )) {
                $isValid = false;
            }
        }
    }
    return $isValid;
}

function latermail($rdb, $sTo, $MsgID, $sMessage)
{
    // log mail in database for later sending
    $sql = "INSERT INTO ToSend (SendTo, MessageID, Message)
		VALUES ('" . addslashes($sTo) . "', '" . $MsgID . "', '" . addslashes($sMessage) . "')";
    mysqli_query($rdb, $sql);
}

///*
$timestamp = time(); //$rsN->fields[3];
$thisDate = strftime('%d %B %Y', $timestamp);
$thisYear = strftime('%Y', $timestamp);
//*/
/*
  $thisYear = $rsN->fields[8];//strftime('%Y', $timestamp);
  $thisDate = $rsN->fields[7] . ' ' .$thisYear; //strftime('%B %Y', $timestamp);
 */
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
    <title>Email template control panel</title>
    <link rel="stylesheet" type="text/css" href="stylesheet/default_styles.css">
</head>

<body bgcolor="#D0ECF8" marginheight="0" marginwidth="0" topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">
    <table border="0" cellspacing="0" cellpadding="0" width="100%">
        <tr>
            <td height="10" bgcolor="#016694"><img src="' . $images . 'spacer.gif" width="1" height="10"></td>
        </tr>
        <tr>
            <td height="2" bgcolor="#FFFFFF"><img src="' . $images . 'spacer.gif" width="1" height="2"></td>
        </tr>
        <tr>
            <td height="2" bgcolor="#016694"><img src="' . $images . 'spacer.gif" width="1" height="2"></td>
        </tr>
        <tr>
            <td height="30"><img src="' . $images . 'spacer.gif" width="1" height="30"></td>
        </tr>

        <tr>
            <td class="paddingLeft20">
                <table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
                    <tr>
                        <td>
                            <hr>
                        </td>
                    </tr>
                    <tr>
                        <td height="10"></td>
                    </tr>
                    <tr>
                        <td class="blackTypeBold"><strong>Newsletters sent to the following people:</strong><br><br></td>
                    </tr>

                    <?php
                    $ms = '';
                    $fin = fopen($filename, 'r');

                    while (!feof($fin)) {
                        $ms .= fgets($fin);
                    }

                    //$mail = new XPertMailer(SMTP_RELAY_CLIENT, 'smtp.mweb.co.za');
                    //$mail = new XPertMailer(SMTP_RELAY_CLIENT, 'smtp-relay.za24.co.za');
                    //	$mail = new XPertMailer();
                    //	$mail->port(25);
                    //	$mail->from('siphom@saace.co.za');
                    // To send HTML mail, the Content-type header must be set
                    $headers = 'MIME-Version: 1.0' . "\r\n";
                    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                    //$headers .= 'From: saace-bounces@xe.co.za' . "\r\n";
                    $headers .= 'From: ' . $fromaddress . "\r\n";
                    //	$headers .= 'From: kuvashneeg@cesa.co.za' . "\r\n";

                    include_once($_SERVER['DOCUMENT_ROOT'] . "/cfg/config.php");
                    $rdbmail = mysqli_connect(EW_CONN_HOST, MAILDB_USER, MAILDB_PASS, MAILDB_DB);

                    if (isset($_GET['test']) && ($_GET['test'] == 1)) {
                        $rdb = $rdbmail;
                    } else {
                        $rdb = mysqli_connect(EW_CONN_HOST, EW_CONN_USER, EW_CONN_PASS, EW_CONN_DB);
                    }

                    // Create message in database
                    $sql = "INSERT INTO Messages (SentToGroup, DateSent, MessageFrom, MessageSize, Subject, Message, Options) "
                        . "VALUES ('', '" . date("Y-m-d H:i:s") . "', '', '', '" . addslashes($subject_heading) . "', '" . addslashes($ms) . "', '" . addslashes(json_encode(array('from-address' => $fromaddress, 'from-name' => 'CESA BECS', 'isHTML' => true))) . "')";
                    //                        echo $sql;
                    //                        exit;
                    mysqli_query($rdbmail, $sql);
                    $messageid = mysqli_insert_id($rdbmail);

                    if (isset($_GET['test']) && ($_GET['test'] == 1)) {
                        $sqlrecipients = "SELECT Email, 567 AS FirmID FROM Members";
                        $sqlrecipients .= " WHERE idMember=11 "
                            . "UNION SELECT 'ann-marie@cesa.co.za' AS Email, 0 AS FirmID "
                            . "UNION SELECT 'cesa-becs@igotafrica.com' AS Email, 0 AS FirmID ";
                    } else {
                        // Mandated principals
                        $sqlrecipients = "SELECT distinct TRIM(MC.Email) as Email, MF.FirmID
		FROM tblMemberFirms MF
		INNER JOIN (tblMemberFirmOffices MFO
			INNER JOIN MemberContacts MC ON MFO.OfficeID = MC.OfficeID)
		ON MF.FirmID = MFO.FirmID
			WHERE
			MC.Email Is Not Null AND MC.Mail = 0 AND
			TRIM(MC.Email) <> '' and
			(MC.Email like '%@%.%') and
			((MF.DeclarationReceived > '2005-12-31' AND MF.MemberStatus = 'Member')) AND
			(MC.MandatedPrincipal=1)";
                        //			UNION SELECT 'chris@cesa.co.za' AS Email, 0 AS FirmID";
                    }
                    //                        die($sqlrecipients);
                    $i = 0;
                    $rsRec = mysqli_query($rdb, $sqlrecipients) or die(mysqli_error($rdb));
                    $send = true;
                    $arrInvalid = array();
                    $arrValid = array();
                    echo '<tr><td valign="top">';
                    echo '<strong>Sending to: </strong>';
                    while ($rsR = mysqli_fetch_assoc($rsRec)) {
                        $newms = str_replace("**FIRMID**", $rsR["FirmID"], $ms);

                        $arrEmails = explode(";", str_replace(",", ";", $rsR["Email"]));
                        foreach ($arrEmails as $key => $value) {
                            $value = strtolower(trim($value));
                            if (isEmail($value) && !in_array($value, $arrValid)) {
                                $arrValid[] = $value;
                                latermail($rdbmail, $value, $messageid, $newms);
                                echo ', ' . $value;
                                $i++;
                                // wait for a quarter second
                                //usleep(250000);
                            } elseif (!in_array($value, $arrValid)) {
                                $arrInvalid[] = $value;
                            }
                        }
                    }
                    echo " - <strong>Done.</strong>";
                    echo '</td></tr>';

                    echo '<tr><td><br/><br/><strong>Sent to ' . $i . ' people</strong><br/>';
                    echo '<br/><br/><strong>NOT SENT to ' . count($arrInvalid) . ' people with INVALID EMAIL addresses, listed below: </strong><br/>' . implode(", ", $arrInvalid);
                    echo '</td></tr>';
                    ?>
                </table>
            </td>
        </tr>

    </table>
</body>

</html>