Skip to content

Instantly share code, notes, and snippets.

@adatta02
Created December 23, 2011 05:35
Show Gist options
  • Save adatta02/1513268 to your computer and use it in GitHub Desktop.
Save adatta02/1513268 to your computer and use it in GitHub Desktop.
Checks an IMAP mailbox for exception emails and then makes a call via Twilio if a limit is hit
<?php
if( $_REQUEST["Digits"] == "1234" ){
if( file_exists(dirname(__FILE__) . "/hasExceptionNotification.lock") ){
unlink( dirname(__FILE__) . "/hasExceptionNotification.lock" );
}
touch( dirname(__FILE__) . "/pauseNotification.lock" );
$str = <<<EOF
<?xml version='1.0' encoding='utf-8' ?>
<Response>
<Say>Thanks! This alert has been cleared.</Say>
</Response>
EOF;
echo $str;
}
<?php
error_reporting(E_ALL);
require dirname(__FILE__) . "/wrapper/Services/Twilio.php";
if(php_sapi_name() == 'cli') {
checkIMAPMailbox( );
}else{
clearNotification( true );
}
function clearNotification( $withXML = true ){
$str = <<<EOF
<?xml version='1.0' encoding='utf-8' ?>
<Response>
<Say>Setfive notifications has received multiple exceptions within the critical time window! Please enter 1 2 3 4 # to acknowledge this alert.</Say>
<Gather action="http://yourhost.com/process_gather.php" method="GET" timeout="10"></Gather>
</Response>
EOF;
if( $withXML ){
echo $str;
}
}
function checkIMAPMailbox( ){
$onlyReadMail = false;
if( file_exists(dirname(__FILE__) . "/pauseNotification.lock") ){
$mtime = filemtime( dirname(__FILE__) . "/pauseNotification.lock" );
if( time() - $mtime > 3600 ){
unlink(dirname(__FILE__) . "/pauseNotification.lock");
}else{
$onlyReadMail = true;
}
}
$res = imap_open ( "{yourhost.com:993/ssl/imap}INBOX", "yourmail@yourhost.com" , "yourpassword" );
if(!$res){
die("Could not determine mailbox info.");
}
$mbox = imap_check( $res );
$exceptionCount = 0;
$hasException = file_exists( dirname(__FILE__) . "/hasExceptionNotification.lock" );
for($i = 1; $i <= $mbox->Nmsgs; $i ++) {
$msgInfo = imap_headerinfo($res, $i);
$subject = strtolower( $msgInfo->subject );
imap_delete($res, $i);
echo $subject . "\n";
if( strpos($subject, "Exception") !== false ){
$exceptionCount ++;
}
if( strpos($subject, "clear") !== false ){
if( file_exists(dirname(__FILE__) . "/hasExceptionNotification.lock") ){
unlink( dirname(__FILE__) . "/hasExceptionNotification.lock" );
}
touch( dirname(__FILE__) . "/pauseNotification.lock" );
$onlyReadMail = true;
}
}
imap_expunge($res);
imap_close($res);
if( $onlyReadMail ){
exit( 0 );
}
if( $exceptionCount >= 5 || $hasException ){
touch( dirname(__FILE__) . "/hasExceptionNotification.lock" );
$client = new Services_Twilio("twilio_account_id", "twilio_secret");
try{
$call = $client->account->calls->create("+your_twilio_number", "+number_to_dial", "http://yourhost.com/checkNotifications.php");
}catch( Exception $ex ){
echo $ex;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment