function dispatchEmails($toAddresses, $sendermail, $subject, $message, $files = array()) { $ret = array(); foreach ($toAddresses as $toAddress) { $ret[] = dispatchEmail($toAddress, $sendermail, $subject, $message, $files); } return $ret; }
function sendTheEmail($parameters) { //replace the custom field placeholders with their values. $fieldsToSubstitute = $parameters; //the following fields should not be in the fields to substitute array $fieldsToRemove = array("to", "from", "fromname", "htmlenabled", "attachimages", "textbody", "htmlbody"); //we want the 'to' - the email address of the receipient to actually be named 'email' $fieldsToSubstitute['email'] = $fieldsToSubstitute['to']; foreach ($fieldsToRemove as $fieldToRemove) { unset($fieldsToSubstitute[$fieldToRemove]); } foreach ($fieldsToSubstitute as $fieldName => $value) { substitutePlaceHoldersWithValues($parameters['subject'], $fieldsToSubstitute); substitutePlaceHoldersWithValues($parameters['htmlbody'], $fieldsToSubstitute); substitutePlaceHoldersWithValues($parameters['textbody'], $fieldsToSubstitute); } //finsihed replacing the custom fields placeholers with their values. dispatchEmail($parameters); }
function sendConfirmedEmail($id) { global $wpdb; $query = "select * from " . $wpdb->prefix . "wpr_subscribers where id={$id}"; $sub = $wpdb->get_results($query); $sub = $sub[0]; //get the confirmation email and subject from newsletter $newsletter = _wpr_newsletter_get($sub->nid); $confirmed_subject = $newsletter->confirmed_subject; $confirmed_body = $newsletter->confirmed_body; //if a registered form was used to subscribe, then override the newsletter's confirmed email. $sid = $sub->id; //the susbcriber id $unsubscriptionURL = wpr_get_unsubscription_url($sid); $unsubscriptionInformation = "\n\nTo manage your email subscriptions or to unsubscribe click on the URL below:\n{$unsubscriptionURL}\n\nIf the above URL is not a clickable link simply copy it and paste it in your web browser."; $fid = $args[2]; $query = "SELECT a.* from " . $wpdb->prefix . "wpr_subscription_form a, " . $wpdb->prefix . "wpr_subscribers b where a.id=b.fid and b.id={$sid};"; $form = $wpdb->get_results($query); if (count($form)) { $confirmed_subject = $form[0]->confirmed_subject; $confirmed_body = $form[0]->confirmed_body; } $confirmed_body .= $unsubscriptionInformation; $params = array($confirmed_subject, $confirmed_body); wpr_place_tags($sub->id, $params); $fromname = $newsletter->fromname; if (!$fromname) { $fromname = get_bloginfo('name'); } $fromemail = $newsletter->fromemail; if (!$fromemail) { $fromemail = get_bloginfo('admin_email'); } $email = $sub->email; $emailBody = $params[1]; $emailSubject = $params[0]; $mailToSend = array('to' => $email, 'fromname' => $fromname, 'from' => $fromemail, 'textbody' => $emailBody, 'subject' => $emailSubject); try { dispatchEmail($mailToSend); } catch (Swift_RfcComplianceException $exception) { //disable all subscribers with that email. $email = $mailToSend['to']; $query = "UPDATE " . $wpdb->prefix . "wpr_subscribers set active=3, confirmed=0 where email='{$email}'"; $wpdb->query($query); } }
function wpr_process_updates() { //double check $updatesEnabled = get_option('wpr_updates_active'); if ($updatesEnabled == 'on') { //fetch the updates feed $updatesfeed = fetch_feed('http://www.wpresponder.com/updates/feed/'); if (is_wp_error($updatesfeed)) { return false; } else { //loop through the list of items and then deliver only the last update that is new. $numberOfItems = $updatesfeed->get_item_quantity(); $items = $updatesfeed->get_items(); $lastDate = get_option('wpr_updates_lastdate'); $dateOfLatestPost = $lastDate; $postToDeliver = false; //this loop loops through all the items in the feed and then delivers the latest possible post. foreach ($items as $item) { $itemDate = $item->get_date(); $itemDateStamp = strtotime($itemDate); if ($dateOfLatestPost < $itemDateStamp) { $dateOfLatestPost = $itemDateStamp; $postToDeliver = $item; } $debug .= "\nNope.."; } //end for loop to loop through the feed items. if ($postToDeliver != false) { //deliver the latest post. $title = $postToDeliver->get_title(); $theBody = $postToDeliver->get_content(); $notificationEmail = getNotificationEmailAddress(); $mail = array('to' => $notificationEmail, 'from' => get_bloginfo('admin_email'), 'fromname' => 'WP Responder Updates', 'subject' => $title, 'htmlbody' => $theBody, 'htmlenabled' => 1); //ob_start(); try { dispatchEmail($mail); } catch (Swift_RfcComplianceException $exception) { return false; } delete_option('wpr_updates_lastdate'); add_option('wpr_updates_lastdate', $dateOfLatestPost); return true; } //end - if the post is to be delivered. } //end - if the field is available } else { return false; } }
$subscriber = new Subscriber($id); foreach ($params as $index => $value) { $params[$index] = Subscriber::replaceCustomFieldValues($value, $subscriber); } $from_email = $newsletter->fromemail; if (!$from_email) { $from_email = get_bloginfo("admin_email"); } $from_name = $newsletter->fromname; if (!$from_name) { $from_name = get_bloginfo("name"); } $subject = $params[0]; $body = $params[1]; $verificationEmail = array('to' => $email, 'subject' => $subject, 'textbody' => $body, 'fromname' => $from_name, 'from' => $from_email); @dispatchEmail($verificationEmail); if (empty($return_url)) { if (isset($theForm)) { $return_url = $theForm->return_url; } } if (!empty($return_url)) { ?> <script> window.location='<?php echo $return_url; ?> '; </script> <?php exit;
function _wpr_process_queue() { global $wpdb; /*ENSURING THERE IS ONLY ONE INSTANCE THAT RUNS FOR A MAXIMUM OF ONE HOUR START HERE*/ set_time_limit(3600); $last_cron_status = get_option("_wpr_queue_delivery_status"); /* When the cron is running the _wpr_queue_delivery_status is set to the timestamp at which the cron processing was started. Before shutting down the _wpr_queue_delivery_status is set to 'stopped'. This cron will run only if the _wpr_queue_delivery_status option is set to "stopped" or is empty. */ $timeOfStart = time(); $timeMaximumExecutionTimeAgo = $timeOfStart - WPR_MAX_QUEUE_DELIVERY_EXECUTION_TIME; if (!empty($last_cron_status) && $last_cron_status != "stopped") { $last_cron_status = intval($last_cron_status); if ($last_cron_status != 0 && $last_cron_status > $timeMaximumExecutionTimeAgo) { return; } } update_option("_wpr_queue_delivery_status", $timeOfStart); $numberOfEmailsToDeliver = getNumberOfEmailsToDeliver(); $queueBatchSize = WPR_MAX_QUEUE_EMAILS_SENT_PER_ITERATION; $numberOfIterations = ceil($numberOfEmailsToDeliver / $queueBatchSize); //queue batch size of number of emails to deliver whichever is lesser should be fetched from db. if ($numberOfEmailsToDeliver < $queueBatchSize) { $queueBatchSize = $numberOfEmailsToDeliver; } for ($iter = 0; $iter < $numberOfIterations; $iter++) { $limitClause = sprintf(" LIMIT %d", $queueBatchSize); $query = sprintf("SELECT q.* FROM `%swpr_queue` q, %swpr_subscribers s WHERE s.id=q.sid AND q.`sent`=0 AND s.confirmed=1 AND s.active=1 %s ", $wpdb->prefix, $wpdb->prefix, $limitClause); $results = $wpdb->get_results($query); foreach ($results as $mail) { $mail = (array) $mail; // $checkWhetherUnsent = sprintf("SELECT COUNT(*) num FROM {$wpdb->prefix}wpr_queue WHERE `id`=%d AND `sent`=0", $mail['id']); $whetherUnsentRes = $wpdb->get_results($checkWhetherUnsent); $number = $whetherUnsentRes[0]->num; if ($number == 0) { continue; } $setEmailAsSentQuery = sprintf("UPDATE `%swpr_queue` SET `sent`=1 WHERE `id`=%d", $wpdb->prefix, $mail['id']); $wpdb->query($setEmailAsSentQuery); try { dispatchEmail($mail); } catch (Swift_RfcComplianceException $exception) { //disable all subscribers with that email. //TODO: Move this to a separate function. $email = $mail['to']; $setTheEmailAsFailedQuery = $wpdb->prepare("UPDATE `{$wpdb->prefix}wpr_subscribers` SET `active`=3, `confirmed`=0 WHERE `email`=%s", $email); $wpdb->query($setTheEmailAsFailedQuery); //set aall other emails in queue which have this email to 3. //TODO: #DevDoc sent=3 means invalid email. $markAllEmailsOfThisEmailUnprocessable = $wpdb->prepare("UPDATE `{$wpdb->prefix}wpr_queue` SET `sent`=3 WHERE `sent`=0 AND `email`=%s", $email); $wpdb->query($markAllEmailsOfThisEmailUnprocessable); } $timeThisInstant = time(); $timeSinceStart = $timeThisInstant - $timeOfStart; if ($timeSinceStart > WPR_MAX_QUEUE_DELIVERY_EXECUTION_TIME) { update_option("_wpr_queue_delivery_status", "stopped"); return; } } } update_option("_wpr_queue_delivery_status", "stopped"); }