문제

Screenshot of my content, which has to be mailed

i am displaying the daily report submitted by the user now i want to mail the same to email when the send email button is clicked. how can i do that in PHP ? i used the following code

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>

i want to mail the entire table with data

도움이 되었습니까?

해결책

Without knowing anything about your code, I can perhaps at least guess as to the structure of what builds this table. Maybe something along the lines of this?:

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

Maybe even abstracted into its own higher-level function?:

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

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

    return $result;
}

Any such function or abstraction can be used to construct the HTML for the email body as well...

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

Styling the email will be another story, of course. It's best not to rely on linked resources in an HTML email, those tend not to be requested by mail clients as they're often abused by spammers and the like. You can perhaps include the style sheet(s) as an "embedded resource" in the email, though in most cases I think inline styling is probably best for emails. (Some mail clients might not even load an embedded resource, at least not without first prompting the user, which isn't an ideal user experience.)

Assuming you don't want to duplicate much code, the above method can be abstracted using the Form Template Method refactoring pattern (example here, among others).

I guess the point I'm getting at is that the email doesn't send the web page, but rather the web page and the email are both independent "views" of the underlying data. Common code between them can be abstracted and shared of course, but trying to re-use a webpage itself in an email is over-complicating the situation.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top