Je suis bloqué lors de l'envoi d'un courriel dans le format correct à partir de cron
les travaux exécutant mes php
script qui récupère les enregistrements à partir de mysql
base de données.
J'ai un php
script qui va chercher les enregistrements dans la base de données et les rend en <html>
format lorsqu'il est affiché dans le navigateur comme
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
dans mon script php j'utilise simple mail() et pour envoyer le mail j'utilise des headers comme
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
mail("someemail@example.com",$subject,$message);
Je configure mon travail cron en tapant comme suit
crontab -e //which opens `VI` editor
Là, je mets
MAILTO="someemail@example.com"
10 * * * * php /var/www/html/xyz/myfile.php
Il envoie des messages toutes les heures et toutes les 10 minutes, mais le format est incorrect.
<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>
</tr><tr><td>12:00:00</td><td>12:59:59</td><td>1</td><td>1</td></tr><tr>
<td>13:00:00</td><td>13:59:59</td><td>2</td><td>2</td></tr><tr><td>14:00:00</td>
<td>14:59:59</td><td>2</td><td>2</td></tr><tr><td>15:00:00</td><td>15:59:59</td>
<td>2</td><td>2</td></tr><tr><td>16:00:00</td><td>16:59:59</td><td>2</td><td>2</td>
</tr><tr><td>17:00:00</td><td>17:59:59</td><td>2</td><td>2</td></tr><tr>
<td>18:00:00</td><td>18:59:59</td><td>2</td><td>2</td></tr><tr><td>19:00:00</td>
<td>19:59:59</td><td>2</td><td>2</td></tr><tr><td>20:00:00</td><td>20:59:59</td>
<td>1</td><td>1</td></tr><tr><td>21:00:00</td><td>21:59:59</td><td>0</td><td>0</td>
</tr><tr><td>22:00:00</td><td>22:59:59</td><td>0</td><td>0</td></tr></body>
</table></html>
comment puis-je envoyer un mail au format correct, comme celui qui est affiché dans le navigateur ? et comment puis-je configurer le cron pour qu'il envoie un mail de 13h à 23h ?
mon php
script
<?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);
?>
aidez-nous