# Auto responder script installation questions



## flagman (Feb 4, 2008)

Hi - I have installed Media Autoresponder script on my server at Hostgator.

No problems with the install or database setup and have already logged into the Cpanel for the script but there were a few details to tie up that I am not exactly sure of and could use some advice.

Here are the final questions as posed by the install instructions and I will post my questions below each one :


6) Add to cron job php responder/robot/check_deliver_broadcast_messages.php once at a minute

"MY QUESTION" - I am not sure where in the file to add "once in a minute" as suggested. I will include a copy of the entire file between dashed lines below. I see in there that there is already a setting set at 60 seconds but am unsure if that is what they are talking about or not.

Start copy of "check_deliver_broadcast_messages.php" below
--------------------------------------------------------------
<?php
// Sends broadcast messages
// Set CRON for each 5 minutes
// Written by Michel Komarov http://icoder.com (2005-12-13)
function error_page($error_message) { return "$error_message"; } 
$bunchSize = 300;
$bunchPause = 60; //sec
$bunchInterval = 600; //sec (from CRON timing)
$cron_file = "../temp/broadcast.cron";
if (file_exists($cron_file)) {
$cron = file_get_contents($cron_file);
if ((5 * $bunchInterval) + $cron > time()) exit;
}
if ($fh = fopen($cron_file, "w")) {
fputs($fh, time());
fclose($fh);
}

require("../include/globals.php");
require("../include/db_mysql.php");
require("../include/phpmailer.php");
require("../include/mail.php");

set_time_limit(0);
$db = new DB_Sql;
$db2 = new DB_Sql;

$queryWhere = "FROM broadcastmessages
WHERE 
broadcastmessages_delivery_date <= NOW() AND
broadcastmessages_sent = '0' ";
$db->query("SELECT * $queryWhere");
if (($count = $db->num_rows()) > 0) {
if ($fh = fopen("../temp/broadcast.log", "a")) {
fputs($fh, "\n".date("Y-m-d H:i:s")." Messages: $count\n");
fclose($fh);
}
}
$msg = 0;
while ($db->next_record()) {
if (0 == ((++$msg) % $bunchSize)) {
if ($fh = fopen("../temp/broadcast.log", "a")) {
fputs($fh, "\t$msg sent\n");
fclose($fh);
 }
if ($fh = fopen($cron_file, "w")) {
fputs($fh, time());
fclose($fh);
}
sleep($bunchPause);
}
$broadcast_message_id = $db->f("broadcastmessages_id");

$error = SendBroadcastMessage($broadcast_message_id);
if ($error) {
if ($fh = fopen("../temp/broadcast.log", "a")) {
fputs($fh, " $error.");
fclose($fh);
}
}

$db2->query("UPDATE broadcastmessages
SET broadcastmessages_sent = '1'
WHERE broadcastmessages_id = '$broadcast_message_id'");
}
unlink($cron_file);
if ($count > 0) {
if ($fh = fopen("../temp/broadcast.log", "a")) {
fputs($fh, "\t$msg Sent.\n");
fclose($fh);
}
}
?>
-------------------------------------------------------------------
End copy of "check_deliver_broadcast_messages.php"


7) Add to cron job php responder/robot/check_deliver_followup_messages.php once at one day

"MY QUESTION" - Same as above, please tell me from the file details below where and how to enter the "once at one day" info.

Start copy of "check_deliver_followup_messages.php" below
--------------------------------------------------------------
<?php
// Sends follow-up and instant messages
// Set CRON for each 5 minutes
// Written by Michel Komarov http://icoder.com (2005-12-13)
function error_page($error_message) { return $error_message; }
$bunchSize = 300;
$bunchPause = 60; //sec
$bunchInterval = 600; //sec (from CRON timing)
$cron_file = "../temp/followup.cron";
if (file_exists($cron_file)) {
$cron = file_get_contents($cron_file);
if ((5 * $bunchInterval) + $cron > time()) exit;
}
if ($fh = fopen($cron_file, "w")) {
fputs($fh, time());
fclose($fh);
}

require("../include/globals.php");
require("../include/db_mysql.php");
require("../include/phpmailer.php");
require("../include/mail.php");

class DB_Sql_Log extends DB_Sql {
var $Write_Log = false;
var $Halt_On_Error = "report";
function haltmsg($msg) {
if ($this->Write_Log && ($fh = fopen("../temp/followup.log", "a"))) {
fputs($fh, "Database error: $msg\nMySQL Error: {$this->Errno} ($this->Error)\n");
fclose($fh);
}
}
}

set_time_limit(0);
$db = new DB_Sql_Log;
$db2 = new DB_Sql;
$query = "SELECT messages_id, subscribers_id, subscribers_email, messages_interval
FROM messages, subscribers, users
WHERE 
users_id = messages_user_id AND
users_id = subscribers_user_id AND
subscribers_active = '1' AND
subscribers_trouble_mailing < 2 AND
messages_interval > subscribers_messages_interval AND
TO_DAYS(subscribers_join_date + INTERVAL messages_interval DAY) = TO_DAYS(NOW()) ";
$db->query("$query ORDER BY messages_interval");
if (0 == $db->Errno && ($count = $db->num_rows()) > 0) {
if ($fh = fopen("../temp/followup.log", "a")) {
fputs($fh, "\n".date("Y-m-d H:i:s")." Messages: $count\n");
fclose($fh);
}
$db->Write_Log = true;
$msg = 0;
while ($db->next_record()) {
if (0 == ((++$msg) % $bunchSize)) {
if ($fh = fopen("../temp/followup.log", "a")) {
 fputs($fh, "\t$msg sent\n");
fclose($fh);
}
if ($fh = fopen($cron_file, "w")) {
fputs($fh, time());
fclose($fh);
}
sleep($bunchPause);
}
$message_id = $db->f("messages_id");
$subscriber_id = $db->f("subscribers_id");
$subscriber_email = $db->f("subscribers_email");
$messages_interval = $db->f("messages_interval");

$error = SendMessage2Subscriber($message_id, $subscriber_id);
if ($error) {
if ($fh = fopen("../temp/followup.log", "a")) {
fputs($fh, date("\tCan't send $subscriber_email\n"));
fclose($fh);
}
}

$db2->query("UPDATE subscribers
SET subscribers_messages_interval='$messages_interval'
WHERE subscribers_id='$subscriber_id'");
}
}
unlink($cron_file);
if ($count > 0) {
if ($fh = fopen("../temp/followup.log", "a")) {
fputs($fh, "\t$msg Sent.\n");
fclose($fh);
}
}
?>
--------------------------------------------------------------
End copy of "check_deliver_followup_messages.php" 


8) Add to cron job php responder/robot/check_instant_messages.php once at a minute 


"MY QUESTION" - Same as above, please tell me from the file details below where and how to enter the "once at a minute" info.

Start copy of "check_instant_messages.php" below
--------------------------------------------------------------
<?php
// scriptul se executa la fiecare 1 minut
// si verifica daca in INBOX (pt account-ul [email protected]{sitename})
// au sosit mesaje destinate utilizatorilor (de ex: [email protected]{sitename})
// daca da, atunci inregistram persoana care a trimis mail ca si 
// un subscriber pt utilizatorul "myuser", si in trimitem toate mesajele instante
function error_page($error_message) { } 

require("imap_globals.php");
require("imap_functions.php");
require("../include/globals.php");
require("../include/db_mysql.php");
require("../include/phpmailer.php");
require("../include/mail.php");

set_time_limit(0);
$db = new DB_Sql;

$message_box = imap_open("{".$MailHost.":110/pop3}INBOX", $MailUser, $MailPassword);

if ($message_box != false)
{
$message_number = imap_num_msg($message_box);
$headers = getHeaders($message_box, 1, $message_number);

// parcurgem mesajele
for ($i=1; $i<=$message_number; $i++)
{
$subject = $headers[$i][2];
$uid = $headers[$i][6];
$from_name = $headers[$i][1];

// extragem numai numele (fara adresa de mail)
$pos = strpos($from_name, "<");

if ($pos === false)
$from_name = $from_name;
else
$from_name = substr($from_name, 0, $pos);

$head = imap_fetchheader($message_box, $uid, FT_UID);
$header_info = imap_headerinfo($message_box, $uid, FT_UID);

$from = $header_info->from;
$to = $header_info->to;

foreach ($from as $id => $object)
{
$from_address = $object->mailbox . "@" . $object->host;
}

foreach ($to as $id => $object)
{
$to_address = $object->mailbox . "@" . $object->host;
$to_user = $object->mailbox;
}

$body = imap_body($message_box, $uid, FT_INTERNAL);

// vedem daca mesajul este adresat vreunui user al aplicatiei
 $query = "SELECT * 
FROM users 
WHERE users_username = '$to_user' ";
$db->query($query);

if ($db->num_rows() != 0)
{
// da, exista un user caruia i se adreseaza mailul primit
$db->next_record();
$user_id = $db->f("users_id");

// verificam daca nu avem deja un subscriber cu email-ul dat
// pt user-ul caruia i s-a trimis email
$query = "SELECT * 
FROM subscribers
WHERE 
subscribers_user_id = '$user_id' AND
subscribers_email = '$from_address' ";
$db->query($query);

if ($db->num_rows() != 0)
{
// gasim id-ul subscrisului
$db->next_record();
$subscriber_id = $db->f("subscribers_id");
}
else
{ 
// inregistram persoana care a trimis mail 
// ca subscriber al user-ului caruia ii era destinat mail-ul
$_head = addslashes($head);
$query = "INSERT INTO subscribers (
subscribers_name,
subscribers_email,
subscribers_user_id,
subscribers_header,
subscribers_join_date
) VALUES (
'$from_name',
'$from_address',
'$user_id',
'$_head',
NOW()
)";
$db->query($query);

// gasim id-ul subscrisului numai ce adaugat
$subscriber_id = mysql_insert_id($db->link_id());
}

// trimitem toate mesajele instante ale user-ului subscrisului 
$query = "SELECT * 
FROM messages 
WHERE 
messages_user_id = '$user_id' AND
messages_interval = '0' AND
messages_disabled = '0' ";
$db->query($query);

while ($db->next_record())
{
SendMessage2Subscriber($db->f("messages_id"), $subscriber_id);
}

// stergem mesajul
imap_delete ($message_box, $uid, FT_UID);
}
}

imap_expunge($message_box);
imap_close($message_box);
}

?>
--------------------------------------------------------------
End copy of "check_instant_messages.php"


One more question. I am assuming that the cron jobs were set up when the install script ran because I haven't actually had to set any up according to their instructions. Does that sound right?

I sure appreciate the help

Regards
flagman


----------



## carsey (Aug 19, 2006)

8) Add to cron job php responder/robot/check_instant_messages.php once at a minute 


Ive only touched Crons a few times but you should need to enter this in the CRON tab in cPanel.

*php responder/robot/check_instant_messages.php*

Then below you will see some options, these relate to your timings of when the script is to run.


----------



## flagman (Feb 4, 2008)

Thanks carsey :smile:

Will give it a shot and post my results

flagman


----------



## carsey (Aug 19, 2006)

Cheers, keep us update


----------



## flagman (Feb 4, 2008)

Got everything working now. Found a tutorial online at

thealexangroup.com/other/MARInstallationManual.pdf

It explains in detail. Hope this helps others having trouble with this script.

Regards


----------



## carsey (Aug 19, 2006)

Great, Glad you found something to help you. :smile:


----------

