Skip to content

Commit 12c25cb

Browse files
committed
replaced PHP mail() function by PHP Pear Mail object, send mail once per day
1 parent 02b5ff4 commit 12c25cb

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
config.inc.php
22
~
3+
alert.lock

config.inc.php-dist

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ $config = array(
1111
// 'smtp.example.tld:25',
1212
),
1313
'alert' => array(
14-
// 'subject' => 'PHP monitoring alert',
15-
// 'to' => 'admin@example.tld',
14+
// 'factory' => 'smtp',
15+
// 'parameters' => array(
16+
// 'host' => 'smtp.example.tld'
17+
// ),
18+
// 'headers' => array(
19+
// 'From' => 'monitoring@example.tld',
20+
// 'To' => 'admin@example.tld',
21+
// 'Subject' => 'PHP monitoring alert',
22+
// ),
1623
),
1724
);

functions.php

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
class PHPMonitoring {
77

8+
const ALERT_FILE = 'alert.lock';
89
const CONFIG_FILE = 'config.inc.php';
910
const ERROR_LOG = '/var/log/php-monitoring.log';
1011
var $config;
@@ -16,11 +17,10 @@ function init(){
1617
ini_set('log_errors', 1);
1718
ini_set('error_log', self::ERROR_LOG);
1819
global $config;
19-
$config_file = dirname(__FILE__) . '/' . self::CONFIG_FILE;
20-
if (!file_exists($config_file)){
21-
throw new Exception("config file $config_file not found");
20+
if (!file_exists($this->getConfigFilePath())){
21+
throw new Exception("config file {$this->getConfigFilePath()} not found");
2222
}
23-
require_once $config_file;
23+
require_once $this->getConfigFilePath();
2424
$this->config = $config;
2525
$this->config['results'] = array();
2626
}
@@ -92,7 +92,7 @@ function error($msg){
9292
}
9393

9494
/**
95-
* Send a mail alert
95+
* Send a mail alert once per day
9696
*/
9797
function alert(){
9898
if (
@@ -102,21 +102,52 @@ function alert(){
102102
){
103103
throw new Exception('alert not configured');
104104
}
105-
$body = '';
106-
foreach ($this->getServices() as $service){
107-
$body .= $this->printService($service);
105+
if (file_exists($this->getAlertFilePath())){
106+
if (date('d', filemtime($this->getAlertFilePath())) != date('d')){
107+
unlink($this->getAlertFilePath());
108+
}else{
109+
return;
110+
}
108111
}
109-
$result = mail(
110-
$this->config['alert']['to'],
111-
$this->config['alert']['subject'],
112-
$body
113-
);
114-
if (!$result){
115-
$this->error('could not send alert');
112+
$this->config['alert']['body'] = '';
113+
foreach ($this->getServices() as $service){
114+
$this->config['alert']['body'] .= $this->printService($service);
116115
}
116+
$this->mail($this->config['alert']);
117+
touch($this->getAlertFilePath());
117118
return $result;
118119
}
119120

121+
/**
122+
* Send a mail using PHP PEAR library
123+
*/
124+
function mail($opts = array()){
125+
if (!isset($opts['factory'])) throw new Exception('mail factory not set');
126+
if (!isset($opts['parameters'])) throw new Exception('mail parameters not set');
127+
if (!isset($opts['headers'])) throw new Exception('mail headers not set');
128+
if (!isset($opts['body'])) throw new Exception('mail body not set');
129+
require_once('Mail.php');
130+
$mail =& Mail::factory($opts['factory'], $opts['parameters']);
131+
$mail->send($opts['headers']['To'], $opts['headers'], $opts['body']);
132+
if (PEAR::isError($mail)) {
133+
throw new Exception($mail->getMessage());
134+
}
135+
}
136+
137+
/**
138+
* Get config file path
139+
*/
140+
function getConfigFilePath(){
141+
return dirname(__FILE__) . '/' . self::CONFIG_FILE;
142+
}
143+
144+
/**
145+
* Get alert file path
146+
*/
147+
function getAlertFilePath(){
148+
return dirname(__FILE__) . '/' . self::ALERT_FILE;
149+
}
150+
120151
/**
121152
* Get services from config
122153
*/

0 commit comments

Comments
 (0)