doulao3905 2013-11-06 13:41
浏览 105
已采纳

使用php邮件功能的电子邮件表

Can you explain a method I can use to email a table generated from a mysql query using a php script. I am looking to use the php mail function but don't know how best so I can keep the format of my table.

Example of one of the table to be emailed:

<table style="text-align:center;" width="500" cellpadding="2" cellspacing="1" border="0" bgcolor="#FFFFFF"> 

    <tr>
      <td style="" colspan="20">
        <valign font face="Verdana" size="2"><b style="font-size:20px;">MSA</b><br />
        <valign label style="font-size:15px;">Invoices<br /></label><br /></font>
      </td>
    </tr>
</table>


 <?php


$sql = "SELECT * FROM invoices";

?>

<table width="500" border="0" cellspacing="1" cellpadding="0">
 <tr>
 <td>
 <table colspan="20" width="500" border="1" cellspacing="0" cellpadding="3">
 <tr>
 <td colspan="20"><strong><center>Entries</strong> </center> </td>
 </tr>

 <tr>

 <td align="center"><strong>Date</strong></td>
 <td align="center"><strong>Supplier</strong></td>
 <td align="center"><strong>Invoice Number</strong></td>
 <td align="center"><strong>Invoice Total</strong></td>
 <td align="center"><strong>Tax</strong></td>
 <td align="center"><strong>Comments</strong></td>
 </tr>

 <?php

foreach ($db->query($sql) as $row) {

?>
<tr>

  <td>
  <?php echo "{$row['dt_invoice']}"; ?> 
  </td>
  <td>
  <?php echo "{$row['supplier']}"; ?> 
  </td>
    <td>
  <?php echo "{$row['invoice_no']}"; ?>
  </td>
    <td>
  <?php echo "{$row['invoice_total']}"; ?>
  </td>    
   <td>
  <?php echo "{$row['amt_tax']}"; ?>
  </td>  
     <td>
  <?php echo "{$row['comments']}"; ?>
  </td>
<td>
</td>

 </tr>
 <?php   
    } 


 ?>



 </table>
 </td>
 </tr>
 </table>
  • 写回答

4条回答 默认 最新

  • duanji5116 2013-11-06 13:45
    关注

    Taken from http://php.net/manual/en/function.mail.php

        // multiple recipients
        $to  = 'aidan@example.com' . ', '; // note the comma
        $to .= 'wez@example.com';
    
        // subject
        $subject = 'Birthday Reminders for August';
    
        // message
        $message = '**your html here**'
        // To send HTML mail, the Content-type header must be set
        $headers  = 'MIME-Version: 1.0' . "
    ";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
    ";
    
        // Additional headers
        $headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "
    ";
        $headers .= 'From: Birthday Reminder <birthday@example.com>' . "
    ";
        $headers .= 'Cc: birthdayarchive@example.com' . "
    ";
        $headers .= 'Bcc: birthdaycheck@example.com' . "
    ";
    
        // Mail it
        mail($to, $subject, $message, $headers);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongnuo3749 2013-11-06 13:45
    关注

    The mail function has an optional fourth parameter called $additional_headers. This parameter allows you to send a HTML email:

    $headers = "MIME-Version: 1.0 
    ";
    $headers .= "Content-type: text/html; charset=iso-8859-1 
    ";
    $headers  .= "From: me@mysite.com
    ";
    
    $send = mail('user@example.com', 'My Subject', $htmlMessage, $headers);
    if($send){
        echo 'SENT!';
    }
    
    评论
  • doulu3808 2013-11-06 13:47
    关注

    Take a look at Example #4 on the PHP documentation page for mail():

    $message = '
    <html>
    <head>
      <title>Birthday Reminders for August</title>
    </head>
    <body>
      <p>Here are the birthdays upcoming in August!</p>
      <table>
        <tr>
          <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
        </tr>
        <tr>
          <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
        </tr>
        <tr>
          <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
        </tr>
      </table>
    </body>
    </html>
    ';
    

    When building an HTML email, you just use HTML code like you would in your webpage output. So you can just construct a string with that format:

    $message = '
    <table width="500" border="0" cellspacing="1" cellpadding="0">
     <tr>
     <td>
     <table colspan="20" width="500" border="1" cellspacing="0" cellpadding="3">
     <tr>
     <td colspan="20"><strong><center>Entries</strong> </center> </td>
     </tr>
    
     <tr>
    
     <td align="center"><strong>Date</strong></td>
     <td align="center"><strong>Supplier</strong></td>
     <td align="center"><strong>Invoice Number</strong></td>
     <td align="center"><strong>Invoice Total</strong></td>
     <td align="center"><strong>Tax</strong></td>
     <td align="center"><strong>Comments</strong></td>
     </tr>';
    
    foreach ($db->query($sql) as $row) {
        $message .= '<tr>
           <td>' . $row['dt_invoice'] . '</td>
           <td>... and so on</td>
         </tr>';
    }
    
    $message .= '    
     </table>
     </td>
     </tr>
     </table>';
    
    评论
  • duanjitong7226 2013-11-06 13:50
    关注

    Just sent it as a normal HTML Mail.

    Modified version of Click me (us.php.net

    <?php
    
    $to  = 'anyone@anyone.com';
    
    // subject
    $subject = 'HTML table';
    
    // message
    $message = '
    <html>
    <head>
    
    </head>
    <body>
      <table>
    </table>
    </body>
    </html>
    ';
    
    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "
    ";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
    ";
    
    // Additional headers
    $headers .= 'To: random <random@random.com>, random <random@andom.com>' . "
    ";
    $headers .= 'From: HTML TABLE <random@random.com>' . "
    ";
    
    // Mail it
    mail($to, $subject, $message, $headers);
    ?> 
    
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 在工控机(Ubuntu系统)上外接USB蓝牙硬件进行蓝牙通信
  • ¥15 关于PROCEDURE和FUNCTION的问题
  • ¥100 webapi的部署(标签-服务器)
  • ¥20 怎么加快手机软件内部计时的时间(关键词-日期时间)
  • ¥15 C语言除0问题的检测方法
  • ¥15 为什么四分管的内径有的是16mm有的15mm,四分不应该是12.7mm吗
  • ¥15 macos13下 ios交叉编译的问题
  • ¥15 bgz压缩文件怎么打开
  • ¥15 封装dll(引入了pcl的点云设计库)
  • ¥30 关于#开发语言#的问题:我需要在抄板的基础上再抄板抄程序,根据RDA8851CM基础上开发