2012-01-13 11 views
0

CRON 작업을 사용하여 최종 자동 이메일을 설정하려고합니다.CRON - PHP - file_get_contents CRON으로 PHP 페이지 및 이메일 실행

CRON 작업을 통해 정적 이메일을 보낼 수 있지만 file_get_contents 및 PHP에서 mysql 쿼리를 사용하여 데이터베이스에서 데이터를 가져 와서 이메일 본문을 만드는 PHP 페이지를 사용할 때 문제가 발생합니다.

도움이 필요하십니까? * 참고 - SMTP 용으로 특수한 php 메일러를 사용합니다. 이 메일러는 정적 이메일을 보내거나 동적 이메일을 수동으로 보낼 때 100 % FINE으로 작동합니다 (CRON 아님). 실제로 PHP 파일에 액세스하여 데이터를 가져 오는 CRON 작업을 가져 오는 데 문제가 있습니다.

메일러 파일은 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  = "[email protected]"; 
    $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("[email protected]","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> 
+0

$ mail-> Username = "EMAIL'이 줄에 글을 올리는 동안이 문제가 발생했는지 확실하지 않지만 display_errors가 꺼져 있으면 잘 될 수 있습니다. 문제의 근원이 되십시오. – mfonda

+0

그래, 그건 내 정보를 편집 한 결과 일 뿐이야. 이 문제는 메일러 파일과 전혀 관련이 없습니다. $ body =를 단지 문자열 (예 : $ body = 'testing')으로 변경하면 이메일이 제대로 전송됩니다. 그것은 file_get_contents와 관련이 있습니다. CRON 작업이 실제로 서버에서 PHP 파일을 처리 할 수있는 경로 또는 방식과 관련이 있다고 생각합니다. – JimmyJammed

답변

1

나는 실제로 그것을 알아 냈다. 오류가 html 전자 메일에 선언되었습니다. <style type="text/css">

+0

내가 사용하는 PHP smtp 메일러는 HTML 태그에 매우 민감합니다. – JimmyJammed

0

.

은 CRON 작업으로 서버에서이 명령을 실행 해보십시오 :

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

이 후 인 test.html의 내용은 무엇입니까? 올바르지 않은 경우 localhost 대신 전체 도메인을 URL에 넣어야합니다.