LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   send output of mysql query to email within same script (https://www.linuxquestions.org/questions/linux-newbie-8/send-output-of-mysql-query-to-email-within-same-script-685331/)

hattori.hanzo 11-22-2008 02:38 AM

send output of mysql query to email within same script
 
I have some code in a php script which produces a table of results. Whats the best and simplest way to send this via email within the same php script?

I dont really want to pipe the output to a text file then use 'mail' from within the system.

Code:

// Display Report title
print "Report\n====================\n";
// Display column headings
print "Host\t\tCount\n";

// Fetch and display the results
while ($result_row = mysql_fetch_row(($result)))
{

printf($result_row[0]);
print "\t";
printf($result_row[1]);
print "\n";

}

regards,

jstephens84 11-22-2008 06:53 PM

Create a variable called mailtxt and use the .= to keep appending the results. Then use the built in mail function for email. That should get what you want.

Code:

// Display Report title
$mailtxt;
$mailtxt .= "Report\n====================\n";
// Display column headings
$mailtxt .= "Host\t\tCount\n";

// Fetch and display the results
while ($result_row = mysql_fetch_row(($result)))
{
 global $mailtxt;
$mailtxt .= $result_row[0];
$mailtxt .= "\t";
$mailtxt .= $result_row[1];
$mailtxt .= "\n";

}

$email = "email@adress.com"; //senders e-mail adress
$recipient = "PersonWhoGetsIt@emailadress.com"; //recipient
$subject = "Subject for reviever"; //subject

mail($recipient, $subject, $mailtxt); //mail command :)

this code should be a start.

hattori.hanzo 12-03-2008 03:14 AM

thanks mate. works perfectly.


All times are GMT -5. The time now is 02:56 PM.