繁体   English   中英

CRON-PHP-file_get_contents使用CRON运行PHP页面和电子邮件

[英]CRON - PHP - file_get_contents to run PHP page and email with CRON

我正在尝试使用CRON作业设置收尾自动电子邮件。

我可以得到CRON作业来发送静态电子邮件,但是尝试使用file_get_contents和php页面,然后使用mysql查询从数据库获取数据并构建电子邮件正文时遇到了麻烦。

需要帮忙! *注意-我为SMTP使用了特殊的php邮件程序。 当发送任何静态电子邮件或手动发送动态电子邮件(不是CRON)时,该邮件程序的FINE效果为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();
}
?>

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>

我真的想通了。 错误是我在html电子邮件中的声明中。 它必须是<style type="text/css">

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

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

wget http://localhost/pbhsadmin/_mail/changes_daily.php > test.html

之后的test.html的内容是什么? 如果它看起来不正确,则很可能需要将完整域而不是localhost放在URL中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM