use Components\Classes\db; use Components\Entity\Message; include_once "functions.php"; if (isset($_REQUEST["read"])) { $message = Message::find(intval($_REQUEST["read"])); if ($message) { $GUI->tmpls[] = $active_module_root . "read.tmpl.php"; include "inc_read.php"; return; } else { $GUI->ERR("Письмо не найдено"); page_ReloadSubSec(); } } if (isset($_REQUEST['delete'])) { \Components\Entity\EmailNotification::delete($_REQUEST['delete']); } $tbl = $GUI->Table("mls_problems", array("cur_sort_up" => true)); $tbl->Width = "100%"; $tbl->DataMYSQL('email_notifications en JOIN ' . TABLE_MESSAGES . ' m ON m.id=en.message_id', 'en.*, m.id AS mid', 'en'); $tbl->FilterMYSQL("en.attempts_to_send>0"); $tbl->Pager(CGUI_PAGER_FLAG_SEL | CGUI_PAGER_FLAG_RR | CGUI_PAGER_FLAG_R | CGUI_PAGER_FLAG_FF | CGUI_PAGER_FLAG_F, 10, array(10, 20, 50, 100, 0)); global $n; if (Roles::isActionAllowed($GUI->mmenu->selected->id, $GUI->mmenu->selected->selected->id, $_SESSION["user"]["data"]["group_id"], "Просмотр сообщения")) { $tbl->RowEvent2 = "if(window.locationLocked === undefined || window.locationLocked ==false) { document.location.href=\"?section=mls&subsection=" . MLS_SELECTED_INBOX . "&type=o&read=%var.message_id%\";} else window.locationLocked = false;"; } $tbl->OnRowStart = "_set_row_color"; $columns_resource = Roles::getColumns($GUI->mmenu->selected->id, $GUI->mmenu->selected->selected->id, $_SESSION["user"]["data"]["group_id"]); if (!is_resource($columns_resource)) { $GUI->ERR($columns_resource); page_reload();
/** * @param int $message_id Message id in TABLE_MESSAGES * @param string $receiver_email receiver's Email address * @param int $message_type тип сообщения, из набора EmailNotificationType::$NOTIFICATION_TYPES * @see EmailNotificationType * @return mixed */ function enqueue_message_to_email($message_id, $receiver_email, $notification_type) { assert(in_array($notification_type, EmailNotificationType::$NOTIFICATION_TYPES)); if (empty($receiver_email)) { return false; } if (!EmailNotificationType::isPersistable($notification_type)) { return false; } return EmailNotification::create(array('message_id' => $message_id, 'receiver_email' => $receiver_email, 'type' => $notification_type)); }
public static function enqueue_message_to_email($message_id, $authors_ids, $notification_type = EmailNotification::TO_AUTHOR_ON_ASSIGN) { if (empty($message_id) || !is_numeric($message_id)) { return false; } assert(in_array($notification_type, EmailNotification::$NOTIFICATION_TYPES)); // Если не надо уведомлять - выходим типа все ок if (!EmailNotificationType::isSendable($notification_type)) { return array(); } $result = array(); if (!is_array($authors_ids) && is_numeric($authors_ids)) { $authors_ids = array($authors_ids); } foreach ($authors_ids as $id) { if (is_numeric($id)) { try { $author = Employee::find($id); } catch (Exception $e) { $result['error'][] = $id; continue; } $notification_id = EmailNotification::create(array('message_id' => $message_id, 'receiver_email' => $author['email'], 'type' => $notification_type)); if ($notification_id) { $result['success'][] = $id; } } else { $ids = explode(', ', $id); $temp_result = self::enqueue_message_to_email($message_id, $ids, $notification_type); if (count($temp_result['success'])) { array_push($result['success'], $temp_result['success']); } if (count($temp_result['error'])) { array_push($result['error'], $temp_result['error']); } } } return $result; }
function cron_sendEmailNotifications() { $execution_time = ini_get('max_execution_time'); if (empty($execution_time)) { $execution_time = DEF_EXEC_TIME; } else { if ($execution_time > MAX_EXEC_TIME * 2) { $execution_time = MAX_EXEC_TIME * 2; } } $execution_time_start = time(); $execution_time_end = $execution_time_start + $execution_time / 2; $notifications = \Components\Entity\EmailNotification::findBy(array(), array('attempts_to_send' => 'ASC'), EMAIL_NOTIFICATION_LIMIT, 0); $notification_index = 0; $good_cnt = 0; $notification_count = count($notifications); while (time() <= $execution_time_end && $notification_index < $notification_count) { $notification = $notifications[$notification_index]; $message = \Components\Entity\Message::find($notification['message_id']); if (!empty($message)) { //Прочитано, или не шлем - надо его сразу убить, чтобы потом когда включим не повалил шквал старых писем if ($message['readed'] || !\Components\Entity\EmailNotificationType::isSendable($notification['type'])) { \Components\Entity\EmailNotification::delete($notification['id']); } else { $attachments = array(); // надо только для писем типа \Components\Entity\EmailNotification::TO_SUBSCRIBED_AUTHORS_ON_DISTRIBUTION $all_added_size = 0; $fna = 0; if ($notification['type'] == \Components\Entity\EmailNotification::TO_SUBSCRIBED_AUTHORS_ON_DISTRIBUTION && !empty($message['order_id'])) { $files = get_order_files($message['order_id']); foreach ($files as $file) { $all_added_size += $file['size']; if ($all_added_size > 16000000) { // write to body, but not add file $fna++; } else { $attachments[] = array('path' => get_file_path($message['order_id'], $file), 'name' => $file['name']); } } } $subtext = ""; if ($fna) { $subtext = "<br><br>-----------------------------------<br>" . "Еще " . $fna . " файла(ов) не были добавлены к письму из-за ограничения размеров"; } // Это условие проверено выше //if ( \Components\Entity\EmailNotificationType::isSendable($notification['type']) ) //{ $email = new \Components\Classes\Email(); $email->IsHTML(true); $email->setData(array('email' => $notification['receiver_email'], 'name' => ''), $message['subject'], $message['text'] . $subtext, $attachments, true, Message::getReceiverEmailAndName($message['creator_id']), Message::getReceiverEmailAndName($message['creator_id'])); try { $send_result = $email->send(); if ($send_result) { \Components\Entity\EmailNotification::delete($notification['id']); $good_cnt++; } else { \Components\Entity\EmailNotification::update($notification['id'], array('attempts_to_send' => $notification['attempts_to_send'] + 1, 'last_attempt' => time(), 'last_error' => $email->ErrorInfo)); } } catch (\Components\Exceptions\Exception $e) { \Components\Entity\EmailNotification::update($notification['id'], array('attempts_to_send' => $notification['attempts_to_send'] + 1, 'last_attempt' => time())); } unset($email); //} } } $notification_index++; } db::query("update ofc_sys_log set p_value=" . $execution_time_start . " where p_name='email_notify_last_tm_start'"); db::query("update ofc_sys_log set p_value=" . (time() - $execution_time_start) . " where p_name='email_notify_last_tm_work'"); db::query("update ofc_sys_log set p_value=" . $notification_index . " where p_name='email_notify_last_all_cnt'"); db::query("update ofc_sys_log set p_value=" . $good_cnt . " where p_name='email_notify_last_good_cnt'"); }