如何在PHP中将HTML表格发送到电子邮件正文中?

6

我正在发送动态创建的HTML表格到电子邮件正文中,但在我接收的电子邮件中,显示的是HTML代码而不是表格。请帮助我。

这是我的代码:

<?php
  $output = '<html><body><form>';

  $output .=  '<table border="1"><tr><th>Author</th><th>Node Title</th><th>Node Summary</th><th>Node Body</th><th>Edit this node</th><th>Report Abuse</th><th>Group</th></tr>';
  while($row = mysql_fetch_array($sql)) {



    $data = explode('"', $query['data']);
    $output .= '<tr><td>';

    if($row['created'] >= $strdate && $row['created'] < $enddate) {

      $output .= '<a href="http://localhost/localstage/user/' . $row['uid'] . '">' .        $query['name'] . '</a></td><td>';
      $output .= '<a href="http://localhost/localstage/node/' . $row['nid'] . '">' .        $row['title'] . '</a> (New)</td><td>';
    }
    else {

      $output .= '<a href="http://localhost/localstage/user/' . $sql_query['uid'] . '">' . $query['name'] . '</a></td><td>';
      $output .= '<a href="http://localhost/localstage/node/' . $row['nid'] . '">' . $row['title'] . '</a> (Updated)</td><td>';
    }
    //$output .= '</td><td>';
    $output .= $row['teaser'] . '</td><td>';
    if($row['field_provcomp_level_value'] == 0 || $row['field_provcomp_level_value'] == 1)<br /> {
    $output .= $row['body'] . '</td><td>';
    }
    else {
    $output .= '</td><td>';
    }
    $output .= '<a href="http://localhost/localstage/abuse/report/node/' . $row['nid'] . '">Abuse</a></td><td>';
    $output .= '<a href="http://localhost/localstage/node/' . $row['nid'] . '/edit">Edit</a></td><td>';
    $query = mysql_fetch_array(mysql_query("SELECT title from node Where type = 'groupnode' AND nid = '" . $row['nid'] . "'"));
    $output .= $query['title'] . '</td></tr>';
  }

  $output .= '</table></form></body></html>';
  print $output;
  $to = 'hmdnawaz@gmail.com';
  $headers = "From Ahmad \r\n";
  //$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
  //$headers .= "CC: susan@example.com\r\n";
  $headers .= 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
  $subject = 'Email report';
  mail($to, $subject, $output, $headers);
  if(mail) {
  echo 'Email sent';
  }
  else {
  echo 'Error sending email';
  }
?>

嗨,当在Stack Overflow上格式化您的代码时,您不需要在每行后添加<br />,请单击橙色问号以查看其工作原理。此外,请查看您的其他问题,如果有人给出了有用的答案,请通过单击复选标记接受答案。 - Ibu
4个回答

9

首先,您的HTML代码中缺少标签,但这不应该是一个问题。

其次,您的标题不正确,应该是:

$headers = "From Ahmad \r\n";
//$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
//$headers .= "CC: susan@example.com\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
$subject = 'Email report';

替代方案:

//$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
//$headers .= "CC: susan@example.com\r\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
$headers .= "From: Ahmad <email@email.com>\r\n";
$subject = 'Email report';

基本上,我认为您的标题应该以“Mime-Version”开头。
请检查此代码:http://us.php.net/manual/en/function.mail.php#example-2877

4

http://php.net/manual/zh/function.mail.php

发送HTML邮件的官方示例:

<?php
// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

// subject
$subject = 'Birthday Reminders for August';

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

// 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);
?>

最后一个问题可能是,您创建的表格树在某些特定的电子邮件客户端中存在渲染问题。您能否测试上面的代码?


这句话强调了写标记语言的重要性,但并未提及如何让客户端将其呈现为HTML而非文本,这似乎是问题所在。 - Quentin

2
首先,我建议使用 Pear Mail 扩展(它与基本的 PHP 构建一起提供),因为在我看来,使用 mail() 函数不是很好,而使用 Pear Mail 类,您还可以轻松设置 mime 类型。
请注意,许多邮件服务仅支持有限的 HTML 消息(如果有的话),但我不知道对表格的支持水平。 Mail_mime 类

写一封简单的电子邮件并不难,如果需要发送附件或更复杂的电子邮件,通常我会使用PHP_Mailer。 - zzarbi

1

程序员们来这里学习电子邮件功能。 - Albuquerque Web Design

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接