0

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

4 回答 4

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);
于 2013-11-06T13:45:07.877 回答
1

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!';
}
于 2013-11-06T13:45:14.627 回答
1

只需将其作为普通 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);
?> 
于 2013-11-06T13:50:12.843 回答
0

查看PHP 文档页面上的示例 #4mail()

$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>';
于 2013-11-06T13:47:25.973 回答