I am stuck when sending email in correct format from cron jobs executing my php script which fetches records from mysql database.

i have a php script which fetches records from database and renders in <html> format when echod in browser like

StartTime   EndTime     Count User  Count Apps
12:00:00    12:59:59      0             0
01:00:00    01:59:59      0             0
02:00:00    02:59:59      0             0
03:00:00    03:59:59      0             0
04:00:00    04:59:59      0             0
05:00:00    05:59:59      0             0
06:00:00    06:59:59      0             0
07:00:00    07:59:59      0             0
08:00:00    08:59:59      0             0

in my php script i use simple mail() and to send mail i use headers like

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";


i set my cron job by typing like

crontab -e   //which opens `VI` editor

there i set

10 * * * * php /var/www/html/xyz/myfile.php

its sending mail every hour past 10 min but the format goes wrong.its sending mail in a format like

<html><head><title>Count User Info TimeWise</title></head><h2>Count     User/Application in CurrentDate</h2><body><table border="3" cellspacing="2">

 <tr><th>StartTime</th><th>EndTime</th><th>Count User</th><th>Count Apps</th>

how can i send mail in the correct format like shown when its echoed in browser?and how can i set cron to send email from 1pm to 11pm .

my php script

$subject = 'Count User Login And Application';

//fetch between 06:00:00 to 08:30:00   09:00:00 to 10:00:00

$msg  = '<html><head>';
$msg .='<title>Some Title</title>';
$msg .='</head>';
$msg .='<h1>Test User</h1>';
$msg .='<table border="1" cellspacing="1">';

$msg .=  "<tr>";
$msg .=  "<th>start time</th>";
$msg .=  "<th>end time</th>";
$msg .=  "<th>Count</th>";


$sql="SELECT count(*) AS test FROM table_name WHERE DATE_FORMAT(sys_time,'%H:%m:%i') BETWEEN DATE_FORMAT(sys_time,'$date[$i]') AND  DATE_FORMAT(sys_time,'$date1[$i]') ";
    die('could not connect'.mysql_error());}

while($row=mysql_fetch_array($query)) {

    $msg .=  "<tr>";
    $subcategory = explode(',', $str);
    foreach($subcategory as $value) 
        $msg .=  "<td>" . $value . "</td>";
    $msg .=  "</tr>";
$msg .=  "</table>";      
$msg .=  "</html>";

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $msg);

please help

  • The mail command in PHP has no relation to the mail cron sends. cron emails anything that was printed to stdout. – saiarcot895 Jul 7 '14 at 19:04
  • To have a cron job execute every hour from 1 pm to 11 pm, start the line with 0 13-23 * * *. – Jos Jul 7 '14 at 19:11
  • @Jos thanks.then how to send email in correct format saiarcot895 .i have also tried with removing the mail() from php script and but no success. – hitlooser Jul 7 '14 at 19:25
  • For this we would need to see the php script. The formatting is done by the script, certainly not by cron. – Jos Jul 7 '14 at 19:46
  • 1
    So you DO want the output to be sent as HTML, only the browser doesn't render the mail as HTML. Is that correct? You don't actually send the $headers anywhere, do you? These are needed for your browser to interpret the mail body as HTML. – Jos Jul 7 '14 at 21:55

For CentOs to install cron type

yum install cronie  

In your php script put the header section of $header above assigning $msg to intercept it as html content before it really assign $msg with HTML so it will be

$subject = 'Count User Login And Application';

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//try adding one more $headers with sender mail address and pass it as fourth     parameter in mail()

mail($to, $subject, $msg ,$headers);

crontab -l   //check wheather a cron tab for this user already runs
crontab -e   //to edit cron or new cron job

opens VI

* 1-11 * * * php /location/of /your/file/script.php 

press ESC then capsLock ON type ZZ from keyboard you are done

 `crontab -l`  //u can see listed cronjob
| improve this answer | |
  • This is a copy/paste without giving due credits. Besides that: the 1st part is generic and we only accept Ubuntu based answer + crontab is already installed on Ubuntu. I also consider using /etc/crontab over crontab to be the wiser choice: whenever you can prevent using root you should on sudo-enabled systems. – Rinzwind Jul 10 '14 at 9:29

You should just use the PHP mail function, which your script already tries to do. Look up the syntax for the mail function and you will see that there is an additional parameter $headers. The script actually constructs these headers but does not use them. The headers are necessary to tell your email client that what follows is HTML and not plain text.
The fact that cron can send its output to an email address is not relevant and you should not rely on it. Systems administrators can use this facility as a last resort, to be certain that any unexpected output from a cron job is brought to their attention. It was never meant as a full-fledged mailing facility such as offered by PHP.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.