<?php include ("_website_init.php"); 
// line 40 and line 99 to toggle test email address?>
<?php  list($accountsRecords, $accountsMetaData) = getRecords(array(
    'tableName'   => 'accounts',
  ));
?>
<?php $color = $dbt_colorsRecord['main_box_background_color'] ?>
<?php foreach($accountsRecords as $record) : ?>
<?php 
@$updateUnixTime     = strtotime( @$record['updatedDate'] ); // seconds since 1970 
@$sixMonths = time() - (180*60*60*24) ; // (changed from + to - )
@$sixMonthsGone = @$updateUnixTime > @$sixMonths;

?>
<?php  if(!@$record['isAdmin'] && ( $sixMonthsGone) ): // live?>
<?php // if(!@$record['isAdmin']): // test?>
<?php 
 @$secondsOld = time() - strtotime($record['updatedDate']); // seconds since updatedDate
  @$daysOld    = intval(($secondsOld/60/60/24)-180); // converts seconds to days (changed from + to -)
 // $first_reminder_day    = intval(($common_informationRecord['first_reminder_day']+180)); // adjusts for 6 month lead time
  // $second_reminder_day    = intval(($common_informationRecord['second_reminder_day']+180)); // adjusts for 6 month lead time
  @$first_reminder_day = $common_informationRecord['first_reminder_day'] ;
 @$second_reminder_day = $common_informationRecord['second_reminder_day'] ;
  @$first_reminder_message = $common_informationRecord['first_reminder_message']; // 8 calendar days
  @$second_reminder_message = $common_informationRecord['second_reminder_message']; // 30 calendar days
   @$practiceName = $record['practice_name'] ;
  @$firstName = $record['contact_first_name'] ;
    @$lastName = $record['contact_last_name'] ;?>
<?php $expires = date("F jS", strtotime($record['updatedDate'])) ; ?>
<?php // ************** START SET PROVIDER VARIABLES AND OUTPUT **************************  ?>
<?php ob_start(); // start capturing output ?>
<?php $first_reminder_message = wordwrap( $first_reminder_message, 70); ?>
<?php echo $first_reminder_message ?>
<?php $output = ob_get_clean(); // stop capturing output  ?>
<?php ob_start(); // start capturing output ?>
<?php $second_reminder_message = wordwrap( $second_reminder_message, 70); ?>
<?php echo $second_reminder_message ?>
<?php $output2 = ob_get_clean(); // stop capturing output  ?>
<?php //************** END SET PROVIDER VARIABLES AND OUTPUT ************************** ?>
<?php  if(!@$record['isAdmin']  && (@$daysOld == -@$first_reminder_day)):  ?>

<?php  // **************DETERMINE PROVIDER SATUS ******************* ?>
<?php  @$the_to = $record['email'];  ?>

<?php 
 
 $message = <<<EOF
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type content='text/html; charset=utf-8" />
<style type="text/css">
.special {font-family:Arial; color:  rgb(0,0,0); font-size: 1.0em; font-weight: bold; text-decoration: underline;}
.text_font {font-family:Arial; color: rgb(0,0,0); font-size: 1.0em;}
.heading_font {font-family:Arial; color: rgb(0,0,0); font-size: 1.3em;}
.heading_font_blue {font-family:Arial; color: rgb(0,0,255); font-size: 1.3em;}
</style>
</head>
<body bgcolor="#$color">
<table style='background-color: #$color;' width='100%' align='center' border='0' >
			  <tr>
    <td align='left' >
	<table style='background-color: #$color;' width='70%' align='center' border='0' >
			  <tr>
    <td align='left' >
 <div align='left'><img src='http://www.dbtproviders.com/images/DBT_email_masthead.png' style='border:hidden; width:489px; height:88px;'/></div><br /><br />
 <div align='left' class='text_font'>
  Hello $firstName $lastName,
  <br /><br />

 $output \r\n<br /><br />
  Best,
  <br /><br />
  The DBT Provider Directory Team<br />
\r\n\r\n</div>
  </td>
    </tr>
	</table>
	</td>
    </tr>
	</table>
  </body>
</html>
EOF;
 $the_from  = "no-reply@dbtproviders.com";
 $the_subject = "A DBT Provider profile manditory update for $practiceName is due about $first_reminder_day days from now. If you do not update your profile it will be hidden from the DBT Provider directory.";

$mailArray = array( 
'to' => $the_to,
'from' => $the_from,
'subject' => $the_subject,
'html'         => $message 
); 
$errors =  sendMessage($mailArray);
// delay execution by .2 sec
usleep(200000);
// reset max_execution_time to 30 sec
set_time_limit(30);
?>
<?php endif ;?>
<?php  if(!@$record['isAdmin']  && (@$daysOld == -@$second_reminder_day)):  ?>
<?php   $the_toa = $record['email'];  ?>

<?php 
 $message2 = <<<EOF
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type content='text/html; charset=utf-8" />
<style type="text/css">
.special {font-family:Arial; color:  rgb(0,0,0); font-size: 1.0em; font-weight: bold; text-decoration: underline;}
.text_font {font-family:Arial; color: rgb(0,0,0); font-size: 1.0em;}
.heading_font {font-family:Arial; color: rgb(0,0,0); font-size: 1.3em;}
.heading_font_blue {font-family:Arial; color: rgb(0,0,255); font-size: 1.3em;}
</style>
</head>
<body bgcolor="#$color">
<table style='background-color: #$color;' width='100%' align='center' border='0' >
			  <tr>
    <td align='left' >
	<table style='background-color: #$color;' width='70%' align='center' border='0' >
			  <tr>
    <td align='left' >
 <div align='left'><img src='http://www.dbtproviders.com/images/DBT_email_masthead.png' style='border:hidden; width:489px; height:88px;'/></div><br /><br />
 <div align='left' class='text_font'>
  Hello $firstName $lastName,
  <br /><br />
<span class='heading_font_blue'>This is your final courtesy reminder.</span>
  <br /><br />
  <span class='heading_font'>You must verify/update the information in your DBT Provider profile within the next 10 days, or your listing will be hidden from the DBT Provider Directory.</span><br /><br />
 $output2 \r\n<br /><br />
  Best,
  <br /><br />
 The DBT Provider Directory Team<br />
\r\n
 </div>
  </td>
    </tr>
	</table>
	</td>
    </tr>
	</table>
  </body>
</html>
EOF;
 $the_from  = "no-reply@dbtproviders.com";
 $the_subject = "A DBT Provider profile manditory update for $practiceName is due about $second_reminder_day days from now. If you do not update your profile it will be hidden from the DBT Provider directory.";

$mailArray = array( 
'to' => $the_toa,
'from' => $the_from,
'subject' => $the_subject,
'html'         => $message2 
); 
$errors =  sendMessage($mailArray);
// delay execution by .2 sec
usleep(200000);
// reset max_execution_time to 30 sec
set_time_limit(30);
?>
<?php endif ?>
<?php endif ;?>

<?php endforeach ?>
 <?php
    $crontext = "Reminder Run at ".date("r")." by ".$_SERVER['USER']."\n" ;
    $folder = substr($_SERVER['SCRIPT_FILENAME'],0,strrpos($_SERVER['SCRIPT_FILENAME'],"/")+1);
    $filename = $folder."cron_test.txt" ;
    $fp = fopen($filename,"a") or die("Open error!");
    fwrite($fp, $crontext) or die("Write error!");
    fclose($fp);
    echo "Wrote to ".$filename."\n\n" ;
    ?>
