1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
<?
/*
* 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 "<br> 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 "<br> 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 "<br> 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 "<br> Deletes Complete";
} catch (Exception $exc) { /* Ignore... :| */ }
//Release Lock
releaseEmailLock($lock);
//TODO !!
//echo "<br>I DIDN'T SEND THIS: ($body, $subject, $to)<br>";
//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);
}
?>
|