LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   coding problem on php auto send mail after a period (https://www.linuxquestions.org/questions/programming-9/coding-problem-on-php-auto-send-mail-after-a-period-358388/)

akamaru607 08-30-2005 03:35 AM

coding problem on php auto send mail after a period
 
hi all, im trying to develop a webased helpdesk, it will auto send a email to engineer after 1 day, 3 day, 5 day & 7day if the status of the report is open. both my crontab and php mail function are working well. now my problem is no idea how to set the condition in php about the date it will send email after the period. hope someone can help me and very appreciate with that, thanx alot.

tomj88 08-30-2005 04:54 AM

Hey, not too sure on your problem, I don't quite understand what these reports are :S, but you may need to have a read up on the php date function @ http://php.net/date.
Below is a simple program I have wrote for you to demonstrate how to work out the previous date.
PHP Code:

<?php
$date
=date(Ymd);
echo 
$date '<br />';
echo 
$date '<br />';
echo 
$date -'<br />';
echo 
$date '<br />';
echo 
$date '<br />';
echo
?>

This works best if you specify the date in the format above, Year month day. This is because you can easily subtract a day of or add a day if it's at the end of the string.

There are some problems with this however, if you look at the output:

Code:

20050830
20050829
20050828
20050831
20050832

You can see here that it is saying that the date + 2 will be the 32nd of august, which is an imposibility. Is there a php function that does this more succesffully anyone?

akamaru607 08-31-2005 04:36 AM

hi tomj88, thanx u so much, u did help me a lot :)
im facing the problem too which the date will not move into nest month and it become 32th after adding the date. hey guyz do u know what to do on this problem, plz give me a hand on it :)

tomj88 08-31-2005 04:47 AM

I suppose you could make some huge switch case or if else statement that checks this, basically make a calender, but this may become highly complicated...

tomj88 08-31-2005 04:59 AM

I haven't coded in php much recently, so the syntax is probably wrong...

If you build up an array of the months and how many days they have, you can address the arrays later to shorten the code.
PHP Code:

$31daymonths = ["January""March""May""July""August""October""December"];
$
30daymonths = ["April""June""September""November"];

if(
$year == 0){
   $
29daymonth "April";
}else{
   $
28daymonth "April";


I'm not sure about this last bit, I always forget how to use the remainder syntax. Basically, If the current year / 4 does not give us a fraction, then april has 29 days.

I would say it would be best to run a for or do while or while loop when you are adding, and only add 1 each time (or reduce). This would be better because instead of going right over, say up to day 35, you could check on each itteration if the day is too high, and then adjust accordingly.

I hope this helps... I will try to build a simple callender later on tonight if you don't manage it now ;). Keep me updated on how its going.

tomj88 08-31-2005 01:29 PM

Quote:

[i]from http://uk.php.net/date
z The day of the year (starting from 0) 0 through 365
If you used this instead, then it would be far easier, just add a check in to see if its from the same year, and whether the year is a leap year.

akamaru607 08-31-2005 01:43 PM

thanx so much. im trying now, hope everything go well :)

tomj88 08-31-2005 01:55 PM

This doesn't solve your problem, but it shows you how you could possibly solve it.
PHP Code:

<?php
        
# value to add
    
$value 150;
        
# day (0 to 365)
    
$day date('z');
        
# the year
    
$year date('Y');

        
# check to see if it is a leap year
    
if ($year == 0){
        
$leap 1;
    }else{
        
$leap 0;
    }

        
# works out whether after adding the value
        # it is a new year
    
if ($leap == 1){
        if ((
$day $value) > 365){
            
$newday= ($day $value);
            
$newday =- 365;
            
$newyear = ++$year;
        }else{
            
$newday= ($day $value);
            
$newyear $year;
        }
    }else{
        if ((
$day $value) > 364){
            
$newday = ($day $value);
            
$newday =- 365;
            
$newyear = ($year 1);
        }else{
            
$newday = ($day $value);
            
$newyear $year;
        }
    }

       
# echo the data
echo $day " & " $newday '<br />';
echo 
$year " & " $newyear '<br />';

?>

Hope this helps!

akamaru607 08-31-2005 02:36 PM

thanks for ur help. i just found a code, i think this can help me out, hopefully :D
PHP Code:

<?
$tommorrow 
time() + (24 60 60);
$nextWeek time() + (24 60 60);
$threedays time() + (24 60 60);
$fivedays time() + (25 60 60);
                   
// 7 days; 24 hours; 60 mins; 60secs
echo 'Now:          'date('Y-m-d') ."<br>";
echo 
'Next Week:    'date('Y-m-d'$nextWeek) ."<br>";
echo 
'Tommorrow:    'date('Y-m-d'$tommorrow) ."<br>";
echo 
'After 3 days: 'date('Y-m-d'$threedays) ."<br>";
echo 
'After 5 days: 'date('Y-m-d'$fivedays) ."<br>";

?>

this is the output
Quote:

Now: 2005-02-28
Next Week: 2005-03-07
Tommorrow: 2005-03-01
After 3 days: 2005-03-03
After 5 days: 2005-03-05
anyway tomj88, really thanks on trying to help me :)

tomj88 08-31-2005 08:54 PM

edit: Removed this post, its the same as next but I had a little problem in the code in this one, and the second one I though I clicked edit but I must have clicked new post... sorry!

tomj88 08-31-2005 08:54 PM

I was bored so I tried to implement this for you. I think you should be able to understand this, and I hope that it fulfils your needs. I'm sure there are better ways of doing this, just a quick little hack :)
PHP Code:

<?php

# a function to check a report:
# will send an email to an admin if a report has
# been open for a day, 3 days, 5 days or anything
# greater than 7 days

# syntax: checkreport("A simple report", 365, 2005);
# first is the name of the report, next is the day the report
# was opened on, and the next is the year it was opened on
# note, all days here are in z format i.e. 0 to 365
function checkreport($reportname$openday$openyear){    
    
    
# get the date in the format 0 to 365
    
$day = ('z');
    
# get the year
    
$year = ('Y');
      
    
# is it a leap year?
    
if ($year == 0){
        
$leap 1;
    }else{
        
$leap 0;
    }
    
    
# after how many days should the emails be sent?
    
$emaildays = array(1357);
    
$emaildayscount count($emaildays);

    
# Checks once you add the $emailday to the $openday that
    # the number is still within the 0 to 365 limit of a year.
    # checks for leap years, and uses a for loop to itterate over the
    # amount of days listed in $emaildays (uses $emaildayscount)
    
for($i 0$i $emaildayscount$i++){
        
$value $emaildays[i];
        if (
$leap == 1){
            if ((
$openday $value) > 365){
                
$newopenday[i] = $openday $value;
                
$newopenday[i] =- 365;
                
$newyear[i] = $year +1;
            }else{
                
$newopenday[i] = $openday $value;
                
$newyear[i] = $year;
            }
        }else{
            if ((
$openday $value) > 364){
                
$newopenday[i] = $openday $value;
                
$newopenday[i] =- 364;
                
$newyear[i] = $year 1;
            }else{
                
$newopenday[i] = $openday $value;
                
$newyear[i] = $year;
            }
        }
    }

    
# run a for loop to check if an email has to be sent
    
for($i 0$i $emaildayscount$i++){
        
    if(
$day+$emaildays[i]>=$newopenday[i]){
            
email($reportname);
            break;
        
# if say, it is the start of a new year, 
        # then the above might not send the email
        # this checks if $openyear was before but
         # not equal to this $year
        
}else if($openyear<$year){
            
email($reportname);
            break;
        }
    }
}

# replace this function with a command that sends an email
function email($reportname){
    echo 
$reportname " needs checking!";
}
# run a check
checkreport("A simple report"02005);
?>


akamaru607 09-01-2005 02:26 AM

been trying whole day, finally get something :D now i using crontab run this page 10am everyday.
PHP Code:

<?

// receiver for case still not close after 1 day 
$to1  'dyer607@hotmail.com'

// receiver for case still not close after 3 days 
$to3  'dyer607@hotmail.com'

// receiver for case still not close after 5 days 
$to5  'dyer607@hotmail.com'

// receiver for case still not close after one week 
$to7  'dyer607@hotmail.com'

// subject for email
$subject 'Problem still not solve';


// To send HTML mail, the Content-type header must be set
$headers 'From: [email]dyer607@hotmail.com[/email]' "\r\n" .
   
'Reply-To: [email]dyer607@hotmail.com[/email]' "\r\n" .
   
'X-Mailer: PHP/' phpversion();

//define the period
$oneday time() - (24 60 60);
             
//1 day ; 24 hours ; 60 mins ; 60 secs
$nextweek time() - (24 60 60);
                     
//7 days ; 24 hours ; 60 mins ; 60 secs
$threedays time() - (24 60 60);
             
//3 days ; 24 hours ; 60 mins ; 60 secs
$fivedays time() - (25 60 60);
                     
//5 days ; 24 hours ; 60 mins ; 60 secs


?>

<?

$connection 
mysql_connect("localhost""root""root");



$db "reminder";



mysql_select_db($db$connection) or die( "Could not open $db");

$sql "SELECT * FROM reminderdate";

$result mysql_query($sql$connection) or 

            die(
"Could not execute sql: $sql");

$num_result mysql_num_rows($result);

for (
$i=0$i $num_result$i++)

{

  
$row mysql_fetch_array($result);

  
$id $row["reminderid"];
  if (
$row["casedate"] == date('Y-m-d'$oneday))
  {
        
// message for case after 1 day
    
$message1  '<html><body><p>Service Report after 1 day</p><table cellspacing=3 cellpadding=3>';
       
$message1 .= '<tr><th>ReportID</th><td>' .$row["reminderid"];
    
$message1 .= '</td></tr><tr><th>Case Date</th><td>' .$row["casedate"]; 
    
$message1 .= '</td></tr><tr><th>Location</th><td>Netrixs</td>';
    
$message1 .= '</tr><tr><th>Assigner</th><td>Joe</td></table></body></html>';
    
// Mail it
    
mail($to1$subject$message1$headers);  
  }
  elseif (
$row["casedate"] == date('Y-m-d'$threedays))
  {
        
// message for case after 3 days
    
$message3  '<html><body><p>Service Report after 3 days</p><table cellspacing=3 cellpadding=3>';
       
$message3 .= '<tr><th>ReportID</th><td>' .$row["reminderid"];
    
$message3 .= '</td></tr><tr><th>Case Date</th><td>' .$row["casedate"]; 
    
$message3 .= '</td></tr><tr><th>Location</th><td>Netrixs</td>';
    
$message3 .= '</tr><tr><th>Assigner</th><td>Joe</td></table></body></html>';
    
// Mail it
    
mail($to3$subject$message3$headers);  
  }
  elseif (
$row["casedate"] == date('Y-m-d'$fivedays))
  {
        
// message for case after 5 days
    
$message5  '<html><body><p>Service Report after 5 days</p><table cellspacing=3 cellpadding=3>';
       
$message5 .= '<tr><th>ReportID</th><td>' .$row["reminderid"];
    
$message5 .= '</td></tr><tr><th>Case Date</th><td>' .$row["casedate"]; 
    
$message5 .= '</td></tr><tr><th>Location</th><td>Netrixs</td>';
    
$message5 .= '</tr><tr><th>Assigner</th><td>Joe</td></table></body></html>';
    
// Mail it
    
mail($to5$subject$message5$headers);  
  }
  elseif (
$row["casedate"] == date('Y-m-d'$nextweek))
  {
        
// message for case after one week
    
$message7  '<html><body><p>Service Report after 7 days</p><table cellspacing=3 cellpadding=3>';
       
$message7 .= '<tr><th>ReportID</th><td>' .$row["reminderid"];
    
$message7 .= '</td></tr><tr><th>Case Date</th><td>' .$row["casedate"]; 
    
$message7 .= '</td></tr><tr><th>Location</th><td>Netrixs</td>';
    
$message7 .= '</tr><tr><th>Assigner</th><td>Joe</td></table></body></html>';
    
    
// Mail it
    
mail($to7$subject$message7$headers);  
  }
  
}
?>

this is the sample output i get from the email
Quote:

Service Report after 3 days
ReportID 2
Case Date 2005-08-29
Location Netrixs
Assigner Joe
Date 2005-09-01

tomj88 09-01-2005 07:48 AM

cool, glad to here you got it working mate!

Tom


All times are GMT -5. The time now is 10:30 PM.