我们如何使用PHP将表数据发送到邮件?

时间:2014-03-07 12:45:21

标签: email sendmail html-email phpmailer

Screenshot of my content, which has to be mailed

我正在显示用户提交的每日报告,现在我想在点击发送电子邮件按钮时将其邮寄到电子邮件。我怎么能用PHP做到这一点? 我使用了以下代码

extract($_REQUEST);

if(isset($send_mail)){
    $edata= $_POST['send_msg'];
    echo "<script type='text/javascript'>alert('$edata');</script>";
    $ka_semail=$_SESSION['user_email'];
    $subject = "Report";
    $body = $edata;
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: Info <'.$ka_semail.'>' . "\r\n"; 

$mchk= mail($ka_semail, $subject, $body, $headers);
        if(isset($mchk)){
        echo "<script type='text/javascript'>alert('Your data Sent sucessfully');</script>";
        header("location:reports.php");
        }
}
?>
<body>
  <div id="main">
    <header>
      <div id="logo">
        <div id="logo_text">
          <a href="index.html">
          <div style="background:#2A0000"><img src="images/kreamsoftlogo.png"/></a></div>
        </div>

      </div>
      <?php include'nav.php'; ?>
    </header>
    <div id="site_content">
      <div id="email-data">
    <div id="content">
      <div class="content" style="min-height:385px; width: 100%" >

        <h1>Daily Report </h1>
        <? 
        $current_date = date("d/m/Y");

        $current_user = $_SESSION['user_login'];

        $find = mysql_query("select * from k_dailyreport where kr_user='$current_user'  AND kr_date='$current_date'"); 


        ?>
        <div class="content_item">
          <ul>

        <?php
        $numrow=mysql_num_rows($find);
        if($numrow>0){
                               $sino = 0;
            echo "<table border='0' width='100%'>";
            echo "<tr class='head'>";
             echo "<th>S.No</th>";

                  echo "<th>Project</th>";
                  echo "<th>Process</th>";
                  echo "<th>Process Date</th>";
                  echo "<th>Current Duration</th>";

                  echo "</tr>";
                  $sno=0;
            while($row = mysql_fetch_array($find)){
                $sno=$sno +1;
                  $sino++;
                                $cls = ($sino%2==0) ? "even" : "odd";
                  echo "<tr class='t1'>";
                   echo "<td>$sno</td>";
                  echo "<td>".$row['kr_project']."</td>";
                  echo "<td>".$row['kr_process']."</td>";
                  echo "<td>".$row['kr_rtime']."</td>";
                 echo "<td>".$row['kr_ctime']."</td>";

                  }?></td>

            <?php
                  echo "</tr>";
                  ?><form name="test" method="post"enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>"> 
                  <p style='padding-top: 15px;margin-right: 50px; float:right'><input class='submit' id="sendEmail" type="submit" name='sendmail' value='Send Mail' />
                  <input type="hidden" name="send_mail" value="send_mail" /> 
                  <input type="hidden" name="send_msg" value="<?php
        $numrow=mysql_num_rows($find);
        if($numrow>0){
                               $sino = 0;
            echo "<table border='0' width='100%'>";
            echo "<tr class='head'>";
             echo "<th>S.No</th>";

                  echo "<th>Project</th>";
                  echo "<th>Process</th>";
                  echo "<th>Process Date</th>";
                  echo "<th>Current Duration</th>";

                  echo "</tr>";
                  $sno=0;
            while($row = mysql_fetch_array($find)){
                $sno=$sno +1;
                  $sino++;
                                $cls = ($sino%2==0) ? "even" : "odd";
                  echo "<tr class='t1'>";
                   echo "<td>$sno</td>";
                  echo "<td>".$row[1]."</td>";
                  echo "<td>".$row[2]."</td>";
                  echo "<td>".$row[3]."</td>";
                 echo "<td>".$row[3]."</td>";
                  echo "</tr>";
                  }echo "</table>";}?>" /></p>

                  </form>
                  <?                
                  }else{ 
                  ?>

                      <center><img style="text-align:center" src="images/no_record.gif" /></center>


                     <?
                     } 

            echo "</table>";
        ?>
    </li>
    </ul>



        </div>
           </div> </div>
      </div>

    </div>

我想用数据

邮寄整个表格

1 个答案:

答案 0 :(得分:1)

在不了解您的代码的情况下,我或许至少可以猜测构建此表的结构。也许是这样的东西?:

writeTableHeader();
foreach ($values as $value) {
    writeTableRow($value);
}
writeTableFooter();

甚至可能抽象成自己的高级功能?:

function writeTable($values) {
    $result = '';

    $result .= buildTableHeader();
    foreach ($values as $value) {
        $result .= buildTableRow($value);
    }
    $result .= buildTableFooter();

    return $result;
}

任何此类函数或抽象都可用于构建电子邮件正文的HTML ...

// ... previously building the overall email body
$mailBody .= writeTable($values);
// ... maybe some more email body elements as well
mail($to, $subject, $mailBody);

当然,为电子邮件设置样式将是另一个故事。最好不要依赖HTML电子邮件中的链接资源,这些资源往往不会被邮件客户端请求,因为它们经常被垃圾邮件发送者等滥用。您可以将样式表作为“嵌入式资源”包含在电子邮件中,但在大多数情况下,我认为内联样式可能最适合电子邮件。 (有些邮件客户端甚至可能不会加载嵌入式资源,至少在没有首先提示用户的情况下,这不是理想的用户体验。)

假设您不想复制很多代码,可以使用Form Template Method重构模式(example here等)抽象上述方法。

我想我得到的一点是电子邮件不发送网页,而是网页和电子邮件都是底层数据的独立“视图”。当然,它们之间的公共代码可以被抽象和共享,但是尝试在电子邮件中重复使用网页本身就会使情况过于复杂。