开发者

How to send html table in email body in php?

开发者 https://www.devze.com 2023-03-09 09:09 出处:网络
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.

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.

<?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';
  }
?>


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

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

instead of:

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

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

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


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

Official example to send 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);
?>

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?


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.

Mail_mime class

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.


I would suggest Swiftmailer

http://swiftmailer.org/

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

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:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号