Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 5265  / 1 Year ago, thu, january 5, 2023, 6:57:58

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' . "
";
$headers .= 'Content-type: text/html; charset=utf-8' . "
";

mail("[email protected]",$subject,$message);


i set my cron job by typing like



crontab -e   //which opens `VI` editor


there i set



MAILTO="[email protected]"
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>
</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>


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



<?php 
$con=mysql_connect("localhost","root","");
mysql_select_db("dbname",$con);
$to="[email protected]"
$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' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
mail($to, $subject, $msg);
?>


please help


More From » php

 Answers
0

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



$to="[email protected]"
$subject = 'Count User Login And Application';


$headers = 'MIME-Version: 1.0' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
//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



MAILTO"[email protected];[email protected]"
* 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

[#24236] Saturday, January 7, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terneive

Total Points: 329
Total Questions: 117
Total Answers: 105

Location: Denmark
Member since Tue, Oct 18, 2022
2 Years ago
;