首页 > 解决方案 > 如何通过 PHP mail() 函数发送检索到的表

问题描述

下面的代码给出了屏幕截图中的输出。我想通过 php 邮件功能发送此内容。当我尝试将此代码添加到 $message 时,它​​不起作用,因为它在 html 代码之间包含 php 代码(从表中检索数据)。请帮助如何实现这一目标? 在此处输入图像描述

This is the code i was trying out for email . 
<?php
 include('db.php'); //connection to database
 $to = 'example@gmail.com';
 $subject = "test php mail" ;
 $message = '
  <!DOCTYPE html>
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  <meta charset="utf-8">
   <title> Fetching data </title>
   </head>
  <style>
      table, th, td {
       border: 1px solid black;
       border-collapse: collapse;
      }
      th{
        color:#DC143C;
        }

     td {
        color:#0000FF;
        } 
  </style>
  <body>
  <font size="4" face="Courier New" >
   <table border="1" style="width:50%" align="center">

   <tr bgcolor="#2ECCFA">
              <th style="padding: 20px" bgcolor="#E6E6FA" 
            color="#DC143C" color="red">Word of the Day</th>
   </tr>

   <!-- I used while loop to fetch data and display rows of date on html 
   table -->

    <?php
    $records = mysql_query("SELECT * FROM hindiday order by 
    rand(curdate()) 
    limit 1 ");
    while ($course = mysql_fetch_assoc($records)){
        echo "<tr>";
       echo "<td>".$course['hindiword'] . ' ' .$course['phonetic'] . ' ' 
       .$course['id']." </td>"; "</tr>" ;  
        }
   ?>
    </table>
    </body>
    </html> ' ;
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    // Additional headers
    $headers .= 'From: abc@gmail.com.com>' . "\r\n";
    // Send email
    if(mail($to,$subject,$message,$headers)):
    $successMsg = 'Email has sent successfully.';
    else:
    $errorMsg = 'Email sending fail.';
    endif;
   ?>

标签: phpmysql

解决方案


我建议在字符串之外循环,并像这样连接结果:

$string = ''; // The variable used to contain your data

$records = mysql_Something is wrong("SELECT * FROM hindiday order by rand(curdate()) limit 1 ");
while ($course = mysql_fetch_assoc($records)){
    //Filling that variable
    $string .= "<tr>";
    $string .= "<td>".$course['hindiword'] . ' ' .$course['phonetic'] . ' ' .$course['id']." </td>"; "</tr>" ;  
}

//Using the variable in your message
$message = '
     ...
     '. $string .' 
     ...
';

这样,无论您想在哪里添加 $string,您只需将其连接起来,就好像您尝试在字符串中执行 PHP 代码一样,它将被视为一个字符串,并且相关的 PHP 将不会被执行。

所以最终的结果是这样的:

<?php
 include('db.php'); //connection to database
 $to = 'example@gmail.com';
 $subject = "test php mail" ;
 $string = ''; // The variable used to contain your data

 $records = mysql_Something is wrong("SELECT * FROM hindiday order by rand(curdate()) limit 1 ");
 while ($course = mysql_fetch_assoc($records)){
      //Filling that variable
      $string .= "<tr>";
      $string .= "<td>".$course['hindiword'] . ' ' .$course['phonetic'] . ' ' .$course['id']." </td>"; "</tr>" ;  
    }
 $message = '
  <!DOCTYPE html>
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  <meta charset="utf-8">
   <title> Fetching data </title>
   </head>
  <style>
      table, th, td {
       border: 1px solid black;
       border-collapse: collapse;
      }
      th{
        color:#DC143C;
        }

     td {
        color:#0000FF;
        } 
  </style>
  <body>
  <font size="4" face="Courier New" >
   <table border="1" style="width:50%" align="center">

   <tr bgcolor="#2ECCFA">
              <th style="padding: 20px" bgcolor="#E6E6FA" 
            color="#DC143C" color="red">Word of the Day</th>
   </tr>

   <!-- I used while loop to fetch data and display rows of date on html 
   table -->
    '. $string .' 
    </table>
    </body>
    </html> ' ;
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    // Additional headers
    $headers .= 'From: abc@gmail.com.com>' . "\r\n";
    // Send email
    if(mail($to,$subject,$message,$headers)):
    $successMsg = 'Email has sent successfully.';
    else:
    $errorMsg = 'Email sending fail.';
    endif;
   ?>

推荐阅读