现在,当我加载页面时,此信息会按预期显示。其中有很多内容。
我想要做的是设置一个cron作业,通过基于此php页面的电子邮件发送每日报告。使用标准HTML似乎工作正常,当我使用动态PHP代码时,似乎搞砸了。
最初我尝试使用标准的php邮件,这只是显示源代码,有人建议使用带有phpmailer的file_get_contents,这就是我现在的地方,但我有点超出我的深度。有什么样的转义或编码/解码我应该做,整个想法是我不必逐行逃避代码所以我希望不会:(
以下是运行代码的片段:
<body>
<?php
$username = 'xxx@gmail.com';
$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 = "xxx@gmail.com"; // GMAIL username
$mail->Password = "xxx"; // GMAIL password
$mail->SetFrom('xxx@gmail.com', 'First Last');
$mail->AddReplyTo("xxx@gmail.com","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 = "xxx@him.net";
$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或如果我需要编码等?
答案 0 :(得分:1)
您没有运行该文件,只是字面上获取文件的内容并将其作为电子邮件正文。
请改为尝试:
ob_start();
include 'jira_filters.php'; //execute the file as php
$body = ob_get_clean();
ob_start()
使每个正常的回声和直接的html,文本等(<?php
标签之外的东西)变得缓冲而不是在某处输出。然后是你
使用ob_get_clean()
获取缓冲区中的内容,并将 作为电子邮件发送。