Can I email to all customers with open orders?

Have checked, re-checked, and re-checked-checked the forums for a solution, but no. So pardon me if this is redundant.



Can I, through stock CS, mod, or code changes, email all customers who have open orders?



We ship live plants, and a one-time bulk email to all open order customers would be fantastic to announce shipping times or to ask them to respond to a specific question.



Thanks for any advice!

I haven’t seen anything like this yet. It would make a very nice and handy addition.

mdekok - No kidding.



It might or might not be an extensive/expensive mod, but I was also wondering if there was a quick and dirty way to do it. (It’s a little tedious cut/pasting each email address from a 3 page list of orders. Oy.)



Anyone?

we’ve been asking for a better newsletter system in cs forever, hopefully we’ll see better improvements in the next version…

sno - I could even manage with the existing newsletter function, I could just add each email and send out a newsletter.



EXCEPT, that when I add a new email address to the subscriber list, CS sends a “welcome/thank you” email to that address… I don’t want to p!$$ off/confuse the open order customers with unnecessary mail and hear, “I didn’t subscribe to your newsletter!!!”



Anyone else? I would also entertain paying for such a mod…

this should do it (off the top of my head, so might have error or two)


$subject = "SUBJECT";
$message = "MESSAGE";
$from = "YOUR@EMAIL";
$headers = "From: $from";

$result = mysql_query("SELECT * FROM cscart_orders WHERE status='O'");
while($row = mysql_fetch_array($result))
{

$to = $row['email'];
mail($to,$subject,$message,$headers);

}
?>

awsom - I thank you very much for this, sort of what I was thinking. I would love to test this, but where and how do I execute such code?

You would create a new php page with this code in it and create a link to it. You will also need to add the php code to connect to the database.



Below is a php class I wrote for database queries. Put it in a file called “classes.php”.



```php

class database {
public $db = false;

function __construct($db_host,$db_user,$db_pass,$db_name) {
$connection = mysql_connect($db_host,$db_user,$db_pass);
if($connection) {
$dbase = mysql_select_db($db_name,$connection);
if($dbase) {
$this->db = true;
return true;
} else {
$this->db = false;
return false;
}
} else {
$this->db = false;
return false;
}
}

function select($fields, $tableName, $where="", $groupby="", $orderby="", $limit="", $showerrors=false) {
if (!$this->db) {
$data = array();
if ($showerrors) {
echo "Unable to select database.
\n";
}
} else {
$fieldCount = count($fields);
$sql = "SELECT " . implode(",",$fields) . " FROM $tableName" . (($where == "")?"":" WHERE $where") . (($groupby == "")?"":" GROUP BY $groupby") . (($orderby == "")?"":" ORDER BY $orderby") . (($limit == "")?"":" LIMIT " . $limit);
$result = mysql_query($sql);
if (!$result) {
$data = array();
if ($showerrors) {
echo "Sorry, could not get " . $tableName . " at this time.
\n";
}
} else {
if (mysql_num_rows($result) == 0) {
$data = array();
if ($showerrors) {
echo "Sorry, there are no " . $tableName . " at this time, please try back later.
\n";
}
} else {
$n = 0;
while($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
for ($j=0; $j<$fieldCount; $j++) {
$field = $fields[$j];
if(substr_count($field,"AS") > 0) {
$field = preg_replace('/.+AS[^A-Z]+([A-Z_]+)[^A-Z]{0,1}/i', '\1', $field);
}
$data[$n][$j] = str_replace("

\r","


\r",$row[$field]);
}
$n = $n + 1;
}
}
}
}
return $data;
}
}
?>

```

In the same folder, put another file "notify_open.php" in the same folder as "classes.php". Give it this modified version of AWSOM50's code.

```php
include "classes.php";

$database = new database("host","user","pass","db_name");

$subject = "SUBJECT";
$message = "MESSAGE";
$from = "YOUR@EMAIL";

$fields = array("email");
$result = $database->select($fields,"cscart_orders","status='O'");
for($i=0;$i $to = $result[$i][0];
mail($to,$subject,$message,"From: $from");
}
?>
```

mdekok - Wow, clearly I am in over my head. But I’ll do my best and see if I can test… (have you tested?)



What folder would be appropriate for these two files, or doesn’t it matter as I would just be calling these up from a browser?



Thank you to all for this ongoing work…

It’s the same class I use for the My Mods link below in my signature.



You can place them in any folder. All you need to do is link to the “notify_open.php” file or simply call it up.



Also if you wish to include more info from the order, like customer’s name, in the body of the message. Here’s how.



```php

include "classes.php";

$database = new database("host","user","pass","db_name");

$subject = "SUBJECT";
$message = "Hello [firstname], ...";
$from = "YOUR@EMAIL";

$fields = array("email","firstname");
$result = $database->select($fields,"cscart_orders","status='O'");
for($i=0;$i $to = $result[$i][0];
$message = str_replace("[firstname]",$result[$i][1],$message);
mail($to,$subject,$message,"From: $from");
}
?>

```

oh yeah, i forgot to connect to the DB in my code. But I agree with Mdekok - you should do this in an external file rather than screwing around with the PHP in cscart, or it will come back to haunt you when you try to upgrade.



If you don’t want to use that classes.php you can just put this at the start of any external php file you write. (just after the




$username=“DBUSERNAME”;

$password=“PW”;

$database=“DBNAME”;

mysql_connect(localhost,$username,$password);

mysql_select_db($database) or die( “Unable to select database”);

Here is my final resulting code for the notify_open.php file: (TESTED)



```php

include "classes.php";

//start editing here

//enter your db information
$database = new database("host","user","pass","db_name");

//enter maximum wait time before actions are taken
$max_wait_before_message = 7;
$max_wait_before_decline = 14;

//enter message info for action 1
$subject1 = "You Have an Open Order";
$message1 = "Hello [firstname],\n\n";
$message1 .= "My records show that you placed an order that has been open (placed, but not paid for) for $max_wait_before_message days. If you wish to cancel the order now, please respond to let me know. If you feel that there has been a mistake, I will need some proof of purchase and that the order has been processed (paid for). This proof could be a PayPal email address, for example. Using this information, I can check for any payments from that account since the date the order was placed. Any failure to respond with the next $max_wait_before_decline days will result in your order being declined.\n________________________\n\n";
$message1 .= "Thank you,\nMatt DeKok\nYouTip Gaming";

//enter message info for action 2
$subject2 = "Order Declined";
$message2 = "Hello [firstname],\n\n";
$message2 .= "My records show that you've had an order open (placed, but not paid for) for " . ($max_wait_before_message + $max_wait_before_decline) . " days. This order has been declined for failure to pay or respond to previous message.\n\n";
$message2 .= "If you still feel that there has been a mistake, I will need some proof of purchase and that the order has been processed (paid for). This proof could be a PayPal email address, for example. Using this information, I can check for any payments from that account since the date the order was placed.\n________________________\n\n";
$message2 .= "Thank you,\nMatt DeKok\nYouTip Gaming";

//enter remaining email information
$from = "YOUR@EMAIL";

//stop editing here.

$fields = array("email","firstname","timestamp","order_id");
$result = $database->select($fields,"cscart_orders","status='O'");
$n = 0;
for($i=0;$i $n++;
$timediff = floor((time() - $result[$i][2]) / (24 * 60 * 60));
$to = $result[$i][0];
if($timediff > $max_wait_before_message - 1 && $timediff < $max_wait_before_message + $max_wait_before_decline) {
echo "Sending email to $to.";
$message = str_replace("[firstname]",$result[$i][1],$message1);
mail($to,$subject1,$message,"From: $from");
echo " Message sent.
\n";
} elseif($timediff > $max_wait_before_message + $max_wait_before_decline - 1) {
echo "Declining order #" . $result[$i][3] . ".";
$return = $database->query("UPDATE cscart_orders SET status='D' WHERE order_id = " . $result[$i][3]);
echo " Order declined.
\n";
echo "Notifying customer.";
$message = str_replace("[firstname]",$result[$i][1],$message2);
mail($to,$subject2,$message,"From: $from");
echo " Message sent.
\n";
}
}
if($n > 0) {
echo "
$n messages sent.
";
} else {
echo "No orders have been open for $max_wait_before_message days.
";
}
?>

Close Window

```

MANY THANKS! I have no time now to test for my uses, but I will not forget your time… I will keep in touch.

…just letting you know I haven’t forgotten… getting this error, though :



Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in classes.php on line 3 ?



But I don’t have much time to look into it right now…

Hmm, can’t understand why. It’s the exact same code I’m using, I just changed the db information and then copied and pasted it into the WYSIWYG editor on this forum.