Author Topic: Flatline-check.php script no send email  (Read 469 times)

0 Members and 1 Guest are viewing this topic.

Offline CarlosLSev

  • Member
  • *
  • Posts: 31
Flatline-check.php script no send email
« on: March 07, 2021, 11:35:01 AM »
Hi I am trying to get the flatline-check.php script to work.
If the realtime.txt file is not updated after 1 minute, it shows me a message. But it doesn't send email. My website is hosted by ionos. I don't think I have sendmail service. He set up PHPMailer and it works.
But I don't know how to tell the script to send it through PHPMailer instead of Sendmail.



<?php
// Checks to see if a delimited data file is changing or not (flatlined)
// Requires that a text file named flatline-status.txt be on the server with write permissions (CHMOD 666)
// Include this in a page where you want the message to show up if the data is flatlined
// If flatlined an EMail can be sent
// 1-4-09
// 5-9-12 - Updated to PHP 5 timezone function
// 3-8-19 - Fixed some date() issues & removed the mail test variable
// Jim McMurry - jcweather.us - jmcmurry@mwt.net
//
// Configuration
date_default_timezone_set("Europe/Madrid");       // Zones available at http://us2.php.net/manual/en/timezones.php

$cachefile = 'flatline-status.txt';                 // File to hold cached data for comparison
$watchfile = 'realtime.txt';                        // Data file to watch for changes
$delimit   = " ";                                   // Delimeter used in that file
$offset1   = 4;                                     // Temp
$offset2   = 5;                                     // Humidity
$offset3   = 6;                                     // Baro
$offset4   = 1;                                     // Wind Ave Speed
$offset5   = 72;                                    // DP
$maxage    = 1;                                    // Minutes that the data can remain unchanged w/o email. Recommend minimum 30
$to         = 'miemail@gmail.com';                    // EMail info (EDITED)
$from      = 'Meteoviso.es';
$subject   = 'Weather Data Appears Flatlined.';
$subject2  = 'Weather Data Back Online.';
$body      = ' now.  Last data change recorded at ';  // Will go between current date/time & last good date/time in the EMail Body.
$HTMLMsg   = '<div style="text-align:center;">Sorry, Weather Station Data Has Been Disrupted ... Notification Has Been Sent.</div>';
// End of configurable items
$now = time();
$newdata = get_contents( $watchfile , $delimit);        // read the target file
$current = " (" . $newdata[$offset1] . " " . $newdata[$offset2] . " " . $newdata[$offset3] . " " . $newdata[$offset4] . " " . $newdata[$offset5] . ")";
$cachedata = get_contents( $cachefile , ",");           // read the cache file
$cachetime = $cachedata[0];
if ($newdata[0] != -9999 && $cachedata[0] != -9999) {   // was able to read the files
   $flat = ($newdata[$offset1] == $cachedata[1]) &&
         ($newdata[$offset2] == $cachedata[2]) &&
         ($newdata[$offset3] == $cachedata[3]) &&
         ($newdata[$offset4] == $cachedata[4]) &&
         ($newdata[$offset5] == $cachedata[5]);      // compare various offsets
   $status = substr(trim($cachedata[6]), 0, 4);        // capture the "sent" if it's already there
   $WatchSpan = round(($now - $cachetime) / 60);       // minutes since last data change
   if ($flat) {
      if ($WatchSpan >= $maxage) {                    // wait time is expired   
         if ( $status != "sent" ) {                  // mail hasn't been sent yet         
            mail($to, $subject , date("m-d-y H:i") . $body . date( "m-d-y H:i", $cachetime) . $current, 'From: ' . $from);
            $status ="sent "  . date("m-d-y H:i", $now);
         } else {
            $status = trim($cachedata[6]);         // carry over the original entry
         }
         echo $HTMLMsg;                                       // put the message on the screen
      } else {
         $status="watching from " . date("g:i a", $cachetime) . ", now " . date("g:i a", $now) . ", static for " . $WatchSpan . " min";
      }
      $now = $cachetime;                                     // leave timestamp as it was for now
   } else {
      if ( $status == "sent" ) {           // mail previously sent            
         mail($to, $subject2 ,"Back active at " . date("m-d-y g:i a") . $current, 'From: ' . $from);
      }
      $status="active " . date("m-d-y g:i a", $now);
   }
   $fp = fopen($cachefile, "w") or die("HINT: Manually upload a file named " . $cachefile . " and give it write permissions (CHMOD 666).");
   fputs($fp, $now . "," . $newdata[$offset1] . "," . $newdata[$offset2] . "," . $newdata[$offset3] . "," . $newdata[$offset4] . "," . $newdata[$offset5] . ", " . $status);
   fclose($fp);
}   // else we couldn't read one of the files

function get_contents( $rawfile , $delimiter) {
    $rawdata = array();
    $fd = fopen($rawfile, "r");
    if ($fd) {
        $rawinfo = '';
        while (! feof ($fd) ) {
            $rawinfo .= fread($fd, 8192);
        }
        fclose($fd);
      $rawinfo = preg_replace("/\x0D/", "", $rawinfo);
      $rawinfo = preg_replace("/\x0A/", "", $rawinfo);
        $rawdata = explode ($delimiter, $rawinfo);
    } else {
        $rawdata[0]= -9999;
    }
    return $rawdata;
}
?>

Offline DW7240

  • Senior Contributor
  • ****
  • Posts: 225
    • The Vicarage Weather Feed
Re: Flatline-check.php script no send email
« Reply #1 on: March 08, 2021, 10:09:44 AM »
Hi,

The 2 lines you need to edit are as follows.....

$to = '***************@dw7240.com';               // EMail info
$from      = 'dw7240.com';

your email address goes in the $to line,
$from is where the email originates from, ie where the script lives.

Hope this helps, this is all I have in my setup and it works flawlessly.....

One more thing you may have to setup up email forwarding so that email from sendmail server goes to the new server (PHPMailer)

Cheers,

Nick. dw7240.com
« Last Edit: March 08, 2021, 10:12:47 AM by DW7240 »


Offline CarlosLSev

  • Member
  • *
  • Posts: 31
Re: Flatline-check.php script no send email
« Reply #2 on: March 09, 2021, 01:11:57 AM »
.Thanks finally uninstall PhPmailer and install Sendmail and now it works

 

anything