Я пытаюсь настроить автоматическую электронную почту на конец дня с помощью задания CRON.
Я могу заставить задание CRON отправлять статическое электронное письмо, однако у меня возникают проблемы при попытке использовать file_get_contents и страницу php, которая затем использует запросы mysql для получения данных из базы данных и построения тела письма.
Нужна помощь!
* ПРИМЕЧАНИЕ. Я использую специальный php-почтовик для SMTP. Этот почтовик работает на 100% FINE при отправке любых статических писем или отправке динамических писем вручную (не CRON).
Просто возникают проблемы с получением задания CRON для фактического доступа к файлу PHP и получения данных.
Файл почтовика, запускаемый CRON:
#!/usr/bin/php
<?php
set_include_path('/Library/WebServer/Documents/pbhsadmin/');
include '_classes/class.phpmailer.php';
$email = 'EMAIL';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$mail->Subject = "PBHS Administration - Changes Department Daily Report";
$body = file_get_contents('http://localhost/pbhsadmin/_mail/changes_daily.php');
//$body = 'Testing no get file';
$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->From = "support@pbhs.uservoice.com";
$mail->FromName = "PBHS Support";
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "mail.SERVER.com"; // SMTP server
$mail->Username = "EMAIL" // SMTP server username
$mail->Password = "PASSWORD"; // SMTP server password
$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("support@pbhs.uservoice.com","PBHS Support");
$mail->AddAddress($email);
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'message sent!';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
PHP-страница, использующая mysql для запроса базы данных и создания электронной почты:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Changes Department - Daily Report</title>
</head>
<body>
<?PHP
set_include_path('/Library/WebServer/Documents/pbhsadmin/');
include 'SQL CONNECTION FILE';
$query = "QUERY";
$result = mysql_query($query)or die(mysql_error());
while($row=mysql_fetch_array($result)){
if($row['userid']==0)
$username = 'Unassigned';
else
$username = $row['username'];
$userid = $row['userid'];
//GET ALL PROJECTS WORKED ON FOR THIS USER
$query2 = "QUERY";
$result2 = mysql_query($query2)or die(mysql_error());
echo '
<div class="employee">
<h1>'.$username.'</h1>
<table><tr><th>Site</th><th>Hours Worked</th><th>Total Hours</th><th>Status</th></tr>';
while($row2=mysql_fetch_array($result2)){
$domain = $row2['domain'];
$hours = $row2['hours'];
if($row2['completed']!='0000-00-00 00:00:00')
$status = 'Completed';
else
$status = 'Incomplete';
$total_hours = $row2['act_hours'];
echo '<tr><td class="center">'.$domain.'</td><td class="center">'.$hours.'</td><td class="center">'.$total_hours.'</td><td class="center '.$status.'">'.$status.'</td></tr>';
}
if(mysql_num_rows($result2)==0)
echo '<tr><td colspan="4" style="text-align:center;"><i>No Projects Worked On</i></td></tr>';
echo '</table></div>';
}
?>
</body>
</html>