views:

33

answers:

2

I am trying to send an email through the result sets generated in MySQL in PHP

This is the code.

<?php
$txtMsg = '<table><th>Name</th>';
$txtMsg = '';
  mysql_connect("localhost", "root", "pop") or die(mysql_error());
      mysql_select_db("jpd") or die(mysql_error());
      $oustanding = mysql_query("select Name from results") or die(mysql_error());  
      $num=mysql_num_rows($oustanding);

      while($row1 = mysql_fetch_array( $oustanding )) {
          ?>
        <tr>
            <td><h3><?php echo $row1['Name']; ?></h3></td>

        </tr>
<?php
$txtMsg .= "<tr><td>".$row1['Name']."</td></tr>";
}     


ini_set ( "SMTP", "xy.domain.com" ); 
$mail_to= '[email protected]';
$mail_from='[email protected]';
$mail_sub='OutStanding Results';
$mail_mesg=$txtMsg;

//Check for success/failure of delivery 
if(mail($mail_to,$mail_sub,$mail_mesg,"From: $mail_from"))
echo "<br><br>Email Successfully Sent!";
else
echo "<br><br>Error Sending Email!";
}

?>

The problem is , I want the results to be displayed in table format. But instead of processing the html tags, they are getting printed as well.

How to get the email with table format?

Thanks.

+3  A: 

You need to specify your e-mail message as HTML. Try this:

$headerFields = array(
    "From: $mail_from",
    "MIME-Version: 1.0",
    "Content-Type: text/html;charset=iso-8859-1"
);
mail($mail_to, $mail_sub, $mail_mesg, implode("\r\n", $headerFields))
Gumbo
+1  A: 

By default emails are sent using the text/plain MIME type.

If you want formatted email you should use a multipart MIME email with a text/plain and a text/html part.

You can find an example at http://www.tuxradar.com/practicalphp/15/5/3

You can also send an email that is text/html instead of multipart, but this tends to score highly with spam filters.

David Dorward