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

时间:2013-11-06 13:41:04

标签: php

你能解释一下我可以使用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 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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!';
}

答案 2 :(得分:1)

只是将其作为普通的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);
?> 

答案 3 :(得分:0)

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