Nicholas Cheek

LITTLE ROCK WEB DEVELOPER

<?php

//when there is time, we need to set the notify to a different way because it's
//not recognizing if there are more than one entry with multiple emails, which will
//cause the rating method to mess up.  We set all old records to 1 at the end of the
//script but there has to be a more elegant way of doing it. -nick


set_time_limit(0);                   // ignore php timeout
ignore_user_abort(true);             // keep on going even if user pulls the plug*
$con=mysqli_connect("your connection info");

//-------------------------------------------------
//
// Check connection
//
//------------------------------------------------

	if (mysqli_connect_errno()) {
	  echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}

//-------------------------------------------------
//
// Include Mandrill Files to route emails
//
//------------------------------------------------

	require '/path/to//Mandrill.php';
	$mandrill = new Mandrill('MANDRILLAPICODEHERE');

//-------------------------------------------------
//
// Get list of past orders from the database 
// (remove limit before it goes live)
//------------------------------------------------
 	$result = mysqli_query($con, "Select notified,x.status,x.entry_date AS 'Order Date', c.field_id_47 AS Fname, c.field_id_48 AS Lname, c.field_id_33  AS email, o.order_id, Group_concat(DISTINCT o.entry_id) AS Products,group_concat(DISTINCT t.title),group_concat(DISTINCT t.url_title) from finn_cartthrob_order_items o LEFT JOIN finn_channel_titles x ON x.entry_id=o.order_id LEFT JOIN finn_channel_data c ON c.entry_id=o.order_id LEFT JOIN finn_channel_titles t ON o.entry_id=t.entry_id  WHERE c.field_id_33 IS NOT NULL AND notified is null AND c.field_id_33 NOT IN('email1','email2') AND x.status NOT IN ('Refunded','Failed','Canceled','RA Issued','Offsite') GROUP BY email");

//-------------------------------------------------
//
// loop through to get results of each
//
//------------------------------------------------
	
	while($row = mysqli_fetch_array($result)) {
	
//-------------------------------------------------
//Setup date check and only do the ones that are 7
// days old in order to make sure they get the
// product they've ordered.  On the test script
//it would echo out the email and show which are older
//-------------------------------------------------
	
	if ($row['Order Date'] <= strtotime("-7 days")) {

//-------------------------------------------------
//
// Break down each product, getting details
//
//------------------------------------------------
 
	$products = explode(",", $row['Products']);

//-------------------------------------------------
//
// Loop and explode products since most people have
// more than one order, including the stylesheet and
// formatting it in to a table for asthetics
//
//------------------------------------------------

  $mymessage = "<head><link rel='stylesheet' type='text/css' href='https://www.domain.com/themes/site_themes/newfin/style.css?" .  rand ( 10000 , 99999 )."' /><body>";
  $mymessage .= "<table class='prods' cellpadding='3' cellspacing = '5'>";
  $mymessage .= "<tr><td  colspan='3'><u>Your Past Purchases</u></td></tr>";
    foreach($products as $item){
     $mymessage .= "<tr class='prod'>"; 
	  $yourprod = mysqli_query($con,"Select t.entry_id,title,url_title,field_id_76,field_id_12 from finn_channel_titles t LEFT JOIN finn_channel_data d ON t.entry_id=d.entry_id WHERE t.entry_id = '$item'");
  		while($prod = mysqli_fetch_array($yourprod)) {
  $mymessage .= "<td style='padding-left:15px;'>";				

//-------------------------------------------------
//
// If the first image isn't there, get the second
// and remove {file_dir} from in front (11 chars)
//------------------------------------------------
  	
  $fiximg = substr($prod['field_id_12'], 11);
  	if (strlen($prod['field_id_76']) > 0){
  		$mymessage .= "<img src='https://domain.com/product_images/" . $prod['field_id_76'] . "' style='width:50px;' />";
  	} else {
  		$mymessage .= "<img src='https://domain.com/product_images/" . $fiximg . "' style='width:50px;' />";
  	}
  $mymessage .= "</td>";
  $mymessage .= "<td>";
//-------------------------------------------------
//
// Give a link to the product and set the rating
// stars and hyperlinks.
//
//------------------------------------------------		
  $mymessage .= "<a href='https://domain.com/detail/" . $prod['url_title'] . "'>" . $prod['title'] . "</a>";
  $mymessage .= "</td>";  	
  $mymessage .= "<td style='padding-left:15px;'><a href='https://domain.com/detail/" . $prod['url_title'] ."#rating'><div class='myrating'><span>☆</span><span>☆</span><span>☆</span><span>☆</span><span>☆</span></div></a></td>";

  $mymessage .= "</tr>"; 		
  			
  			} //end while loop
  		} //end foreach loop
 
  $mymessage .= "</table>";  
  $mymessage .= "<br><br>";
  $mymessage .= "</body></html>"; 

//-------------------------------------------------
//
// Use mandrills api to route each email, sending
// through them first, using their arrays
//
//------------------------------------------------
  $name = $row['Fname'] . " " . $row['Lname'];
    //uncomment for live mode
  $emailto = $row['email'];
  //$emailto = 'nick@nicholascheek.com'; //TESTING ONLY, PLEASE DO NOT SEND ME 800 EMAILS!
  $message = array(
    'subject' => 'Love it or Leave it?  Rate Your Purchase!',
    'from_email' => 'orders@domain.com',
    'to' => array(array('email' => $emailto, 'name' => $name)),
    'merge_vars' => array(array(
        'rcpt' => $emailto,
        'vars' =>
        array(
            array(
                'name' => 'FIRSTNAME',
                'content' => 'Recipient 1 first name'),
            array(
                'name' => 'MYMESSAGE',
                'content' => $mymessage),

            array(
                'name' => 'LASTNAME',
                'content' => 'Last name')
    ))));

	$template_name = 'Rating Email';

	$template_content = array(
	    array(
	        'name' => 'main',
	        'content' => 'Hello')	
	  );
	  $response = $mandrill->messages->sendTemplate($template_name, $template_content, $message); //Sends the email
	//  print_r($response); //print the response
	
// Mandrill END
//-------------------------------------------------
//
// Echo out the results so we're not looking at a
// blank page after the script runs.
//
//------------------------------------------------
 
//  echo $mymessage;

//-------------------------------------------------
//
// Mark each item as sent so we don't send duplicate
// emails to everyone. Set order variable to mark.
//
//------------------------------------------------
 
  
  $order = $row['order_id'];  
  echo "<br /><br />";
  //uncomment for live mode
  $markit = mysqli_query($con,"UPDATE finn_channel_data SET notified='1' WHERE entry_id='$order'");
  } //end the 7 day conditional
  } //end initial while loop
  
//-------------------------------------------------
//
// Close our connection
//
//------------------------------------------------
  //uncomment for live mode
  //$setit = mysqli_query($con,"UPDATE finn_channel_data SET notified='1' WHERE notified='0'");
	mysqli_close($con);
	
//	echo "complete";
?> 

						

LITTLE ROCK WEB DEVELOPER

Living the dream.

nick@nicholascheek.com