使用按钮从PHP发送的电子邮件中下载表格

时间:2017-10-20 18:01:59

标签: php postgresql html-email

当调用PHP时,postgres查询会构建一个表并通过电子邮件发送它。我最初使用fputcsv下载而不是发送电子邮件但由于某种原因它停止工作(405 Not Allowed)。事实上,我没有成功地简单地让表格显示而没有任何额外的动作。我认为它与服务器交互有关。无论如何,构建表和电子邮件的查询仍然有效,所以我试图在表下添加一个允许表下载的按钮。在html和javascript中,我可以使用exportTableToCSV函数实现这一点。但是,由于我无法在<?php标记之外工作,因此我无法在PHP电子邮件页面中使用它。

这就是我所拥有的 -

<?php
{
$datea= $_POST["userDatea"];
$media= $_POST["userMedia"];
$datez= $_POST["userDatez"];
$media_names = "'".implode( "%','", $media)."%'";
$myemail = 'myemail@recipient.com';
$email_address = 'reports@sender.com';

$message = "
            <html>
            <head>
            <title>Daily Media Summary</title>

            </head>
            <body>";

$body .= "<table border='1' cellpadding='0' cellspacing='3' width='1360'>";
$body .= "<tr><td width: '650'>Date</td><td width: '140'>Total Plays</td><td width: '140'>Total Stores</td><td width: '140'>PPH</td></tr>";


// Create connection
$conn = pg_connect("host=ipofhost port=portofhost dbname=name user=username password=pword");

// Check connection
if (!$conn) {
  echo "Did not connect.\n";
  exit;
}

$partsresult = pg_query($conn, "SELECT
date (b.starttime),
Count(b.starttime) as Plays,
Count(distinct(b.playerid)) as Stores

FROM
public.billing b,
public.medias m

WHERE
b.mediaitemid = m.id and
m.name LIKE any (array[$media_names]) and
b.starttime >= date('$datea') and 
b.starttime < date('$datez')+1 and
m.startdate >  '2015-01-01'

GROUP BY
date (b.starttime)

ORDER BY
date (b.starttime);");

$day = date("D"); 
if($day == 'Mon') { 

            if (!$partsresult) {
              echo "Query failed.\n";
              exit;
            }

              while ($partsrow = pg_fetch_row($partsresult)) { 
              //$body .= "";
              $divide = ($partsrow[1]/$partsrow[2]);
              $math = round($divide/7.2, 2);
              $body .= "<tr><td width: '750'> $partsrow[0] </td>";
              $body .= "<td width: '140'> $partsrow[1] </td>";
              $body .= "<td width: '140'> $partsrow[2] </td>";
              $body .= "<td width: '75'> $math </td></tr>";
             };


}

else { 

            if (!$partsresult) {
              echo "Query failed.\n";
              exit;
            }

              while ($partsrow = pg_fetch_row($partsresult)) { 
              //$body .= "";
              $divide = ($partsrow[1]/$partsrow[2]);
              $math = round($divide/10.7, 2);
              $body .= "<tr><td width: '750'> $partsrow[0] </td>";
              $body .= "<td width: '140'> $partsrow[1] </td>";
              $body .= "<td width: '140'> $partsrow[2] </td>";
              $body .= "<td width: '140'> $partsrow[3] </td>";
              $body .= "<td width: '140'> $partsrow[4] </td>";
              $body .= "<td width: '75'> $math </td></tr>";
             };


}


pg_close($conn);


            $message .= $body . "</table>";
            $message .= $body . "<input type=button onclick=mybutton()/>";//not sure how to make this happen?
            $message .= $body . "</body>";
            $message .= $body . "</html>";

{
$to = $myemail;
$email_subject = "daily media item report";
$email_body = "$message";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html;';
$headers .= "From: sender@sender.com";
$headers .= "Reply-To: $email_address";

mail($to,$email_subject,$email_body,$headers, "-f reports@data.com");
//redirect to the 'thank you' page

}
}
?>

0 个答案:

没有答案