Home > Enterprise >  Send mail if specific condition
Send mail if specific condition

Time:11-10

I am working on a small project but I am facing issue which I cannot solve by myself. Long story short: I have a MariaDB database with a lot of information inside and I am printing this information in frontedn in table:

code here:

<center> <table id="table_id" >

    <tr>
        <th>Номер</th> <th>Статус</th> <th>Найменование</th> <th>Сериен номер</th> <th>Локация</th> <th>Отговорник</th> <th>Калибриран на</th> <th>Следваща калибрация</th> <th>Калибрира се при</th> <th>Интервал</th> <th>Оставащи дни</th> <th>Действия</th>
    </tr>
    <?php
    
    while($user_data = mysqli_fetch_array($result)) 
{
    echo "<tr>";
    echo "<td>".$user_data['toolnr']."</td>";
    echo "<td>".$user_data['status']."</td>";
    echo "<td>".$user_data['toolname']."</td>";
    echo "<td>".$user_data['serial']."</td>";
    echo "<td>".$user_data['usedat']."</td>";
    echo "<td>".$user_data['owner']."</td>";
    echo "<td>".$user_data['calibrated']."</td>";
    echo "<td>".$user_data['nextcalibration']."</td>";
    echo "<td>".$user_data['vendors']."</td>";
    echo "<td>".$user_data['lifetime']."</td>";
    echo "<td>";
    $toolnqkakav = $user_data['toolnr'];
    $days=$user_data['days'];
    if($days <= 30){
        echo "<span style='color:red;'>$days</span>";
    }
    else{
        echo "<span style='color:green;'>$days</span>";
    }
    echo "</td>";
    echo "<td><a href='edit.php?id=$user_data[id]'><img src='img/edit.png' ></a> | <a href='delete.php?id=$user_data[id]' onclick='return checkDelete()'><img src='img/delete.png'></a></td></tr>";

So I want to make "mail function" in this case as follow: If for some of the records:

`$days <= 30; // to send email to the $owner of this record.`

I have tryed this:

`if ($days<=30)
{
ini_set('SMTP', "smtp.server.int");
ini_set('smtp_port', "25");
ini_set('sendmail_from', "[email protected]");
$to_email = "[email protected]";
$subject = "The period is expiring";
$body = "Tool ".$toolnr." expiring ";
$headers = "From: MyTools ";
 
if (mail($to_email, $subject, $body, $headers))
 
{
    echo "Email successfully sent to $to_email...";
}
 
else
 
{
    echo "Email sending failed!";
} 
}`

In this case I am getting Emails, but the goal is for every record to get separate email to the specific $owner. .Here is my query to get the data from the database:

`SELECT *, DATEDIFF(nextcalibration, CURDATE()) AS days FROM tools AS dp where isdeleted <> 'Deleted' `

Any support is higly evaluated.

And for example if $toolnr "213454" with id in the databse "23" have $days<=30 to send email to the $owner

EDIT:

Here is the full code:

    <?php
// We need to use sessions, so you should always start sessions using the below code.

session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
     header('Location:login.html');
    exit;
}
?>

<?php
// Create database connection using config file
include_once("config.php");

// Fetch all users data from database
$result = mysqli_query($mysqli, "SELECT *, DATEDIFF(nextcalibration, CURDATE()) AS days FROM tools AS dp where isdeleted <> 'Deleted' ");

?>

<html>
<head>
    <title>Homepage</title>
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">
    <style type="text/css">
        .wrapper{
            width: 500px;
            margin: 0 auto;
        }
    </style>    
    <center> <img src="logo-ottobock.png" alt="Logo"> <br>
    <i> Calibration management system - Bulgaria </i> <br><br>
<input type="submit"  onclick="window.location.href='../';"  value="Обратно" > 
<input type="submit"  onclick="window.location.href='../add/add.php';"  value="Добави нов" >

    </center>
    <hr>
</head>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script language="JavaScript" type="text/javascript">
function checkDelete(){
    return confirm('Сериозно! Потврждавате ли изтриването ?');
}
</script>
<body>
    <center> <table id="table_id" >

    <tr>
        <th>Номер</th> <th>Статус</th> <th>Найменование</th> <th>Сериен номер</th> <th>Локация</th> <th>Отговорник</th> <th>Калибриран на</th> <th>Следваща калибрация</th> <th>Калибрира се при</th> <th>Интервал</th> <th>Оставащи дни</th> <th>Действия</th>
    </tr>
    <?php
    
    while($user_data = mysqli_fetch_array($result)) 
{
    echo "<tr>";
    echo "<td>".$user_data['toolnr']."</td>";
    echo "<td>".$user_data['status']."</td>";
    echo "<td>".$user_data['toolname']."</td>";
    echo "<td>".$user_data['serial']."</td>";
    echo "<td>".$user_data['usedat']."</td>";
    echo "<td>".$user_data['owner']."</td>";
    echo "<td>".$user_data['calibrated']."</td>";
    echo "<td>".$user_data['nextcalibration']."</td>";
    echo "<td>".$user_data['vendors']."</td>";
    echo "<td>".$user_data['lifetime']."</td>";
    echo "<td>";
    
    $days=$user_data['days'];
    if($days <= 30){
        echo "<span style='color:red;'>$days</span>";
    }
    else{
        echo "<span style='color:green;'>$days</span>";
    }
    echo "</td>";
    echo "<td><a href='edit.php?id=$user_data[id]'><img src='img/edit.png' ></a> | <a href='delete.php?id=$user_data[id]' onclick='return checkDelete()'><img src='img/delete.png'></a></td></tr>";
}
/* mail function
if ($days<=30)
{
ini_set('SMTP', "smtp.server.int");
ini_set('smtp_port', "25");
ini_set('sendmail_from', "[email protected]");
$to_email = "[email protected]";
$subject = "The period is expiring";
$body = "Tool ".$toolnr." expiring ";
$headers = "From: MyTools ";
 
if (mail($to_email, $subject, $body, $headers))
 
{
    echo "Email successfully sent to $to_email...";
}
 
else
 
{
    echo "Email sending failed!";
} 
}
 end mail function */
?>
</table> </center>
</body>
<hr>
<footer> 
<center> <br>

When I use $toolnr and $user_data['owner'] the "system" is sending only one mail for only one record with $days<=30 not for all the tools witch are with $days<=30.

CodePudding user response:

Thank you everyone the final solution is working.

while($user_data = mysqli_fetch_array($result)) 
{
    echo "<tr>";
    echo "<td>".$user_data['toolnr']."</td>";
    echo "<td>".$user_data['status']."</td>";
    echo "<td>".$user_data['toolname']."</td>";
    echo "<td>".$user_data['serial']."</td>";
    echo "<td>".$user_data['usedat']."</td>";
    echo "<td>".$user_data['owner']."</td>";
    echo "<td>".$user_data['calibrated']."</td>";
    echo "<td>".$user_data['nextcalibration']."</td>";
    echo "<td>".$user_data['vendors']."</td>";
    echo "<td>".$user_data['lifetime']."</td>";
    echo "<td>";
    $toolnomer = $user_data['toolnr'];
    $otgovoren = $user_data['owner'];
    $imetool = $user_data['toolname'];
    $days=$user_data['days'];
    if($days <= 30){
        echo "<span style='color:red;'>$days</span>";
    }
    else{
        echo "<span style='color:green;'>$days</span>";
    }
    echo "</td>";
    echo "<td><a href='edit.php?id=$user_data[id]'><img src='img/edit.png' ></a> | <a href='delete.php?id=$user_data[id]' onclick='return checkDelete()'><img src='img/delete.png'></a></td></tr>";
    
    if ($days<=30)
    {
    ini_set('SMTP', "smtp.XXXX.int");
    ini_set('smtp_port', "25");
    ini_set('sendmail_from', "[email protected]");
    $to_email = "".$otgovoren."@XXX.com";
    $subject = "Изтичаща калибрация";
    $body = "Инструмент ".$toolnomer." с найменование ".$imetool.", намиращ се в зона ".$user_data['usedat']." изтича на ".$user_data['nextcalibration']."";
    $headers = "From: MyTools XXXX Bulgaria";
     
    if (mail($to_email, $subject, $body, $headers))
    {
        echo "Email successfully sent to $to_email...";
        }
     
    else {
        echo "Email sending failed!";
        } 
    }

}
  • Related