<?php
include("class.phpmailer.php");
include("class.smtp.php");
//for posting of internal messages
function postmessage($subject,$body,$userid,$groupid,$eventid)
{
$query = "INSERT INTO message (FromUserID, ToUserID, Subject, Body, IsRead, MessageType, IsSent, GroupID, ReminderID, EventID, Created) VALUES (0, $userid, '".$subject."', '".$body."', 0, 0, 0, ".$groupid.", 0, ".$eventid.", NOW())";
mysql_query($query) or die('Error, insert query failed');
$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error, insert query failed');
$query = "UPDATE reminder set Complete = TRUE where eventid = ".$eventid;
$result = mysql_query($query) or die('Error, insert query failed');;
}
function sendemail($subject,$body,$from,$to,$email,$messageid, $eventid)
{
$mail=new PHPMailer();
$mail->SMTPDebug = 2;
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port
$mail->Username = ""; // GMAIL username
$mail->Password = ""; // GMAIL password
$mail->From = "";
$mail->FromName = "";
$mail->Subject = $subject;
$mail->Body = $body; //HTML Body
$mail->AltBody = $body; //Text Body
$mail->WordWrap = 50; // set word wrap
$mail->AddAddress($email,$to);
//$mail->AddReplyTo("hide@address.com","Webmaster");
//$mail->AddAttachment("/path/to/file.zip"); // attachment
//$mail->AddAttachment("/path/to/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent to ".$to." at: ".$email;
//if message sends correctly, update message table that email has been sent
if ($messageid>0)
{
setassent($messageid);
}
if ($eventid>0)
{
$query = "UPDATE reminder set Complete = TRUE where eventid = ".$eventid;
$result = mysql_query($query);
}
}
}
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("calendarix",$dbhandle)
or die("Could not select database");
echo "\nLoop1 Send";
//main processing loop
for($i=0;$i<10;$i++)
{
//stop the loop hogging processor time
sleep(2);
//query to select both message to send and get sender and recipient details
standardemail();
invitations();
reminders();
internalreminders();
smsmessage();
}
function invitations()
{
$query = "SELECT touserid, eventid , messageid
FROM message
WHERE messagetype =3
and issent =0 ";
$result = mysql_query($query);
while($row = mysql_fetch_object($result))
{
sendinvite($row->touserid, $row->eventid,$row->messageid);
}
}
function setassent($messageid)
{
$query = "update message set IsSent = 1 where messageid = ".$messageid;
mysql_query($query);
$query = "FLUSH PRIVILEGES";
mysql_query($query);
}
function sendinvite($touser, $eventid,$messageid)
{
$query = "select username, pref, email, phone from calendar_users where user_id =".$touser;
$result = mysql_query($query);
$row = mysql_fetch_object($result);
$username = $row->username;
$email = $row->email;
$phone = $row->phone;
$pref = $row->pref;
$query = "SELECT starttime, endtime, day, month, year, title, description
FROM calendar_events
WHERE id =".$eventid;
$result = mysql_query($query);
$row = mysql_fetch_object($result);
if($pref == 0)
{
$description="Dear ".$username.",<br><br>".
"This is an invitation to the event: ".$row->title."<br>\n".
"This event is on: ".$row->day."/".$row->month."/".$row->year."<br>\n";
if($row->starttime <> '')
$description .= "This event will start at: ".$row->starttime;
if($row->starttime <> '' && $row->endtime <> '')
$description .= " and end at: ".$row->endtime;
$description .= "<br><br>\n";
$description .= "Please reply to this email with YES or NO on the first line to indicate whether you will be attending this event or not.\n".
"<br>The first line of your reply will be posted to the event.".
"<br><br>Details of the event are below:<br><br>\n".$row->description.
"<br><br>EventID='".$eventid."'";
sendemail("Invitation To Event: ".$row->title,$description,"Calendar Invitations",$username,$email,$messageid,0);
}
else if($pref == 1)
{
$description="Dear ".$username.
", You are invited to event: ".substr($row->title, 0, 30).
" On: ".$row->day."/".$row->month."/".$row->year;
if($row->starttime <> '')
$description .= " At: ".$row->starttime;
if($row->starttime <> '' && $row->endtime <> '')
$description .= " to: ".$row->endtime;
$description .= " Please reply with YES or NO and ID:".$eventid." to vote.";
sendemail("Invitation To Event: ".$row->title,$description,"Calendar Invitations",$username,$phone."@messagenet.com.au",$messageid,0);
}
}
function standardemail()
{
$query = "SELECT subject, body, fr.username fruname, tou.username touname, tou.email, messageid
FROM (message inner join calendar_users fr on ToUserID = fr.user_id)
inner join calendar_users tou on ToUserID = tou.user_id
where (MessageType = 1 or MessageType = 2) and IsSent = 0";
$result = mysql_query($query);
if(!$result)
mysql_error();
//$rows = mysql_num_rows($result);
if($result)
{
while($row = mysql_fetch_object($result))
{
echo "\n".$row->subject."\n".$row->body."\n".$row->fruname."\n".$row->touname."\n".$row->email;
sendemail("Message: ".$row->subject,$row->body,$row->fruname,$row->touname,$row->email,$row->messageid,0);
}
}
}
function smsmessage()
{
$query = "SELECT subject, body, tou.username touname, tou.email, messageid, tou.phone phone
FROM message inner join calendar_users tou on ToUserID = tou.user_id
where (MessageType = 4) and IsSent = 0";
$result = mysql_query($query);
if(!$result)
mysql_error();
//$rows = mysql_num_rows($result);
if($result)
{
while($row = mysql_fetch_object($result))
{
echo "\n".$row->subject."\n".$row->body."\n".$row->touname."\n".$row->email;
sendemail("SMS reminder",$row->body,"Calendar Reminders",$row->touname,$row->phone."@messagenet.com.au",$row->messageid,0);
}
}
}
function reminders()
{
$currtdate = date("YmdHi");
//query to send reminder messages via email
$query = "SELECT calendar_events.day, calendar_users.pref, reminder.eventid, calendar_events.month, calendar_events.year, calendar_users.email, calendar_users.phone, calendar_events.starttime, calendar_events.endtime, calendar_events.title, calendar_events.description, concat( reminder.year, reminder.MONTH, reminder.DAY, reminder.ReminderTime ) AS tdate, reminder.eventid, groups.groupid, groups.groupname, vote.decision
FROM (
(
(
(
reminder
INNER JOIN calendar_events ON calendar_events.id = reminder.eventid
)
INNER JOIN groupusers ON calendar_events.cat = groupusers.groupid
)
INNER JOIN calendar_users ON groupusers.userid = calendar_users.user_id
)
INNER JOIN groups ON calendar_events.cat = groups.groupid
)
INNER JOIN vote ON calendar_events.id = vote.eventid and calendar_users.user_id = vote.userid
where ('".$currtdate."' >= concat(reminder.year, reminder.month, reminder.day, reminder.ReminderTime)) and Complete = false and reminder.ReminderType = 1 and vote.decision = 1";
$result = mysql_query($query);
if($result)
{
while($row = mysql_fetch_object($result))
{
echo "\n".$row->tdate." ctd".$currtdate." Reminder for:".$row->email.$row->phone;
if(isset($row->email) && $row->email != '')
{
if($row->pref == 0)
{
//processing for email reminder
$description="This is a reminder for the event: ".$row->title."<br>\n".
"This event is on: ".$row->day."/".$row->month."/".$row->year."<br>\n".
"And is for the group: ".$row->groupname."<br>\n".
"<br>Details of the event are below:<br><br>\n".$row->description.
"<br>EventID='".$row->eventid."'";
sendemail("Reminder for Event:".$row->title,$description,"Industrial Project Reminders",$row->email,$row->email,0,$row->eventid);
}
}
if(is_numeric($row->phone))
{
if($row->pref == 1)
{
//processing for SMS reminder here
$description="This is a reminder for the event: ".substr($row->title, 0, 30).
" This event is on: ".$row->day."/".$row->month."/".$row->year.
" And is for the group: ".$row->groupname;
sendemail("Reminder for Event: ".$row->title,$description,"Calendar Reminders","Reminder",$row->phone."@messagenet.com.au",0,$row->eventid);
}
}
}
}
}
function internalreminders()
{
$currtdate = date("YmdHi");
//reminder for internal messages
$query = "SELECT reminder.eventid, calendar_events.day, groupusers.userid, calendar_events.month, calendar_events.year, calendar_users.email, calendar_users.phone, calendar_users.username, calendar_events.starttime, calendar_events.endtime, calendar_events.title, calendar_events.description, concat( reminder.year, reminder.MONTH, reminder.DAY, reminder.ReminderTime ) AS tdate, eventid, groups.groupid, groups.groupname
FROM (
(
(
reminder
INNER JOIN calendar_events ON calendar_events.id = reminder.eventid
)
INNER JOIN groupusers ON calendar_events.cat = groupusers.groupid
)
INNER JOIN calendar_users ON groupusers.userid = calendar_users.user_id
)
INNER JOIN groups ON calendar_events.cat = groups.groupid
where (concat(reminder.year, reminder.month, reminder.day, reminder.ReminderTime) <= '".$currtdate."') and Complete = false and reminder.ReminderType = 0";
$result = mysql_query($query);
if($result)
{
while($row = mysql_fetch_object($result))
{
echo "\n".$row->tdate." ctd".$currtdate." Internal reminder for:".$row->username;
$description="This is a reminder for the event: <a href=\"cal_popup.php?op=view&id=".$row->eventid."\">".$row->title."</a><br>
This event is on: ".$row->day."/".$row->month."/".$row->year."<br>
And is for the group: ".$row->groupname."<br>
Details of the event are below:<br><br>".$row->description;
postmessage("Reminder for Event:".$row->title,$description,$row->userid,$row->groupid,$row->eventid);
}
}
}
?>