【问题标题】:How to send html table in email body in php?如何在 php 的电子邮件正文中发送 html 表格?
【发布时间】:2011-09-03 20:34:00
【问题描述】:

我在电子邮件正文中发送动态创建 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';
  }
?>

【问题讨论】:

  • 嗨,当你在堆栈溢出时格式化你的代码时,你不需要在每行之后添加&lt;br /&gt;,请点击橙色问号看看它是如何工作的。还请查看您的其他问题,如果有人给了您有用的答案,请点击复选标记接受答案

标签: php html email


【解决方案1】:

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

发送 HTML-Emails 的官方示例:

<?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 而不是文本,这似乎是这里的问题。
【解决方案2】:

我建议使用 Swiftmailer

http://swiftmailer.org/

它被 Symfony 项目所使用,并且有很好的文档和支持。

不建议自己编写电子邮件功能,因为有很多怪癖和安全注意事项。

这里是设置html正文内容的文档:

http://swiftmailer.org/docs/message-body

【讨论】:

    【解决方案3】:

    首先,您的 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】:

      首先,我建议使用 Pear Mail 扩展(它是在基本构建的 php 中提供的),因为使用 mail() 函数 imo 效果不佳,使用 pear 邮件类,您还可以设置mime 类型很容易。

      Mail_mime class

      请记住,许多邮件服务对 HTML 消息的支持有限(如果有的话),但是我不知道表格的支持级别。

      【讨论】:

      • 写一封简单的邮件真的没那么难,我一般在需要发送附件的时候使用PHP_Mailer,或者更复杂的邮件。
      猜你喜欢
      • 2012-10-31
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 2017-01-16
      • 2017-06-28
      • 1970-01-01
      相关资源
      最近更新 更多