-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron_send_email_notifications.php
145 lines (122 loc) · 4.64 KB
/
cron_send_email_notifications.php
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
<?php
use \Components\Classes\db;
use \Components\Entity\Message;
require_once('includes/application_top.php');
define('EMAIL_NOTIFICATION_LIMIT', 50);
define('MAX_EXEC_TIME', 50);
define('DEF_EXEC_TIME', 20);
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'" );
}
cron_sendEmailNotifications();