Set('BaseURL', $ROOTURL); $Email_API->Set('CharSet', SENDSTUDIO_ENCODING); /* This function will retrieve details for a particular send (if an id is specified). If there is no id specified it will find the first one in the list to run. */ function FetchSend($sendid=0) { global $TABLEPREFIX, $SYSTEMTIME; if ($sendid) { $query = "SELECT ss.*, s.NotifyOwner FROM " . $TABLEPREFIX . "server_sends ss, " . $TABLEPREFIX . "sends s WHERE s.SendID=ss.SendID AND ss.ServerSendID='".addslashes($sendid)."'"; } if (!$sendid) { // we only want the one that was entered first in the list that isn't in progress. $query = "SELECT ss.*, s.NotifyOwner FROM " . $TABLEPREFIX . "server_sends ss, " . $TABLEPREFIX . "sends s WHERE s.SendID=ss.SendID AND InProgress='0' AND ss.Finished='0' AND ss.Paused='0' AND ss.SendTime <= '".addslashes($SYSTEMTIME)."' ORDER BY ss.SendTime ASC LIMIT 1"; } $result = mysql_query($query); if (mysql_num_rows($result) < 1) { mysql_free_result($result); return false; } $server_send = mysql_fetch_assoc($result); mysql_free_result($result); return $server_send; } while($SERVER_SEND = FetchSend()) { $GLOBALS['ERROR_LOG_FILE'] = $ROOTDIR . 'temp/mail_errors.' . $SERVER_SEND['SendID'] . '.csv'; $Email_API->ForgetEmail(); $Email_API->ClearAttachments(); // let's load the list up! $list_query = "SELECT * FROM " . $TABLEPREFIX . "lists l, " . $TABLEPREFIX . "sends s WHERE s.SendID='".addslashes($SERVER_SEND['SendID'])."' AND s.ListID=l.ListID"; $list_result = mysql_query($list_query); $list_details = mysql_fetch_assoc($list_result); mysql_free_result($list_result); /* If the newsletter isn't in the process of sending - which it should never be at this stage! Notify the list admin - only if we're not restarting sending after a pause. */ if (!$SERVER_SEND['InProgress']) { $update_query = "UPDATE " . $TABLEPREFIX . "server_sends SET InProgress='1' WHERE ServerSendID='" . addslashes($SERVER_SEND['ServerSendID']) . "'"; mysql_query($update_query); if (!$SERVER_SEND['Resumed']) { NotifySchedulerAdmin('start', $SERVER_SEND['ServerSendID']); } } // extra check to make sure we don't try to send to an in progress schedule. if ($SERVER_SEND['InProgress']) { continue; } // We want to spread out the emails getting sent over the whole hour (if the user has throttling). // how many can we send per hour? $user_query = "SELECT * FROM " . $TABLEPREFIX . "admins WHERE AdminID='".addslashes($SERVER_SEND['AdminID'])."'"; $user_result = mysql_query($user_query); $USER_DETAILS = mysql_fetch_assoc($user_result); mysql_free_result($user_result); $PAUSE = 0; if ($USER_DETAILS['PerHour'] > 0) $PAUSE = 3600/$USER_DETAILS['PerHour']; if ($USER_DETAILS['EmailServer'] || $USER_DETAILS['EmailServer'] != '') { if (strpos($USER_DETAILS['EmailServer'], ';') !== false) { list($servername, $username, $password) = explode(';', str_replace(' ', '', $USER_DETAILS['EmailServer'])); } else { $servername = $USER_DETAILS['EmailServer']; $username = $password = false; } $Email_API->SetSmtp($servername, $username, $password); } // load up the newsletter to send! $send_result = mysql_query("SELECT * FROM " . $TABLEPREFIX . "sends WHERE SendID='".addslashes($SERVER_SEND['SendID'])."'"); $send=mysql_fetch_assoc($send_result); mysql_free_result($send_result); if($send['DateStarted']==0) { $result = mysql_query("UPDATE " . $TABLEPREFIX . "sends SET DateStarted='".addslashes(time())."' WHERE SendID='".addslashes($SERVER_SEND['SendID'])."'"); $send['DateStarted'] = $SYSTEMTIME; } $newsletter_query = "SELECT * FROM " . $TABLEPREFIX . "composed_emails WHERE ComposedID='" . $send['ComposedID'] . "'"; $newsletter_result = mysql_query($newsletter_query); $newsletter_details = mysql_fetch_assoc($newsletter_result); $Email_API->TrackLinks($send['TrackLinks']); $Email_API->TrackOpens($send['TrackOpens']); $Email_API->Set('AdminID', $SERVER_SEND['AdminID']); $Email_API->Set('ComposedID', $send['ComposedID']); $Email_API->Set('TablePrefix', $TABLEPREFIX); $Email_API->Set('ReplyTo', $send['ReplyTo']); $Email_API->Set('BounceAddress', $send['ReturnPath']); $Email_API->Set('SendFrom', $send['SendFrom']); $Email_API->Set('Subject', html_entity_decode(stripslashes($newsletter_details['Subject']))); $Email_API->AddBody('text', stripslashes($newsletter_details['TextBody'])); $Email_API->AddBody('html', stripslashes($newsletter_details['HTMLBody'])); $attachment_ids = explode(':', $newsletter_details['AttachmentIDs']); if (!empty($attachment_ids) && $attachment_ids[0] != '') { $attachment_filename_result = mysql_query("SELECT AttachmentName, AttachmentFilename FROM " . $TABLEPREFIX . "attachments WHERE AttachmentID IN (" . implode(',', $attachment_ids) . ")"); while($attachment_row = mysql_fetch_assoc($attachment_filename_result)) { $attachment_file = dirname(__FILE__) . '/' . $attachment_row['AttachmentFilename']; $Email_API->AddAttachment($attachment_file, $attachment_row['AttachmentName']); } } $custom_fields_to_replace = $Email_API->GetCustomFields(); $breakme = false; $query = "SELECT sr.MemberID, sr.Format, Problems, Email, ConfirmCode, SubscribeDate, m.Confirmed, m.Status FROM " . $TABLEPREFIX . "send_recipients sr, " . $TABLEPREFIX . "members m WHERE sr.MemberID=m.MemberID AND sr.SendID='".addslashes($SERVER_SEND['SendID'])."' AND Problems='0'"; $result = mysql_query($query); while ($e = mysql_fetch_assoc($result)) { $Email_API->Set('Multipart', false); // if we don't send an email for whatever reason, then we need to update the user stats to reflect it properly. $update_user_stats_query = "UPDATE " . $TABLEPREFIX . "sends_permonth SET EmailsSent = EmailsSent - 1 WHERE SendID='" . (int)$SERVER_SEND['SendID'] . "'"; // if somehow the subscriber has been marked as 'bad', then skip it. // just in case. if ($e['Problems'] == 1) { mysql_query($update_user_stats_query); continue; } if (Banned($e['Email'], $send['ListID'])) { log_mailing_error(DisplayDate(time()) . ',' . $e['Email'] . ',' . 'Email address (or domain) is banned from the mailing list'); mysql_query($update_user_stats_query); continue; } $Email_API->ClearRecipients(); if ($e['Format'] == '3') { $Email_API->Set('Multipart', true); } if (empty($newsletter_details['HTMLBody']) || empty($newsletter_details['TextBody'])) { $Email_API->Set('Multipart', false); } switch($e['Format']) { case '1': $format = 'text'; break; case '2': case '3': $format = 'html'; break; } if (empty($newsletter_details['HTMLBody'])) { $format = 'text'; $e['Format'] = '1'; } $Email_API->AddRecipient($e['Email'], '', $format, $e['MemberID']); $custom_fields = array(); if (!empty($custom_fields_to_replace)) { $query = "SELECT v.FieldID AS FieldID, Value, FieldType, AllValues, DefaultValue FROM " . $TABLEPREFIX . "list_field_values v, " . $TABLEPREFIX . "list_fields f WHERE v.FieldID=f.FieldID AND v.ListID='" . addslashes($send['ListID']) . "' AND v.UserID='".addslashes($e['MemberID'])."' AND v.FieldID IN (" . implode(',', $custom_fields_to_replace) . ")"; $customfields_result = mysql_query($query); while($customfields_row = mysql_fetch_assoc($customfields_result)) { $value = stripslashes($customfields_row['Value']); switch($customfields_row['FieldType']) { case 'checkbox': if ($customfields_row['Value'] == 'CHECKED') { $value = 'Yes'; } else { $value = 'No'; } break; case 'longtext': if ($format=='HTML') { $value=nl2br($customfields_row['Value']); } break; case 'multicheckbox': $myvals = explode(':', $value); $value = ''; $fieldvals=explode(';',$customfields_row['AllValues']); foreach($fieldvals as $fieldval){ if (!$fieldval) continue; list($sys,$lab)=explode('->',$fieldval); if (in_array($sys, $myvals)) $value .= $lab . ', '; } $value = substr($value, 0, -2); break; case 'datebox': $order = explode(',', $customfields_row['AllValues']); list($day, $month, $year) = explode(':', $value); $value = ''; foreach($order as $o) { if (!in_array($o, array('month', 'day', 'year'))) continue; $value .= $$o . '/'; } $value = substr($value, 0, -1); break; } $custom_fields[] = array('fieldid' => $customfields_row['FieldID'], 'data' => $value); } } if ($e['Confirmed'] == 1) { $confirmation = 'Confirmed'; } else { $confirmation = 'Unconfirmed'; } if ($e['Status'] == 1) { $status = 'Active'; } else { $status = 'Inactive'; } switch($e['Format']) { case '1': $cf_format = 'Text'; break; case '2': case '3': $cf_format = 'HTML'; break; } $customfield_info = array( 'listid' => $send['ListID'], 'confirmcode' => $e['ConfirmCode'], 'sendid' => $SERVER_SEND['SendID'], 'CustomFields' => $custom_fields, 'subscriberid' => $e['MemberID'], 'subscribedate' => $e['SubscribeDate'], 'confirmation' => $confirmation, 'status' => $status, 'format' => $cf_format, 'listname' => $list_details['ListName'] ); $Email_API->AddCustomFieldInfo($e['Email'], $customfield_info); $mark_as_problem_query = "UPDATE " . $TABLEPREFIX . "send_recipients SET Problems='1' WHERE MemberID='".addslashes($e['MemberID'])."' AND SendID='".addslashes($SERVER_SEND['SendID'])."'"; mysql_query($mark_as_problem_query); if ($e['Email']) { $mail_result = $Email_API->Send(true); if ($mail_result['success'] > 0) { $email_result = true; } else { $email_result = false; list($bad_email, $servermsg) = current($mail_result['fail']); } if ($email_result) { if($e['Format'] == 1) { $s=", TextRecipients=TextRecipients+1"; } else { $s=", HTMLRecipients=HTMLRecipients+1"; } $update_result = mysql_query("UPDATE " . $TABLEPREFIX . "sends SET EmailsSent=EmailsSent+1 $s WHERE SendID='".addslashes($SERVER_SEND['SendID'])."'"); } else { mysql_query($update_user_stats_query); $logmsg = 'Unable to Queue Email'; if ($servermsg) $logmsg .= ': ' . $servermsg; log_mailing_error(DisplayDate(time()) . ',' . $e['Email'] . ',' . $logmsg); } } else { mysql_query($update_user_stats_query); log_mailing_error(DisplayDate(time()) . ',' . $e['MemberID'] . ',' . 'Unable to find email address for subscriber'); } // check to make sure the scheduled event hasn't been paused.. $ss = FetchSend($SERVER_SEND['ServerSendID']); if ($ss['Paused']) { $Email_API->_Close_Smtp_Connection(); $breakme = true; break; } if ($PAUSE > 0 && $PAUSE < 1) { $p = floor($PAUSE*1000000); usleep($p); } else { sleep(floor($PAUSE)); } } if ($breakme) { continue; } $delete_query = "DELETE FROM " . $TABLEPREFIX . "send_recipients WHERE SendID='".addslashes($SERVER_SEND['SendID'])."'"; mysql_query($delete_query); $update_query = "UPDATE " . $TABLEPREFIX . "sends SET Completed='1', DateEnded='".addslashes(time())."' WHERE SendID='".addslashes($SERVER_SEND['SendID'])."'"; $update_result = mysql_query($update_query); $query = "UPDATE " . $TABLEPREFIX . "server_sends SET Finished='1', InProgress=0, Notified=0 WHERE ServerSendID='".addslashes($SERVER_SEND['ServerSendID'])."'"; $update_result = mysql_query($query); NotifySchedulerAdmin('finish', $SERVER_SEND['ServerSendID']); } exit(); ?>