/*
* Email methods; QueueEmail and SendQueuedEmail
*/
include_once('sqlEmbedded.php');
//Email sending lock
define('FILE_EMAIL_LOCK', './emailLockFile');
//Returns false if nothing is sent.
function SendQueuedEmail() {
global $mysqli;
//Delay by x seconds per email:
$delay = 10;
$lock = acquireEmailLock();
try
{
//Have we recently sent an email?
//This is phrased so that- if the setting doesn't exist; it wont send.
$res = $mysqli->query("SELECT `value` FROM `settings`
WHERE `name` = 'Email_Last_Sent' AND
`value` < (UNIX_TIMESTAMP(NOW()) - $delay)");
if ($res->fetch_array() == NULL) {
releaseEmailLock($lock);
return false;
}
//echo "
Checking Queue";
//Check the Queue
$res = $mysqli->query("SELECT `emailID`
FROM `emailQueue`
ORDER BY `priority` DESC
LIMIT 1");
$response = $res->fetch_array();
if ($response == NULL) {
releaseEmailLock($lock);
return false;
}
$emailID = $response['emailID'];
//echo "
Email found $emailID";
$res = $mysqli->query("SELECT
`ID`, `fromUserID`, `to`, `subject`, `body`
FROM `emails` WHERE `ID` = '$emailID'
LIMIT 1");
$response = $res->fetch_array();
if ($response == NULL) {
releaseEmailLock($lock);
return false;
}
//echo "
Email Data pulled; Deleting::";
$to = $response['to'];
$subject = $response['subject'];
$body = $response['body'];
$success = SendEmail($body, $subject, $to);
if (!$success) {
$res = $mysqli->query("DELETE FROM `emailQueue` WHERE `ID` = $emailID");
$res = $mysqli->query("UPDATE `emails` SET `error` = 'Failed to send' WHERE `ID` = $emailID");
return false;
}
//I declare this letter sent!
$res = $mysqli->query("DELETE FROM `emailQueue` WHERE `ID` = $emailID");
$res = $mysqli->query("UPDATE `settings` SET `value` = UNIX_TIMESTAMP(NOW()) WHERE `name` = 'Email_Last_Sent'");
$res = $mysqli->query("UPDATE `emails` SET `dateSent` = NOW() WHERE `ID` = $emailID");
//echo "
Deletes Complete";
} catch (Exception $exc) { /* Ignore... :| */ }
//Release Lock
releaseEmailLock($lock);
//TODO !!
//echo "
I DIDN'T SEND THIS: ($body, $subject, $to)
";
//SendEmail($body, $subject, "snapwilliam@gmail.com");
//SendEmail($body, $subject, $to);
return true;
}
function QueueEmail($fromUserID, $to, $subject, $body, $priority = 100, $fromServer = false) {
global $mysqli;
//TODO !! allow a way for users to refer-friends via email. Here.
if ($fromServer == false) {
return false;
//Email tracker is not added !!
$res = $mysqli->query("SELECT `fromUserID` FROM emailTracker WHERE `fromUserID` = '$fromUserID'");
// AND dateSubmited
var_dump($res->fetch_all());
}
$stmt = $mysqli->prepare("INSERT INTO `emails`
(`fromUserID`, `to`, `subject`, `body`)
VALUES (?, ?, ?, ?)");
$stmt->bind_param('isss', $fromUserID, $to, $subject, $body);
$stmt->execute();
$emailID = $stmt->insert_id;
$stmt = $mysqli->prepare("INSERT INTO `emailQueue`
(`emailID`, `priority`)
VALUES (?, ?)");
$stmt->bind_param('ii', $emailID, $priority);
$stmt->execute();
//TODO insert into tracker table
}
function SendEmail($body, $subject = "Pathery", $to = 'snapwilliam@gmail.com', $fromemail = "snap@pathery.com") {
$mycompany = "Pathery";
$replyemail = $fromemail;
# -=-=-=- MIME BOUNDARY
$mime_boundary = "----$mycompany----".md5(time());
# -=-=-=- MAIL HEADERS
$headers = "From: $mycompany <$fromemail>\n";
$headers .= "Reply-To: $mycompany <$replyemail>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
# -=-=-=- TEXT EMAIL PART
$message = "--$mime_boundary\n";
$message .= "Content-Type: text/plain; charset=UTF-8\n";
$message .= "Content-Transfer-Encoding: 8bit\n\n";
$message .= "$body\n";
# -=-=-=- HTML EMAIL PART
//None
# -=-=-=- FINAL BOUNDARY
$message .= "--$mime_boundary--\n\n";
# -=-=-=- SEND MAIL
$mail_sent = @mail( $to, $subject, $message, $headers );
Return $mail_sent;
}
/**
* a file is used to prevent race conditions
*/
function acquireEmailLock() {
$fileHandle = fopen(FILE_EMAIL_LOCK, 'a+');
flock($fileHandle, LOCK_EX);
return $fileHandle;
}
/**
* Releases the lock acquired from acquireEmailLock
*/
function releaseEmailLock($fileHandle) {
if($fileHandle)
flock($fileHandle, LOCK_UN);
}
?>