【问题标题】:Email table using php mail function使用php邮件功能的电子邮件表
【发布时间】:2013-11-06 13:41:04
【问题描述】:

你能解释一下我可以用电子邮件发送一个使用 php 脚本从 mysql 查询生成的表的方法吗?我正在寻找使用 php 邮件功能,但不知道如何最好,所以我可以保持我的表格格式。

要通过电子邮件发送的表格之一的示例:

<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>

【问题讨论】:

标签: php


【解决方案1】:

取自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' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Additional headers
    $headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
    $headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
    $headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
    $headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

    // Mail it
    mail($to, $subject, $message, $headers);

【讨论】:

    【解决方案2】:

    mail 函数有一个可选的第四个参数,称为 $additional_headers。此参数允许您发送 HTML 电子邮件:

    $headers = "MIME-Version: 1.0 \r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
    $headers  .= "From: me@mysite.com\r\n";
    
    $send = mail('user@example.com', 'My Subject', $htmlMessage, $headers);
    if($send){
        echo 'SENT!';
    }
    

    【讨论】:

      【解决方案3】:

      只是作为普通的 HTML 邮件发送。

      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' . "\r\n";
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
      
      // Additional headers
      $headers .= 'To: random <random@random.com>, random <random@andom.com>' . "\r\n";
      $headers .= 'From: HTML TABLE <random@random.com>' . "\r\n";
      
      // Mail it
      mail($to, $subject, $message, $headers);
      ?> 
      

      【讨论】:

        【解决方案4】:

        看看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>
        ';
        

        在构建 HTML 电子邮件时,您只需像在网页输出中一样使用 HTML 代码。所以你可以用这种格式构造一个字符串:

        $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>';
        

        【讨论】:

          猜你喜欢
          • 2012-12-12
          • 2014-05-23
          • 2014-04-30
          • 2010-11-25
          • 1970-01-01
          • 2021-05-10
          • 1970-01-01
          • 2013-03-17
          • 1970-01-01
          相关资源
          最近更新 更多