php 如何在php的电子邮件正文中发送html表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6160320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:31:47  来源:igfitidea点击:

How to send html table in email body in php?

phphtmlemail

提问by Ahmad

I am sending dynamically creating html table in email body but in the email I receive the html code instead of displaying a table. Please help me. Here is my code.

我在电子邮件正文中发送动态创建 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 = '[email protected]';
  $headers = "From Ahmad \r\n";
  //$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
  //$headers .= "CC: [email protected]\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';
  }
?>

回答by zzarbi

First you are missing the tag in your HTML code but that should not be an issue.

首先,您在 HTML 代码中缺少标记,但这应该不是问题。

Secondly your header is incorrect, you have that:

其次你的标题不正确,你有:

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

instead of:

代替:

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

Basically I think you header should start by "Mime-Version".

基本上我认为你的标题应该以“Mime-Version”开头。

Check this code: http://us.php.net/manual/en/function.mail.php#example-2877

检查此代码:http: //us.php.net/manual/en/function.mail.php#example-2877

回答by Ahmad

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

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

Official example to send HTML-Emails:

发送 HTML 电子邮件的官方示例:

<?php
// multiple recipients
$to  = '[email protected]' . ', '; // note the comma
$to .= '[email protected]';

// 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 <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>

At the end one problem could be that the table tree you created have some render problems in specific email clients. Can you test the code above?

最后,一个问题可能是您创建的表树在特定电子邮件客户端中存在一些呈现问题。你能测试一下上面的代码吗?

回答by Itai Sagi

First of all, I would suggest using the Pear Mail extension (it's supplied with the basic build of php), because using the mail() function imo, is not as good, using th pear mail class, you can also set the mime type easily.

首先,我建议使用Pear Mail扩展(它与php的基本构建一起提供),因为使用mail()函数imo,不是那么好,使用th pear邮件类,您还可以设置mime类型容易地。

Mail_mime class

mail_mime 类

Please keep in mind that alot of mail services have limited (if any) support for HTML messages, I don't know the support level for tables, however.

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

回答by serby

I would suggest Swiftmailer

我会建议 Swiftmailer

http://swiftmailer.org/

http://swiftmailer.org/

It is used by the Symfony project and has great docs and support.

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

Writing email functionality yourself is not advises as there are so many quirks and security considerations.

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

Here is the doc for setting the html body content:

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

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

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