Your IP : 216.73.217.79


Current Path : /var/www/cesa.co.za/crons/
Upload File :
Current File : /var/www/cesa.co.za/crons/checkmail.php

<?php
error_reporting(E_ALL); // php errors
define('DISPLAY_XPM4_ERRORS', true); // display XPM4 errors
//define('LOG_XPM4_ERRORS', serialize(array('type' => 1, 'destination' => 'errors@domain.tld', 'headers' => 'From: xpm4@domain.tld'))); // <- send mail
//define('LOG_XPM4_ERRORS', serialize(array('type' => 3, 'destination' => '/var/tmp/XPM4.log'))); // <- append file

	require_once ('../inc/auth.inc');
// path to 'MIME.php' file from XPM4 package
require_once '../XpertMailer/MIME4.php';
	require_once("class.email-reader.php");

/**
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;
}

$nl="<br/>\n";
$report = TRUE;
 
// saace-ypf	bb6m4y95
// saace-principals	od1gq7px

$site=array();
$site['email_address']  = "saace-members@xe.co.za";
$site['email_name']     = "SAACE Members";
$site['pop_host']       = "pop.xe.co.za";
$site['host']           = "smtp.xe.co.za";
$site['username']       = "saace-members@xe.co.za";
$site['password']       = "6y134k3u";
$site['authentication'] = TRUE;
$site['folder']         = "temp/";
 
$reader=new email_reader_class();
$reader->set_account($site['pop_host'], $site['username'], $site['password']);
$ok=$reader->connect();
if($ok)  // if connected
{
	$count=$reader->get_count();
if($report) echo $count.' emails on server<br/>';
	if($count>0)  // if emails to process
	{
		$other_emails=TRUE;
		while($other_emails)  // step through emails
		{
			$processed=FALSE;
			$head=$reader->get_header();
			$from_address = trim($head['from'][0]['mailbox']) . "@" . trim($head['from'][0]['host']);
if($report) echo 'From: '.$from_address.'<br/>';
			$subject = $head['subject'];
			
			if (eregi("@xe.co.za", $from_address) || eregi("@saace.co.za", $from_address))
			{
				$message_id="";
				if(isset($head['message_id'])) $message_id=$head['message_id'];
if($report) echo 'ID: '.$message_id.'<br/>';
				if(!empty($message_id))
				{
						$body=trim($reader->get_body());
						$attachments=$reader->get_attachments();

				//Create outgoing mail
				// set text/plain version of message
				$text = MIME4::message(strip_tags($body), 'text/plain');
				// set text/html version of message
				$html = MIME4::message($body, 'text/html');
				// add attachments
				$at = array();
						while(list($key,$value)=each($attachments))
						{
							if(is_dir($site['folder']))
							{
								$filename=$site['folder'].$value['filename'];
								$filehandle=fopen($filename, "ab");
								fwrite($filehandle, $value['filedata']);
								fclose($filehandle);
								$at[] = MIME4::message($value['filedata'], FUNC4::mime_type($filename), $value['filename'], null, 'base64', 'inline',$value['id']);
							}
						}
				
				// compose mail message in MIME format
				$mess = MIME4::compose($text, $html, $at);

				// Send email
				
				// 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: '. $from_address . "\r\n";
				
				// All members
				/*$sqlrecipients = "SELECT distinct TRIM(MC.Email) as Email
				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') OR
					(MF.MemberStatus = 'retired member') Or
					(MF.MemberStatus = 'honorary member'))";
					*/
				$sqlrecipients = "SELECT 'julia@xe.co.za' AS Email";
				
				$db = mysql_connect($loghost,$logid,$logpwd);
				
				mysql_select_db($dbname,$db);
				
				$icount = 0;
				$j = 0;
				$batch = 0;
				$rsRec = mysql_query($sqlrecipients);
				$send = true;
				$arrInvalid = array();
				while ($rsR = mysql_fetch_assoc($rsRec)) {
				
					$recipient = $rsR["Email"];
					if (isEmail($recipient)) {
				
						// send mail
						$send = mail($recipient, $subject, $mess['content'], 'From: '.$from_address."\n".$mess['header']);
						$icount++;
						// wait for a quarter second
						usleep(250000);
					} else {
						$arrInvalid[] = $recipient;
					}
				
					$j++;
					
					if ($j >= 500) {
						// wait for 10 seconds
						sleep(10);
						$j = 0;
						$batch++;
					}
					
				}
				
				mail($from_address, "Bulk email status", $icount . " email messages sent", $headers);

				}
				if($processed)
				{
					$other_emails=$reader->delete_current_email(); // <- when live
				}
				else
				{
					$other_emails=$reader->next_email(); // <- when testing
				}
			}
			else
			{
				$other_emails=$reader->next_email();
			}
		}
	}
	else
	{
		echo "No emails to process".$nl;
	}
	$reader->disconnect(); 
}
else
{
	echo $reader->error;
	echo $nl;
}
 


?>