Jump to content

RE: Sending SQL query to email in html small bug - please help


cyberwar

Recommended Posts

Hallo There,

 

I have some code I got from the internet a while ago. The code works as follow:

 

1.) The script runs from the command prompt (Linux) with a contab service

2.) It executes a specified SQL query against a MySQL database server

3.) Formats the output of the result set into an html table

4.) Sends the result to an email address

 

I am not a programmer :-(

I have been using the code with great success for some time, but it has the following bug: On certain email clients it displays as text and not as an html email. Especially on mac operated systems and iPhones/iPADs etc.

 

Any assistance would be greatly appreciated.

 

Thanks in advance

CW

 

<code starts>

 

<?php

 

//define the receiver of the email

$to = 'receiver@mail.com';

 

//define the subject of the email

$subject = 'My Subject';

 

//create a boundary string. It must be unique

//so we use the MD5 algorithm to generate a random hash

$random_hash = md5(date('r', time()));

 

//define the headers we want passed. Note that they are separated with \r\n

$headers = "From: sernder@mail.com\r\nReply-To: noreply@mail.com";

 

//add boundary string and mime type specification

$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";

 

//define the body of the message.

ob_start(); //Turn on output buffering

?>

--PHP-alt-<?php echo $random_hash; ?>

Content-Type: text/html; charset="iso-8859-1"

Content-Transfer-Encoding: 7bit

 

<h2>Some text for heading...</h2>

 

<?php

 

//Call the function like this:

echo SQLResultTable("SELECT * from table_name;");

function SQLResultTable($Query)

{

//mysql user variables

$HOST = "localhost";

$USER = "mysqluser";

$PASS = "password";

$DB = "database";

 

    $link = mysql_connect($HOST, $USER, $PASS) or die('Could not connect: ' . mysql_error());  //build MySQL Link

    mysql_select_db($DB) or die('Could not select database');        //select database

    $Table = "";  //initialize table variable

 

    $Table.= "<table border='1' style=\"border-collapse: collapse;\">"; //Open HTML Table

 

    $Result = mysql_query($Query); //Execute the query

    if(mysql_error())

    {

        $Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";

    }

    else

    {

        //Header Row with Field Names

        $NumFields = mysql_num_fields($Result);

        $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\">";

        for ($i=0; $i < $NumFields; $i++)

        {

            $Table.= "<th>" . mysql_field_name($Result, $i) . "</th>";

        }

        $Table.= "</tr>";

 

        //Loop thru results

        $RowCt = 0; //Row Counter

        while($Row = mysql_fetch_assoc($Result))

        {

            //Alternate colors for rows

            if($RowCt++ % 2 == 0) $Style = "background-color: #00CCCC;";

            else $Style = "background-color: #0099CC;";

 

            $Table.= "<tr style=\"$Style\">";

            //Loop thru each field

            foreach($Row as $field => $value)

            {

                $Table.= "<td>$value</td>";

            }

            $Table.= "</tr>";

        }

        $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\"><td colspan='$NumFields'>Query Returned " . mysql_num_rows($Result) . " records</td></tr>";

    }

    $Table.= "</table>";

 

    return $Table;

}

?>

<h2>Disclaimer</h2>

<p>

 

Some text ... This is my Discaimer (footer)...

 

</p>

--PHP-alt-<?php echo $random_hash; ?>--

<?

//copy current buffer contents into $message variable and delete current output buffer

$message = ob_get_clean();

//send the email

$mail_sent = @mail( $to, $subject, $message, $headers );

//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"

echo $mail_sent ? "Mail sent" : "Mail failed";

?>

 

 

 

</code ends>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.