【问题标题】:CRON - PHP - file_get_contents to run PHP page and email with CRONCRON - PHP - file_get_contents 使用 CRON 运行 PHP 页面和电子邮件
【发布时间】:2012-01-13 22:43:52
【问题描述】:

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

我可以让 CRON 作业发送一封静态电子邮件,但是在尝试使用 file_get_contents 和然后使用 mysql 查询从数据库获取数据并构建电子邮件正文的 php 页面时遇到问题。

需要帮助! *注意-我为 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();
}
?>

PHP Page 使用 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>

【问题讨论】:

  • 您在$mail-&gt;Username = "EMAIL 行中缺少一个“结束”,不确定是否在发布到 SO 时发生这种情况,但如果您关闭了 display_errors,这很可能是您的问题的根源。
  • 是的,这只是我编辑安全信息的结果。该问题根本与邮件文件无关。如果我将 $body= 更改为只是一个字符串(即 $body = 'testing'),电子邮件就会正常发送。它与file_get_contents 有关。我正在考虑与路径或 CRON 作业实际处理服务器上的 php 文件的方式有关。

标签: php email cron file-get-contents


【解决方案1】:

我真的想通了。 错误出现在我在 html 电子邮件中的声明中。 必须是&lt;style type="text/css"&gt;

【讨论】:

  • 我猜我使用的 php smtp 邮件程序对 HTML 标签非常敏感。
【解决方案2】:

您的服务器可能未配置为侦听 localhost。

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

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

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

【讨论】:

    猜你喜欢
    • 2010-10-29
    • 1970-01-01
    • 2013-10-10
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    相关资源
    最近更新 更多