Php – sending email from cron in html format not text

centos6cronemailPHP

i am trying to send email every 1 hour from cron job which is executing a php script that fetches data from database and shows in a table format.

i have CentOs 6.1 where i created my cron job.firstly i installed cronie from terminal yum install cronie.then i type crontab -e and opens VI editor

there i put my cron with a MAILTO like

MAILTO="someemail@ex.com"
10 * * * * php /var/www/html/myfile.php

its sending email every 1hour past 10min but its sending email in a text content which i dont want.

myfile.php

<?php 
$con=mysql_connect("localhost","root","");
mysql_select_db("dbname",$con);
$to="some@example.com"
$subject = 'Count User Login And Application';

//fetch between 06:00:00 to 08:30:00   09:00:00 to 10:00:00
$date=array('06:00:00','09:00:00');
$date1=array('08:30:00','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>";

$count=count($date);
for($i=0;$i<$count;$i++){

$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]') ";
$query=mysql_query($sql);

if(!$query){
die('could not connect'.mysql_error());}


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

$msg .=  "<tr>";
$str=$row['test'];
$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);
?> 

as i am using simple mail() in php with headers to render it in html but my cron is sending mail in text format and not html format.

i am trying to get email in table format like one get in from browser but i am getting in text format.

is it possible to get email in html format from cron

any other possibilities is always welcomed.i have tried every if's and but's that i can get from google but i cant find a way to work it.

Best Answer

According to the not accepted answers to a similar question it depends on which version of cron you're running.

Check man 5 crontab if setting the CONTENT_TYPE and/or CONTENT_TRANSFER_ENCODING variables is supported to change from the default plain text.

AFAIK in cronie on RHEL 6 it is and if the output from script is HTML formatted the following should work in your crontab:

MAILTO="someemail@example.com"
CONTENT_TYPE="text/html; charset=iso-8859-1"
10 * * * * php /var/www/html/myfile.php