Help sending result of query via email

Discussion in 'PHP' started by getwiththeprogram, Sep 29, 2006.

  1. #1
    Hi all,
    I'm a newbie and having trouble working out how to send the results of a query via email.

    I have looked at the mail function on php.net and on google which says I should use a pear class... can anyone advise on example or tutorial to help me out.

    Thanks

    $query = "SELECT date, url FROM `test` WHERE date < (CURDATE() - INTERVAL -1 DAY )";
    // execute query
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
    // see if any rows were returned
    if (mysql_num_rows($result) > 0) {
        // yes
        // print them one after another
        echo "<table cellpadding=10 border=1>";
        while($row = mysql_fetch_row($result)) {
            echo "<tr>";
            echo "<td>".$row[0]."</td>";
            echo "<td>" . $row[1]."</td>";
            echo "<td>".$row[2]."</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
    else {
        // no
        // print status message
        echo "No rows found!";
    }
    
    // free result set memory
    mysql_free_result($result);
    // close connection
    mysql_close($connection);
    Code (markup):
     
    getwiththeprogram, Sep 29, 2006 IP
  2. aditya_sfs

    aditya_sfs Peon

    Messages:
    2,271
    Likes Received:
    389
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Store the whole query output in a variable as HTML. You can start like this -

    $message = '<html><head><title>Output</title></head></html><body>';

    Then inside that block of the PHP code store all results in the message variabe like this -

    if (mysql_num_rows($result) > 0) {
    // yes
    // print them one after another
    $message .= "<table cellpadding=10 border=1>";
    while($row = mysql_fetch_row($result)) {
    $message .= "<tr>";
    $message .= "<td>";
    $message .=$row[0];
    $message .="</td>";
    $message .= "<td>";
    $message .=$row[1];
    $message .="</td>";
    $message .="<td>";
    $message .=$row[2];
    $message .="</td>";
    $message .= "</tr>";
    }
    $message .= "</table>";
    }
    else {
    // no
    // print status message
    $message .= "No rows found!";
    }

    end with something like this

    $message .="</body></html>

    Then configugure something like this

    $to = 'aidan@example.com' . ', '; // note the comma
    $subject = 'Birthday Reminders for August';
    // 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: ' . "\r\n";
    $headers .= 'Bcc: ' . "\r\n";

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

    I think that will do your job

    regards
    aditya
     
    aditya_sfs, Sep 29, 2006 IP