Link to home
Start Free TrialLog in
Avatar of wantabe2
wantabe2Flag for United States of America

asked on

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
Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

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.
You also might want to check your error_log file (if you have access to it) for recent error messages.
Avatar of stu215
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.
try without double quotes (they are unneeded)

mail($email, "Evaluation(s) due within 60 days", $reminder_details, $mailheader);
Avatar of wantabe2

ASKER

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....
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
To learn the correct ways of handling DATETIME values in PHP and MySQL you might want to read over this article.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
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

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...
Use single quotes around the datetime strings.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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
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