-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcron.php
More file actions
86 lines (58 loc) · 2.22 KB
/
cron.php
File metadata and controls
86 lines (58 loc) · 2.22 KB
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
<?php
/*
SWIFT MAILER REQUIRED
---------------------
sudo apt-get install php-pear
sudo pear channel-discover pear.swiftmailer.org
sudo pear install swift/swift
*/
// 1) Setup swift mailer transport
require_once 'swift_required.php';
$transport = Swift_SmtpTransport::newInstance('mail.yourserver.com', 26)
->setUsername('username')
->setPassword('password')
;
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// 2) Determine feeds
$now = time();
$h24 = date("Y-n-j H:i:s", time() - (3600*2));
$h48 = date("Y-n-j H:i:s", time() - (3600*4));
$mysqli = new mysqli("localhost","username","password","database");
$result = $mysqli->query("SELECT id,userid,name FROM feeds WHERE `time`>='$h48' AND `time`<='$h24';");
$users = array();
while ($row = $result->fetch_object())
{
if (!isset($users[$row->userid])) $users[$row->userid] = array('id'=>$row->userid, 'feeds'=>array());
$users[$row->userid]['feeds'][] = $row->name;
}
foreach ($users as $user)
{
$userid = $user['id'];
$result = $mysqli->query("SELECT email FROM notify WHERE `userid` = '$userid' AND `enabled` = '1';");
if ($result && $result->num_rows)
{
$row = $result->fetch_array();
$email = $row['email'];
// Send an email
$body = "<p><b><?php echo _('Hello!'); ?></b></p><p><?php echo _('The following emoncms feeds have become inactive for more than 2 hours:'); ?></p><ul>";
foreach ($user['feeds'] as $feed)
{
$body .= "<li>".$feed."</li>";
}
$body .= "</ul>";
$body .= "<p><i><?php echo _('This is an automated email generated by the emoncms notify module. To turn 'notify on inactive' off click on disable on the notify module page.'); ?></i></p>";
// Create the message
$message = Swift_Message::newInstance()
// Give the message a subject
->setSubject(count($user['feeds'])._("emoncms feeds have become inactive"))
// Set the From address with an associative array
->setFrom(array('from@address.example' => 'Emoncms'))
// Set the To addresses with an associative array
->setTo(array($email))
// Give it a body
->setBody($body, 'text/html')
;
$result = $mailer->send($message);
}
}