Email is sent only if first condition is true

esp32 with sensor bme280 sends values to xampp server which records values in mysql database every minute…

Email is sent only when both conditions are true value1 < 25 and value2 < 25

If only second value is true value2 < 25 email is not sent

mark where elseif condition begins

. Please help :slight_smile:

$subject = 'πŸ‘€ πŸ”₯ ESP32 BME280 Readings Temperature πŸ”₯ πŸ‘€';
$subject1 = 'πŸ‘€ 🌊 ESP32 BME280 Readings Humidity 🌊 πŸ‘€';
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 
$headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
// api key is defined that is not the problem. Data is recorded every minute
// $api_key = $value1 = $value2 = $value3 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $api_key = test_input($_POST["api_key"]);
    if($api_key == $api_key_value) {
        $value1 = test_input($_POST["value1"]);
        $value2 = test_input($_POST["value2"]);
        $value3 = test_input($_POST["value3"]);
        $message = '<html><body style="text-shadow:1px 1px #ffffff;background-color: #000000;">';
		$message .= '<div style="position:fixed;top:0px;left:0px;">';
		$message .= '<center><h2 style="color:#ff0000;">Temperature is '. $value1 .'&deg;C - '. (($value1)-40) .'&deg;C above treshold...</h2><br>';
		$message .= '<center><img src="https://store.canterlotavenue.com/file/file/pic/photo/2018/12/96413f50d7ece7340a59e29ca17d67ec_1024.gif" width="444" height="250"></img></center>';
		$message .= '<center><p style="font-weight:900;color:#ff0000;">Temperature: '. $value1 .'&deg;C</p></center>';
		$message .= '<center><p style="font-weight:900;color:#0000ff;">Humidity: '. $value2 .'&#37;</p></center>';
		$message .= '<center><p style="font-weight:900;color:#dedede;">Pressure: '. $value3 .'hPa</p></center>';
		$message .= '<center><p style="font-weight:900;color:#ff0000;">Treshold: '. (($value1)-40) .'&deg;C</p></center>';
		$message .= '</div>';
		$message .= '</body></html>';
		
		$msg = '<html><body style="text-shadow:1px 1px #0000ff;background-image: linear-gradient(180deg,rgba(255,255,255,0), rgba(172,254,253,1));">';
		$msg .= '<div style="position:fixed;top:0px;left:0px;">';
		$msg .= '<center><h2 style="color:#0000ff;">Humidity is '. $value2 .'&#37; - '. (($value2)-20) .'&#37; above treshold...</h2><br>';
		$msg .= '<center><img src="https://acegif.com/wp-content/gifs/water-43.gif" width="620" height="333"></img></center>';
		$msg .= '<center><p style="font-weight:900;color:#ff0000;">Temperature: '. $value1 .'&deg;C</p></center>';
		$msg .= '<center><p style="font-weight:900;color:#0000ff;">Humidity: '. $value2 .'&#37;</p></center>';
		$msg .= '<center><p style="font-weight:900;color:#000000;">Pressure: '. $value3 .'hPa</p></center>';
		$msg .= '<center><p style="font-weight:900;color:#ff0000;">Treshold: '. (($value2)-20) .'&#37;</p></center>';
		$msg .= '</div>';
		$msg .= '</body></html>';
        
        if($value1 < 40){
            echo "Temperature too high...";
            exit;
        }
		if(mail($to, $subject, $message, $headers)){
    echo 'Your mail has been sent successfully.';
} 

        elseif($value2 < 20){
            echo "Humidity too high...";
            exit;
        }
        
		if(mail($to, $subject1, $msg, $headers)){
    echo 'Your mail has been sent successfully.';
}

else{
    echo 'Unable to send email. Please try again.';
}

         
}
}
else {
    echo "No data posted with HTTP POST.";
}
Sponsor our Newsletter | Privacy Policy | Terms of Service