PHP Code - Where is my mistake?

The attached code is supposed to look at the eval_due_date in a mysql database & if the eval_due_date is within 60 days of todays date, it is supposed to send an email to myemail@gmail.org

When I run it it acts like it works & I get the attachment in the browser but I get no email??? Thanks for any help provided.
<?php
include('connection.php'); 

$number_of_days_before = 60;
$email = "myemail@gmail.org";
$reminder_details = "";
$todays_date = date( "Ymd" );
echo $todays_date;
$year = substr($todays_date, 0, 4);
$month = substr($todays_date, 4, 2);
$date = substr($todays_date, 6, 2);
$trigger_date = date("Ymd", mktime(0,0,0,$month, $date+$number_of_days_before,$year));
echo $trigger_date;

$result = mysql_query( "SELECT f_name, l_name FROM hr_info WHERE supervisor = 'john doe' AND eval_due_date BETWEEN $todays_date AND $trigger_date" );
$nr = mysql_num_rows( $result );

 if (mysql_errno()) {
  echo "MySQL error ".mysql_errno().": ".mysql_error()."\n<br>";

$reminder_details .= "Name: ".$row["f_name"]." ".$row["l_name"]."\n";
$reminder_details .= "Evaluation Due Date: ".$row["eval_due_date"]."\n\n\n";
}

if( !empty( $nr ) )
{

$mailheader = "From: Evaluation Reminder System <$email>\nX-Mailer: Reminder\nContent-Type:text/plain";
mail("$email", "Evaluation(s) due within 60 days", "$reminder_details", "$mailheader");

}
?>

Open in new window

attatch.JPG
LVL 15
wantabe2Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Marco GasiConnect With a Mentor FreelancerCommented:
$result = mysql_query( "SELECT f_name, l_name FROM hr_info WHERE supervisor = 'john doe' AND eval_due_date BETWEEN $todays_date AND $trigger_date" );
$nr = mysql_num_rows( $result );

 if (mysql_errno()) {
  echo "MySQL error ".mysql_errno().": ".mysql_error()."\n<br>";

while ($row = mysql_fecth_array($result)){
  $reminder_details .= "Name: ".$row["f_name"]." ".$row["l_name"]."\n";
  $reminder_details .= "Evaluation Due Date: ".$row["eval_due_date"]."\n\n\n";
}
}

Cheers
0
 
Hugh McCurdyCommented:
I don't see an obvious problem.  A good next step is to print debugging information.

if( !empty( $nr ) )
{

echo "attempting to send message<br />\n";

$mailheader = "From: Evaluation Reminder System <$email>\nX-Mailer: Reminder\nContent-Type:text/plain";
$sent = mail("$email", "Evaluation(s) due within 60 days", "$reminder_details", "$mailheader");

var_dump ( $sent ); echo "<br />\n";
var_dump ( $email ); echo "<br />\n";
var_dump ( $reminder_details); echo "<br />\n";
var_dump ( $mailheader); echo "<br />\n";

}

Open in new window


Caveat: I didn't test my additions.

If the problem becomes obvious after that, then fix it.  If not, copy/paste the relevant output here.
0
 
Hugh McCurdyCommented:
You also might want to check your error_log file (if you have access to it) for recent error messages.
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
stu215Systems AnalystCommented:
Have you tried sending a basic mail to see if you can send mail at all from the server?
<?php
// The message
$message = "Line 1\nLine 2\nLine 3";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70);

// Send
mail('yourEmail@example.com', 'My Subject', $message);
?> 

Open in new window


Did you try checking your mailError.log file to see if maybe the issue is not with the script?
http://help.yahoo.com/l/us/yahoo/smallbusiness/webhosting/php/php-03.html

You'll find your error logs for PHP and Perl mail in your tmp subdirectory, in a log file called mailError.log. Messages will be saved to this file automatically whenever an error occurs.
0
 
Marco GasiFreelancerCommented:
try without double quotes (they are unneeded)

mail($email, "Evaluation(s) due within 60 days", $reminder_details, $mailheader);
0
 
wantabe2Author Commented:
okay....it has started emailing me now when I run the script. There must have been something wrong with the mail server (backlogged or something) I got numerous emails. okay, now that I'm getting the emails I get the subject line I have in the code but no results int eh body of the email....
0
 
wantabe2Author Commented:
marqusG
still the same thing..I get an email but with no results. There is data within the date range that should be sent via the email but it is not getting put inthe body of the message.
0
 
Ray PaseurCommented:
To learn the correct ways of handling DATETIME values in PHP and MySQL you might want to read over this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
 
Ray PaseurCommented:
Try this and see if it makes sense to you.  I am assuming that the column named eval_due_date is of DATETIME data type, right?  Sorry I cannot test this because I do not have your data base, but hopefully it will work or nearly work.  At least you will be able to see the query string, and know whether the query worked or not!
<?php
include('connection.php'); 

$number_of_days_before = 60;
$email                 = "myemail@gmail.org";
$reminder_details      = NULL;
$todays_date           = date("c");
$trigger_date          = date("c", strtotime($todays_date . ' + ' .$number_of_days_before . ' DAYS'));

$query  = "SELECT f_name, l_name FROM hr_info WHERE supervisor = 'john doe' AND eval_due_date BETWEEN $todays_date AND $trigger_date LIMIT 1";
$result = mysql_query($query);
if (!$result) die("QUERY FAILED: $query<br/>" . mysql_error() );
$row = mysql_fetch_assoc($result);
if ($row)
{
    $reminder_details .= "Name: ".$row["f_name"]." ".$row["l_name"]."\n";
    $reminder_details .= "Evaluation Due Date: ".$row["eval_due_date"]."\n\n\n";
}
else
{
    die("NOTHING FOUND BY QUERY $query");
}
echo "SUCCESS: $query";
$mailheader = "From: Evaluation Reminder System <$email>\nX-Mailer: Reminder\nContent-Type:text/plain\r\n";
mail("$email", "Evaluation(s) due within 60 days", "$reminder_details", "$mailheader");

Open in new window

0
 
wantabe2Author Commented:
still nothing:

QUERY FAILED: SELECT f_name, l_name FROM hr_info WHERE supervisor = 'john doe' AND eval_due_date BETWEEN 2011-09-30T18:14:03+00:00 AND 2012-09-28T18:14:03+00:00 LIMIT 1
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':14:03+00:00 AND 2012-09-28T18:14:03+00:00 LIMIT 1' at line 1

There is data in the DB & the eval_due_date is the DATE data type. I change dit to DATETIME & ran the above code with no luck either...
0
 
Ray PaseurCommented:
Use single quotes around the datetime strings.
0
 
Ray PaseurConnect With a Mentor Commented:
But I am wondering... Did you run exactly the script I posted, or did you make some other changes?  Here is the script with the quotes in the right places in the query.  Sorry I cannot test any of this because I do not have your data base, so you will have to do the testing for us, and post the results back here.
<?php
include('connection.php'); 

$number_of_days_before = 60;
$email                 = "myemail@gmail.org";
$reminder_details      = NULL;
$todays_date           = date("c");
$trigger_date          = date("c", strtotime($todays_date . ' + ' . $number_of_days_before . ' DAYS'));

$query  = "SELECT f_name, l_name FROM hr_info WHERE supervisor = 'john doe' AND eval_due_date BETWEEN '$todays_date' AND '$trigger_date' LIMIT 1";
$result = mysql_query($query);
if (!$result) die("QUERY FAILED: $query<br/>" . mysql_error() );
$row = mysql_fetch_assoc($result);
if ($row)
{
    $reminder_details .= "Name: ".$row["f_name"]." ".$row["l_name"]."\n";
    $reminder_details .= "Evaluation Due Date: ".$row["eval_due_date"]."\n\n\n";
}
else
{
    die("NOTHING FOUND BY QUERY $query");
}
echo "SUCCESS: $query";
$mailheader = "From: Evaluation Reminder System <$email>\nX-Mailer: Reminder\nContent-Type:text/plain\r\n";
mail("$email", "Evaluation(s) due within 60 days", "$reminder_details", "$mailheader");

Open in new window

0
 
wantabe2Author Commented:
Ray,
Thanks for your help. Between my code & what you posted I was able to get it working 90%. The code below is only returning i result in the email I recieve....I know I probably don't have a ; or () somewhere but just can't seem to find it. Any suggestions before I award the points? Thanks again for your assistance.
<?php
include('connection.php'); 

$number_of_days_before = 364;
$email = "myemail@yahoo.com";
$reminder_details = "";
$todays_date = date( "Ymd" );
echo $todays_date;
$year = substr($todays_date, 0, 4);
$month = substr($todays_date, 4, 2);
$date = substr($todays_date, 6, 2);
$trigger_date = date("Ymd", mktime(0,0,0,$month, $date+$number_of_days_before,$year));
echo $trigger_date;

$result = mysql_query( "SELECT hrid, f_name, l_name, eval_due_date FROM hr_info WHERE supervisor = 'john doe' AND eval_due_date BETWEEN $todays_date AND $trigger_date" );

$row = mysql_fetch_assoc($result);
if ($row)
{
    $reminder_details .= "Name: ".$row["f_name"]." ".$row["l_name"]."\n";
    $reminder_details .= "Evaluation Due Date: ".$row["eval_due_date"]."\n";
}

$mailheader = "From: Evaluation Reminder System <$email>\nX-Mailer: Reminder\nContent-Type:text/plain\r\n";
mail("$email", "Evaluation(s) due within 60 days", "$reminder_details", "$mailheader");

Open in new window

0
 
wantabe2Author Commented:
oh, one more thing to add...the one result I get back in the email is not the one you'd think it would be. It skips October & returns an entry for December... hth
0
 
wantabe2Author Commented:
I was able to get this working by using the code posted by ray & by adding the while( $row = mysql_fetch_array( $result ) ) to the code.
<?php
include('connection.php'); // DB connection
// Constants
$number_of_days_before = 60;
$email = "email@yahoo.com";
$reminder_details = "";
$todays_date = date( "Ymd" );
echo $todays_date;
$year = substr($todays_date, 0, 4);
$month = substr($todays_date, 4, 2);
$date = substr($todays_date, 6, 2);
$trigger_date = date("Ymd", mktime(0,0,0,$month, $date+$number_of_days_before,$year));
echo $trigger_date;

$result = mysql_query( "SELECT f_name, l_name, eval_due_date FROM hr_info WHERE supervisor = 'Jane Doe' AND eval_due_date BETWEEN $todays_date AND $trigger_date" );
$nr = mysql_num_rows( $result );

$row = mysql_fetch_assoc($result);
while( $row = mysql_fetch_array( $result ) )
if ($row)
{
    $reminder_details .= "Name: ".$row["f_name"]." ".$row["l_name"]."\n";
    $reminder_details .= "Evaluation Due Date: ".$row["eval_due_date"]."\n";

}
$mailheader = "From: Evaluation Reminder System <$email>\nX-Mailer: Reminder\nContent-Type:text/plain\r\n";
mail("$email", "Evaluation(s) due within 60 days", "$reminder_details", "$mailheader");

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.