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


Email table using php mail function

你能解释一下我可以用来通过电子邮件发送从 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>

自 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);

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!';
}

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

单击我 (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);
?> 

看看 PHP 文档页面上的示例 #4 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>';