2012-12-12 153 views
0

好吧,即時通訊的圈子,我想做的事情很簡單。我有一個網頁,將信息從一個API拖到一個對象中,遍歷各種對象並在簡單的html表格中顯示信息。phpmailer file_get_contents和動態php代碼

現在,當我加載頁面時,此信息按預期顯示。它有很多內容。

我想要做的是設置一個cron作業,通過基於此php頁面的電子郵件發送每日報告。使用標準的HTML,它似乎工作正常,當我使用動態PHP代碼似乎弄亂了事情。

本來我試着用標準的PHP郵件,這只是顯示源代碼,有人建議使用file_get_contents與phpmailer和即時通訊在現在,但即時通訊有點出我的深度。是否有某種逃避或編碼/解碼我應該做的,整個想法是我不會逃避代碼行,所以我不希望:(

這是代碼im運行的代碼片段:

<body> 

<?php 

$username = '[email protected]'; 
$password = 'xxx'; 

$url ='xxx.atlassian.net/rest/api/2/search?jql=project+%3D+bug+AND+updated+%3C%3D+2d+AND+status+%3D+%22In+Beta%22+AND+assignee+!%3D+beta_merge+ORDER+BY+priority+DESC'; 

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password"); 
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 

$issue_list = curl_exec($curl); 
$issue_json = json_decode($issue_list); 
//var_dump($issue_json); 
$bugs = $issue_json->issues; 


?> 
<h2>Table 1</h2> 
<table cellspacing="0" cellpadding="3" width="80%"> 
    <?php foreach ($bugs as $bug) 
      {?> 
       <tr> 
<td><?php echo $bug->fields->issuetype->iconUrl; ?></td> 
</tr> 


      <?php } ?> 
    </table> 

現在,如果我加載頁面我看到:

表1

http://www.xxx.com/images/bug_16.png 

http://www.xxx.com/images/bug_16.png 

http://www.xxx.com/images/feature_16.png 

http://www.xxx.com/images/bug_16.png 

但是,如果我運行此:

<?php 

//error_reporting(E_ALL); 
error_reporting(E_STRICT); 

date_default_timezone_set('America/Toronto'); 

require_once('../class.phpmailer.php'); 
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded 

$mail    = new PHPMailer(); 

$body    = file_get_contents('jira_filters.php'); 
//$body    = preg_replace('/[\]/','',$body); 

$mail->IsSMTP(); // telling the class to use SMTP 
$mail->Host  = "smtp.gmail.com"; // SMTP server 
$mail->SMTPDebug = 1;      // enables SMTP debug information (for testing) 
              // 1 = errors and messages 
              // 2 = messages only 
$mail->SMTPAuth = true;     // enable SMTP authentication 
$mail->SMTPSecure = "ssl";     // sets the prefix to the servier 
$mail->Host  = "smtp.gmail.com";  // sets GMAIL as the SMTP server 
$mail->Port  = 465;     // set the SMTP port for the GMAIL server 
$mail->Username = "[email protected]"; // GMAIL username 
$mail->Password = "xxx";   // GMAIL password 

$mail->SetFrom('[email protected]', 'First Last'); 

$mail->AddReplyTo("[email protected]","First Last"); 

$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic"; 

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 

$mail->MsgHTML($body); 

$address = "[email protected]"; 
$mail->AddAddress($address, "John Doe"); 

$mail->AddAttachment("images/phpmailer.gif");  // attachment 
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment 

if(!$mail->Send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message sent!"; 
} 

?> 

那麼這是輸出我看到當電子郵件到達是:

issues; ?> 

Table 1 

fields->issuetype->iconUrl; ?> 

我只想電子郵件正是我在瀏覽器中看到,當裝載顯示這一頁。有沒有一種簡單的方法來做到這一點,嘗試搜索,但發現很難描述什麼即時消息,因爲我不知道它可能與phpmailer或如果我需要編碼等?

回答

1

你並沒有運行該文件,只是從文件中獲取文件的內容並將它們放在電子郵件正文中。

試試這個:

ob_start(); 
include 'jira_filters.php'; //execute the file as php 
$body = ob_get_clean(); 

ob_start()使成爲緩衝的而不是直接輸出某處每一個正常的回聲和直接的HTML,文本等(外<?php標籤的東西)。然後您 通過ob_get_clean()獲得它在緩衝區中的內容,並將發送給作爲電子郵件。