From f8d48c412670381356f2f28b826a8f9f57e1094e Mon Sep 17 00:00:00 2001 From: Patrick Davison Date: Sat, 9 Feb 2013 14:22:22 -0800 Subject: Emails!! go to /massemail as an admin. --- includes/emails.php | 18 ++- pages/massemail.php | 320 +++++++++++++++++++++++++++++++--------------------- 2 files changed, 205 insertions(+), 133 deletions(-) diff --git a/includes/emails.php b/includes/emails.php index c971f25..182749c 100644 --- a/includes/emails.php +++ b/includes/emails.php @@ -2,17 +2,16 @@ /* * Email methods; QueueEmail and SendQueuedEmail */ - +include_once('sqli.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; - require('sqli.php'); - + $lock = acquireEmailLock(); try { @@ -57,6 +56,12 @@ function SendQueuedEmail() { $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"); @@ -69,14 +74,15 @@ function SendQueuedEmail() { releaseEmailLock($lock); //TODO !! - echo "
I DIDN'T SEND THIS: ($body, $subject, $to)
"; + //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) { - require('sqli.php'); + global $mysqli; //TODO !! allow a way for users to refer-friends via email. Here. if ($fromServer == false) { diff --git a/pages/massemail.php b/pages/massemail.php index d45588b..4bbd26b 100644 --- a/pages/massemail.php +++ b/pages/massemail.php @@ -1,5 +1,9 @@ "; if ($_SESSION['isAdmin'] !== true) return; - - $data = $_POST; - $data['session'] = print_r($_SESSION, true); - $userID = $_SESSION['userID'] * 1; if (!is_int($userID)) return; - include_once "includes/sqlEmbedded.php"; - $sql = "SELECT `email` FROM `users` - WHERE `ID` = '$userID'"; - $result = mysql_query($sql); + //lol because I tink dis is cool... + echo "Validated permissions...
"; + + $data = $_POST; + + //Sanitize + $selections = $_POST['selections']; + foreach ($selections as $selectedID=>&$value) { + $value = intval($selectedID); + if (!is_int($selectedID)) die("Sanitization of userID's failed"); + } + + $in = implode(',', $selections); - $email = mysql_result($result, 0, 'email'); + $sql = "SELECT `id`, `displayName`, `dateJoined`, `dateLogin`, `email` + FROM `users` + WHERE `ID` IN ( $in ) + "; + echo "SQL: $sql
"; - $data['email'] = $email; + $result = mysql_query($sql) or die(mysql_error()); - EmailError($data, "Pathery Feedback"); - DoRedirect("Thank you for your feedback!
- I'm Reading this right now.
- Well, obviously I'm not reading it right NOW.
- I guess what I meant to say is that I'll be reading it soon.
- Wow this redirect is taking a while.. Sorry
- Are you still here?
- Something must be wrong, you should really have been redirected by now", $mydomain, 5); + while (list($tmpID, $tmpDisplay, $tmpJoined, $tmpLastLogon, $tmpEmail) = mysql_fetch_row($result)) { + //Replacements are done in order; + $replacements = array( + '%footer' => $emailFooter, + '%display' => $tmpDisplay, + '%id' => $tmpID, + '%email' => $tmpEmail, + '%mydomain' => $mydomain, + '%joined' => relative_date(strtotime($tmpJoined)), + '%lastLogin' => relative_date(strtotime($tmpLastLogon)) + ); + + $tmpBody = stripslashes($_POST['body']); + $tmpTitle = stripslashes($_POST['title']); + $tmpBody = replaceByArray($tmpBody, $replacements); + $tmpTitle = replaceByArray($tmpTitle, $replacements); + echo "$tmpBody
"; + //This Sanitizes data - so no worries! + QueueEmail(0, $tmpEmail, $tmpTitle, $tmpBody, 100, true); + } + + echo "Complete!...
"; + DoRedirect("Emails processed!"); exit; } +include_once('./includes/sqli.php'); -include('./includes/sqli.php'); - -$res = mysqli_query($mysqli, "SELECT count(*) as _msg FROM `users`"); +$res = mysqli_query($mysqli, "SELECT count(*) as count FROM `emailQueue`"); $row = mysqli_fetch_assoc($res); -echo $row['_msg']; +$count = $row['count']; + +//QueueEmail(3, "snapwilliam@gmail.com", "hi,testing", "this is a body", 20, true); +//echo "
QUEUED!
"; -exit; -function QueueEmail($unsanitizeduserID, $unsanitizedto, $unsanitizedSubject, $unsanitizedBody) { +//$time_start = microtime(true); +//Loop It +// for ($i = 1; $i <= 10000; $i++) { + // SendQueuedEmail(); +// } +//$time_end = microtime(true); +//$time = $time_end - $time_start; +//echo "
Script execution: $time seconds XX\n
"; - //Check if there's something in the queue already: - $sql = "SELECT `userID` FROM emailQueue WHERE '$userID' = "; +$where = ''; +if ($_POST['selectResultSet'] == 'true') { + // CLAUSES: + $clause1 = $_POST['clause1']; + $clauseOptions = array('ID', 'displayName', 'dateJoined', 'dateLogin', 'email', 'isAdmin'); + if (!in_array($clause1, $clauseOptions)) die("clause1 fail"); + $operatorOptions = array('LessThan' => '<', 'GreaterThan' => '>', 'EqualTo' => '='); + $operator1 = $operatorOptions[$_POST['operator1']]; + $value1 = mysql_escape_string($_POST['value1']); + $where = "AND $clause1 $operator1 '$value1'"; } -//new table -// emailQueue ID, userID, sent, to, subject, body, dateSubmited, dateSent - -$sql = "SELECT `ID`, `displayName`, `dateJoined`, `dateLogin`, `email`, `isAdmin` FROM `users`"; +$sql = "SELECT `ID`, `displayName`, `dateJoined`, `dateLogin`, `email`, `isAdmin` +FROM `users` +WHERE `isOptedOutOfEmails` = '0' +$where +"; $result = mysql_query($sql); -echo " +?> +
-
-

MASS EMAIL

-
-"; +

Mass Email

+

There are emails in emailQueue.

-?> +

Modify the WHERE clause first to get the users you want selected.

+ +

Varriables available

+
- + + WHERE + -

+ IS + + TO + -Title:
-
- Please email me back -

- +
+

-

EXAMPLE SET

+
+
+ Email to send + Title:
+
+ Sends emails to all selected users. +
+ +

RESULT SET

+ + WHERE `isOptedOutOfEmails` = '0' +
+
+
+ - -IDDisplay name:Joined On:Last LogonEmailAdministrator - -"; - -while (list($CUID, $CUsername, $Joined, $LastLogon, $email, $isAdmin) = mysql_fetch_row($result)) { - - //$Joined = Date("d/m/y - g:ia", $Joined); - $Joined = strtotime($Joined); - //$Joined = relative_date($Joined); - $Joined = date("Y-m-d", $Joined); - - $LastLogon = strtotime($LastLogon); - //$LastLogon = relative_date($LastLogon); - $LastLogon = date("Y-m-d", $LastLogon); +echo displayUserResultSet($result); - if ($isAdmin == 1) - $isAdmin = "Yes"; - else - $isAdmin = "No"; - +echo "
"; - Echo " -$CUID -$CUsername -$Joined -$LastLogon -$email -$isAdmin -"; +htmlFooter(); +function displayUserResultSet($result) { + + $r = " + + + + + "; + $i = 0; + while (list($CUID, $CUsername, $Joined, $LastLogon, $email, $isAdmin) = mysql_fetch_row($result)) { + $i++; + //$Joined = Date("d/m/y - g:ia", $Joined); + $Joined = strtotime($Joined); + //$Joined = relative_date($Joined); + $Joined = date("Y-m-d", $Joined); + + $LastLogon = strtotime($LastLogon); + //$LastLogon = relative_date($LastLogon); + $LastLogon = date("Y-m-d", $LastLogon); + + if ($isAdmin == 1) + $isAdmin = "Yes"; + else + $isAdmin = "No"; + + $r .= " + + + + + + + "; + + } + $r .= " +
IDDisplay name:Joined On:Last LogonEmailAdministrator
$CUID + + $CUsername$Joined$LastLogon$email$isAdmin
+
+
+ + + + +
+ "; + $r = "

$i Users Returned

$r"; + return $r; } -Echo " - -
-
- - - - - -"; - - -function SendEmail($body, $subject = "Pathery Newsletter", $to = 'snapwilliam@gmail.com') { - // !! TODO - $to = 'snapwilliam@gmail.com'; - $mycompany = "Pathery"; - - $fromemail = "snap@pathery.com"; - $replyemail = "snap@pathery.com"; - - # -=-=-=- 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; - - # -=-=-=- HTML EMAIL PART - //None - # -=-=-=- FINAL BOUNDARY - $message .= "--$mime_boundary--\n\n"; - # -=-=-=- SEND MAIL - $mail_sent = @mail( $to, $subject, $message, $headers ); - Return $mail_sent; +function replaceByArray($text, $replacements) { + foreach ($replacements as $search=>$replace) { + $text = str_replace($search, $replace, $text); + } + return $text; } - - //Thank you: //http://snippets.dzone.com/posts/show/196 function relative_date($time) { @@ -205,5 +272,4 @@ function relative_date($time) { } } -htmlFooter(); ?> \ No newline at end of file -- cgit v1.2.3