CRON-PHP-file_获取内容以运行PHP页面并使用CRON发送电子邮件

CRON-PHP-file_获取内容以运行PHP页面并使用CRON发送电子邮件,php,email,cron,file-get-contents,Php,Email,Cron,File Get Contents,我正在尝试使用CRON作业设置一个一天结束时的自动电子邮件 我可以让CRON任务发送一封静态电子邮件,但是我在尝试使用file_get_内容和一个php页面时遇到了麻烦,该页面随后使用mysql查询从数据库获取数据并构建电子邮件正文 需要帮助! *注意-我为SMTP使用了一个特殊的php邮件程序。当手动发送任何静态电子邮件或动态电子邮件(而不是CRON)时,此邮件器100%正常工作。 只是在获取CRON作业以实际访问PHP文件并获取数据时遇到了问题 由CRON运行的邮件程序文件: #!/usr/

我正在尝试使用CRON作业设置一个一天结束时的自动电子邮件

我可以让CRON任务发送一封静态电子邮件,但是我在尝试使用file_get_内容和一个php页面时遇到了麻烦,该页面随后使用mysql查询从数据库获取数据并构建电子邮件正文

需要帮助! *注意-我为SMTP使用了一个特殊的php邮件程序。当手动发送任何静态电子邮件或动态电子邮件(而不是CRON)时,此邮件器100%正常工作。 只是在获取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();
}
?>
#/usr/bin/php
使用mysql查询数据库和创建电子邮件的PHP页面:

<!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>

更改部门-每日报告

您的服务器可能未配置为在本地主机上侦听

尝试使用CRON作业从服务器运行此命令:

wget http://localhost/pbhsadmin/_mail/changes_daily.php > test.html
在此之后test.html的内容是什么?如果它看起来不正确,那么您很可能需要将整个域放在URL中,而不是本地主机中。

我实际上已经找到了答案。 错误出现在我在html电子邮件中的声明中。
必须是

您在
$mail->Username=“EMAIL
行中缺少一个“结束”不确定这是否发生在向SO发布时,但是如果您关闭了显示错误,这很可能是您问题的根源。是的,这只是我编辑了安全信息的结果。这个问题根本与邮件文件无关。如果我将$body=更改为一个字符串(即$body='testing'),电子邮件就可以正常发送。它与文件内容有关。我想这与CRON作业在服务器上实际处理php文件的路径或方式有关。我想我使用的php-smtp邮件程序对HTML标记非常敏感。