LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-30-2005, 03:35 AM   #1
akamaru607
LQ Newbie
 
Registered: May 2005
Location: Malaysia
Posts: 22

Rep: Reputation: 15
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.
 
Old 08-30-2005, 04:54 AM   #2
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
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?

Last edited by tomj88; 08-31-2005 at 01:58 PM.
 
Old 08-31-2005, 04:36 AM   #3
akamaru607
LQ Newbie
 
Registered: May 2005
Location: Malaysia
Posts: 22

Original Poster
Rep: Reputation: 15
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

Last edited by akamaru607; 08-31-2005 at 04:39 AM.
 
Old 08-31-2005, 04:47 AM   #4
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
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...
 
Old 08-31-2005, 04:59 AM   #5
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
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.

Last edited by tomj88; 08-31-2005 at 01:57 PM.
 
Old 08-31-2005, 01:29 PM   #6
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
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.
 
Old 08-31-2005, 01:43 PM   #7
akamaru607
LQ Newbie
 
Registered: May 2005
Location: Malaysia
Posts: 22

Original Poster
Rep: Reputation: 15
thanx so much. im trying now, hope everything go well
 
Old 08-31-2005, 01:55 PM   #8
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
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!
 
Old 08-31-2005, 02:36 PM   #9
akamaru607
LQ Newbie
 
Registered: May 2005
Location: Malaysia
Posts: 22

Original Poster
Rep: Reputation: 15
thanks for ur help. i just found a code, i think this can help me out, hopefully
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
 
Old 08-31-2005, 08:54 PM   #10
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
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!

Last edited by tomj88; 08-31-2005 at 08:58 PM.
 
Old 08-31-2005, 08:54 PM   #11
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
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);
?>

Last edited by tomj88; 08-31-2005 at 08:59 PM.
 
Old 09-01-2005, 02:26 AM   #12
akamaru607
LQ Newbie
 
Registered: May 2005
Location: Malaysia
Posts: 22

Original Poster
Rep: Reputation: 15
been trying whole day, finally get something 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

Last edited by akamaru607; 09-01-2005 at 02:28 AM.
 
Old 09-01-2005, 07:48 AM   #13
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
cool, glad to here you got it working mate!

Tom
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed to send mail using a remote smtp server with php s1mpl1c1ty Programming 4 09-19-2005 12:53 AM
Cannot send mail with mail() command in PHP 4.3.4, Apache 2.0.48, Mandrake Linux 10 arcanum Linux - General 3 06-27-2004 06:06 PM
send mail via PHP from website on apache. not working mdkelly069 Linux - Software 0 02-25-2004 12:15 PM
Send Mail Problem mallikarjun Linux - Networking 4 05-17-2002 09:22 AM
Problem with Send mail Buduu Linux - General 2 05-28-2001 01:22 PM


All times are GMT -5. The time now is 07:28 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration