Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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
share|improve this answer
    
thank you for response.by typing man 5 crontab from centos terminal i get a cronie user manual.i have append your lines in crontab.lets c ..cheers! –  hitlooser Jul 8 at 14:55
    
i am getting this in mail /bin/sh:-c line 0 enexpected EOF while looking for matching ``'/bin/sh:-c line1:syntax error:unexpected EOF –  hitlooser Jul 8 at 15:16
    
Check that your quotes are the correct ' " ' quotes and not two ' ` ' or even ' “ ' and ' ” ' quotes. And make sure that when you execute php /var/www/html/myfile.php from the commandline returns HTML and no errors. –  HBruijn Jul 8 at 15:39
    
OK just to be clear, the MAILTO in your crontab redirects the STDOUT and STDERR the script generates to your mail recipient. That is a completely different message from the email message sent from the php mail() function in the script that gets executed. –  HBruijn Jul 8 at 17:01
    
actually i have done a mistake in my script to throw errors like in the top of my script i have placed #!/user/bin/env php which i removed and the error is gone. and actually putting the headers $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; in top of my script after $to works for me :) –  hitlooser Jul 8 at 17:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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