[thelist] Sending Tabular data with mail()

Santilal Parbhu santilal at scorpioneng.co.nz
Thu May 22 04:24:29 CDT 2008



I am working on a php script to send emails.  I want to be able to collect
some email content from a form (typically a covering note e.g Hi Joe,
Please find below the data you asked for") and then read some tabular data
from a database, which would be appended to the covering note.  I have tried
to create an html email using <html>  </html>, but when I did this all of
the formatting (paragraphing) disappeared.  Without the html tags, the
paragraphing was present but the tables were not properly columised.
Basically I want to create the following


Hi Joe,


Here is the data.






Date                 Time                 Venue   

May 3               6:00pm             Here

May 10             7:00pm             There


Here is a snippet from the script.  $notes contains the covering note i.e Hi
Joe etc.


$body = "";

            $body .= "$notes";

            $body .= "<br /><br /><br />";

            $body .= "<html>";



            $body .= "<table border = '1'><tr><td width = '100'>Team
1</td><td width = '100'>Team 2</td><td width = '60'>Date</td>

                                                <td width =
'60'>Time</td><td width = '100'>Umpire</td><td width =




            $query = "SELECT new_round_id, team1_name, team2_name,
matchdate, match_time, umpire, venue FROM $compdraw WHERE grade = '$grd' AND
new_round_id = '$required_round' ORDER BY matchdate, match_time, game_no";
//Delete this line when new database is loaded.

            if (@mysql_query ($query)) {                   

                        ($r = mysql_query ($query));  // Run the query.

                                    while ($row = mysql_fetch_array ($r)){

                                    $row[3]= date('j M', $row[3]);

                                                if ($row[1]!=='Bye' AND
$row[2]!=='Bye') {

                                                $row[4]= date('g:ia',




                                    $body .=           "<tr><td width =

width = '120'>$row[2]</td>

width = '60'>$row[3]</td>

width = '60'>$row[4]</td>

width = '120'>$row[5]</td>

width = '120'>$row[6]</td></tr>";                   




                                    $body .= "</table>";

                                    $body .= "<br /><br /><br />";

                                    $body .= "</html>";

}else {print "DATABASE ACCESS ERROR!!!";






$to = "";

foreach($emailgroup as $value) {

            $to .= "$value;";




// Always set content-type when sending HTML email

$headers = "MIME-Version: 1.0" . "\r\n";

$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";// More

$headers .= 'From: $from' . "\r\n";



if (mail($to, $subject, $body, $headers)) {

  echo("<p>Message successfully sent!</p>");

 } else {

  echo("<p>Message delivery failed...</p>");







More information about the thelist mailing list