Computing.Net > Forums > Web Development > How to email a report

How to email a report

Reply to Message Icon

Original Message
Name: swati
Date: July 20, 2005 at 09:13:30 Pacific
Subject: How to email a report
OS: Win XP
CPU/Ram: pentium 256mb ram
Comment:

Hi,
I have a report generated thru the foll php script that appears on my screen.I want to email this report without using the file /send feature of any email program but directly from the page itself using a php script that says ' email this report to a/b/c/d or whoever.

Any and all advise welcome ! Thanks !


<? print(date("l F d, Y")); ?>


Monthly Report


Name :
<?php
$month = $HTTP_POST_VARS["month"];
$year = $HTTP_POST_VARS["year"];
$serial = 0;

mysql_connect("localhost", $dbname, $dbpasswd )
or die ("Unable to connect to server.");

mysql_select_db($database)
or die ("Unable to select database.");

$result = mysql_query("SELECT * FROM `Temps` WHERE Temp='$temp' ");
$myrow = mysql_fetch_array($result);

echo "$temp
Month : ";
if ($month == 1)
{
$month_name = "January";
echo $month_name;
}
if ($month == 2)
{
$month_name = "February";
echo $month_name;
}

echo ", $year";

?>


<table border="1" style="border-collapse: collapse" bordercolor="#111111" width="100%">
<form method="post" action="temp.php?name=<? echo $temp ?>">
<tr bgcolor="#D3D3D3">
<td>Serial</td>
<td>Date</td>
<td>Trip</td>
<td>From</td>
<td>To</td>

</tr>


<?php
$serial = 0;
mysql_connect("localhost", $dbname, $dbpasswd )
or die ("Unable to connect to server.");

mysql_select_db($database)
or die ("Unable to select database.");

$result = mysql_query("SELECT * FROM `Drive` WHERE Temp='$temp' && MONTH(`Date`)=$month && YEAR(`Date`)=$year ORDER BY `Date` asc");


if ($myrow = mysql_fetch_array($result)) {


do {
$spot=$myrow["id"];
$result1 = mysql_query("SELECT * FROM `Drive` WHERE TDEID='$spot' ");
$myrow2 = mysql_fetch_array($result1);
$Name=$myrow2["Name"];
$serial+=1;
printf("<tr>
<td> %s </td>
<td align=\"center\"> %s </td>
<td align=\"center\"> %s </td>
<td> %s </td>
<td> %s </td>
</tr>",
$serial,
calculatedate($myrow["Date"]),
$myrow["Trip"],
$myrow["From"],
$myrow["To"]
);

} while ($myrow = mysql_fetch_array($result));


} else {

echo "Sorry, no records were found!";
}
?>
</table>
</div>
</body>
</html>


swati


Report Offensive Message For Removal


Response Number 1
Name: Laler
Date: July 20, 2005 at 16:57:23 Pacific
Subject: How to email a report
Reply: (edit)

hi,

are you familiar with PHP? If so, you can put the data into $message and send it directly from the server. Sample:

$to = 'your_email@domain.com';
$subject = date ("d-m-y", time()) . " report";

$message = '';
$message .= 'add everything you want here';

mail ($to, $subject, $message);

:::::

I was going to give you clues about how to put your data into $message, but since it's too many then I'm afraid I'll just confuse things up :D

---
Site of the Day


Report Offensive Follow Up For Removal

Response Number 2
Name: swati
Date: July 21, 2005 at 07:21:54 Pacific
Subject: How to email a report
Reply: (edit)

Hi,
Thanks for reading thru my message.

I am not too familiar with php .I can do the send mail bit(see below) but do i insert it at the bottom of the php file that i have pasted above which is called temptrip.php. And if I do then does the $message = "temptrip.php" bit work ?

Or any other suggestions ?
<?php
$result2 = mysql_query("SELECT * FROM `users` WHERE x='$x'");
$myrow2 = mysql_fetch_array($result2);

$current=(date("l F d, Y"));
if ( $send_mail == 1 )
{
$to_mail = "abc@abc.com";
$subject = "Trip Details Submitted On:".$current." By ".$x." (".$myrow2["y"]." ".$myrow2["z"].") ";
$message = " temptrip.php "

";
$from_mail = "def@def.com";
$headers = "From: $from_mail\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

if ( mail( $to_mail, $subject, $message, $headers ) )
{
echo "

This data has been emailed.;


}
else
{
echo "
There was an error while sending mail. Contact your administrator

";
}
}
?>

swati


Report Offensive Follow Up For Removal

Response Number 3
Name: Laler
Date: July 21, 2005 at 15:36:47 Pacific
Subject: How to email a report
Reply: (edit)

Hi,

do i insert it at the bottom of the php file that i have pasted above which is called temptrip.php. And if I do then does the $message = "temptrip.php" bit work ?

It's best if you fill $message one by one, on each desired output, which will be different from the web output (e.g.: no html tags).

After all desired output is placed into $message, then you can do mail() at the bottom (in the same script - temptrip.php).

You can probably just put the whole temptrip.php into $message, but I think you must tell PHP to read the file normally like a web browser do, and not by simply doing $message = "temptrip.php"; but then the html tags will also be put into $message.

You can then probably strip html tags from $message (there's a PHP function for it) but still, I think it's better if you put values into $message directly in temptrip.php, slowly one by one after each output is produced.

Who made that mailer script? If it's not you, can't you ask him/her to fix it? Because as I said above, if I try to write a script for you then there'll be too many possibilities for error (and confusion). Like your temptrip.php overthere, where's the opening html tag? It's best if you can ask someone whom you can trust to test things directly on your server, along with the database connection, etc. :)


---
Site of the Day


Report Offensive Follow Up For Removal

Response Number 4
Name: Laler
Date: July 21, 2005 at 15:45:00 Pacific
Subject: How to email a report
Reply: (edit)

But of course you can keep asking questions in here, especially if you're trying to fix it yourself. If the question is clear and specific enough, lots of people here will help :)

---
Site of the Day


Report Offensive Follow Up For Removal

Response Number 5
Name: swati
Date: July 21, 2005 at 22:47:32 Pacific
Subject: How to email a report
Reply: (edit)

hi,
the guy who set it up is not working here anymore so i am bug fixer in charge ! Pl. do let me know what the $message= should read as.
I didnt put in the html tags because i thot it wasnt relevant to my query- clearly i was wrong !
Thanks !


swati


Report Offensive Follow Up For Removal


Response Number 6
Name: Laler
Date: July 22, 2005 at 09:06:38 Pacific
Subject: How to email a report
Reply: (edit)

ok if you want to try, let's do it slowly so it'll be easier to troubleshoot problems.

First, I want to make sure the purpose. When temptrip.php is opened, it'll show report on the browser and also sends an e-mail, is this correct?

If it is, add these at the bottom of temptrip.php, after the </html> tag:

<?php
$to = "your@email.com";
$subject = "Trip Details Submitted On: " . date("l F d, Y", time ());

$message = "Monthly Report

Name : " . $temp . "
Month : ";

if (isset ($month_name)){
$message .= $month_name;
}else{
$message .= $month;
}

mail ($to, $subject, $message);
?>

Replace your@email.com with your email, save and upload the file.

open it from your browser, wait for a moment and check the email (check the bulk/spam folder also). See if the email got sent, then post back here.

I dont use anything from your 2nd script because I think it should be included somewhere, the script won't work by itself. Like where'd $x got defined (in the 2nd script)?

Don't forget to backup temptrip.php first.

---
Site of the Day


Report Offensive Follow Up For Removal

Response Number 7
Name: swati
Date: July 23, 2005 at 09:24:18 Pacific
Subject: How to email a report
Reply: (edit)

Hi and Thank You!

a. I did backup temptrip.
b. You understood my reqt. correctly.
c. The email got sent from 'apache' ..as below:
d. Now how do I get the contents of the report and the correct from-mail id to appear ?


From "Apache" <apache@abc.def.com>
Subject Trip Details Submitted On: Saturday July 23, 2005
Date Sat, July 23, 2005 10:13 am
To abc@xyz.com


Monthly Report

Name : Noel
Month : February


Report Offensive Follow Up For Removal

Response Number 8
Name: swati
Date: July 23, 2005 at 10:07:20 Pacific
Subject: How to email a report
Reply: (edit)

Hi,
I got the from-mail id set up (see below) but the time is not showing up( not really relevant but its a bother !) and ofcourse the rest of the actual report.


<?php
$to = "abc@def.com";
$subject = "Trip Details Submitted On: " . date("l F d, Y", time ());
$from = "form@def.com";
$message = "Monthly Report

Name : " . $temp . "
Month : ";

if (isset ($month_name)){
$message .= $month_name;
}else{
$message .= $month;
}

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

mail ($to, $subject, $message, $headers);
?>

swati


Report Offensive Follow Up For Removal

Response Number 9
Name: Laler
Date: July 23, 2005 at 14:03:19 Pacific
Subject: How to email a report
Reply: (edit)

but the time is not showing up

You mean on the subject? On your script on Response #8? Now that's weird :( date("l F d, Y", time ()) should always produce current time in GMT (according to server settings) no matter what.

But anyways, you may try to modify temptrip.php into this below. Remember, I only modify things you paste. So if there's some more code above, don't delete them:

<? print(date("l F d, Y")); ?>


Monthly Report


Name :
<?php
$month = $HTTP_POST_VARS["month"];
$year = $HTTP_POST_VARS["year"];
$serial = 0;

mysql_connect("localhost", $dbname, $dbpasswd )
or die ("Unable to connect to server.");

mysql_select_db($database)
or die ("Unable to select database.");

$result = mysql_query("SELECT * FROM `Temps` WHERE Temp='$temp' ");
$myrow = mysql_fetch_array($result);

echo "$temp
Month : ";
if ($month == 1)
{
$month_name = "January";
echo $month_name;
}
if ($month == 2)
{
$month_name = "February";
echo $month_name;
}

echo ", $year";

?>


<table border="1" style="border-collapse: collapse" bordercolor="#111111" width="100%">
<form method="post" action="temp.php?name=<? echo $temp ?>">
<tr bgcolor="#D3D3D3">
<td>Serial</td>
<td>Date</td>
<td>Trip</td>
<td>From</td>
<td>To</td>

</tr>


<?php
$serial = 0;
mysql_connect("localhost", $dbname, $dbpasswd )
or die ("Unable to connect to server.");

mysql_select_db($database)
or die ("Unable to select database.");

$result = mysql_query("SELECT * FROM `Drive` WHERE Temp='$temp' && MONTH(`Date`)=$month && YEAR(`Date`)=$year ORDER BY `Date` asc");

// Here
$tmp_message = "Serial | Date | Trip | From | To
================================
";
// To here

if ($myrow = mysql_fetch_array($result)) {


do {
$spot=$myrow["id"];
$result1 = mysql_query("SELECT * FROM `Drive` WHERE TDEID='$spot' ");
$myrow2 = mysql_fetch_array($result1);
$Name=$myrow2["Name"];
$serial+=1;
printf("<tr>
<td> %s </td>
<td align=\"center\"> %s </td>
<td align=\"center\"> %s </td>
<td> %s </td>
<td> %s </td>
</tr>",
$serial,
calculatedate($myrow["Date"]),
$myrow["Trip"],
$myrow["From"],
$myrow["To"]
);

// Here
$thedate = calculatedate($myrow["Date"]);
$tmp_message .= $serial." | ".$thedate." | ".$myrow["Trip"]." | ".$myrow["From"]." | ".$myrow["To"];
$tmp_message .= "\r\n";
// To here

} while ($myrow = mysql_fetch_array($result));


} else {

// Here
$tmp_message .= "Sorry, no records were found!";
// To here

echo "Sorry, no records were found!";
}
?>
</table>
</div>
</body>
</html>
<?php
$to = "Your Email<abc@def.com>";
$subject = "Trip Details Submitted On: " . date("l F d, Y", time ());
$headers = "From: Webserver<form@def.com>"."\r\n";

$message = "Monthly Report

Name : " . $temp . "
Month : ";

if (isset ($month_name)){
$message .= $month_name;
}else{
$message .= $month;
}

$message .= ", " . $year . "\r\n\r\n";

$message .= $tmp_message;

mail ($to, $subject, $message, $headers);
?>


If the above works, you can add or remove spaces on the added code above, and view the email in proportional font (like courier) so it'll be easier to read

Sending HTML email is actually not very hard to do, especially if you're the only one who'll read the email (and thus you'll know to read it in an HTML email client). But let's discuss that on another topic :D

---
Site of the Day


Report Offensive Follow Up For Removal

Response Number 10
Name: swati
Date: July 23, 2005 at 22:50:43 Pacific
Subject: How to email a report
Reply: (edit)

Hi !

a.) The time doesnt appear
in the subject line. (See below)

Subject: Trip Details Submitted On: Sunday July 24, 2005
From: form@def.com
Date: Sun, July 24, 2005 12:40 am
To: abc@def.com

The php script i have for this is :

$subject = "Trip Details Submitted On: " . date("l F d, Y", time ());

b.)I got all the data to appear on the email but sometimes the trip info is a long sentence which then means the page formatting doesnt remain A4 with all fields coming up on a single line but spilling over to the next. This wouldnt be a problem if I could do a wrap text and have the email come in a grid form.Any pointers ?

c.)Another thing: I was wondering if there is anyway I could see the report and then email it without creating another php file ? Basically, if I could put a line like - 'click here to email this page' without it having to get processed into another page...

Again, many many thanks for all the help you have been giving me.

swati


Report Offensive Follow Up For Removal

Response Number 11
Name: Laler
Date: July 24, 2005 at 09:51:01 Pacific
Subject: How to email a report
Reply: (edit)

hi, no problem. I'm learning as well.

a)
ah, you mean _the_ time, here's to show date and time:

date("l F d, Y - g:i a", time ());

Look here, I think you can then customize the format to suits you best.


b)
Yes, it's a "problem" on plain text email. You should use HTML then.

This temptrip.php is hopefully works to send an HTML e-mail.

c)
It is possible to be done in that 1 file. But as there're still some parts missing, like the form (that defines $month & $year), etc, I can't help you.

Maybe it's easier to create another file let's say temptripmailer.php, Which looks exactly like temptrip.php but not a single output thrown to the screen, by removing all shown html tags (not the ones in $message), all echos, and all printf()...

Then you may echo 'Thank you, e-mail sent!';

At the temptrip.php, you need to create a link to temptripmailer.php, which will pass the same $_POST as in temptrip.php itself, and also any needed variables.

:::::

Can the site use session? If so then it might be easier for me to give example of normal temptrip.php with links to temptripmailer.php, and also the temptripmailer.php itself...

with session, I can put $message into a session and pass it to temptripmailer.php without the need of a form, etc.


---
Site of the Day


Report Offensive Follow Up For Removal

Response Number 12
Name: swati
Date: July 25, 2005 at 21:45:37 Pacific
Subject: How to email a report
Reply: (edit)

Hi,
a. I have no clue if the site uses session . I guess I have to ask the site admin for that info. correct ?

b. The temptripmailer.php is a great idea but it would have to come in the temptripform.php file ?

c.temptripform.php (defining month etc) is below.

d.I ave put in another message on a leave record script..can you please tell me what i am doing wrong?

Thanks again.

Temptripform.php
================
<? include("protection.php"); ?>

<td width="85%" align="left" valign="top">Please enter the month and the year for which you want to see the report


<form method="post" action="temptrip.php">
<table border="0">
<tr><td>Name:</td><td>
<select name="temp" >
<option value="">[Select One]
<?php

mysql_connect("localhost", $dbname, $dbpasswd )
or die ("Unable to connect to server.");

mysql_select_db($database)
or die ("Unable to select database.");

$result = mysql_query("SELECT * FROM `Temps`");

if ($myrow = mysql_fetch_array($result)) {
do {
printf("<option>
%s", $myrow["Temp"]);
} while ($myrow = mysql_fetch_array($result));

}
?>
</select></p>

<tr><td>Month: </td><td><select name="month">
<option value="">[Select One]
<option value="1">January
<option value="2">February
<option value="3">March
<option value="4">April
<option value="5">May
<option value="6">June
<option value="7">July
<option value="8">August
<option value="9">September
<option value="10">October
<option value="11">November
<option value="12">December
</select> Mandatory field</td></tr>
<tr><td>Year: </td><td><select name="year">
<option value="">[Select One]
<option>2005
<option>2006
</selecttr></td>

</table>



<input type="submit" name="submit" value="Get Report"> </p>
</form>

Back

Sign Out

</tr>
</table>
</div>

swati


Report Offensive Follow Up For Removal

Response Number 13
Name: Laler
Date: July 26, 2005 at 09:16:12 Pacific
Subject: How to email a report
Reply: (edit)

I still don't know a simple way to call temptripmailer.php by the code you've pasted above without using session. You must use session. Otherwise, some javascript will be required and I'm more confident with PHP.

Session is (i think) available with all PHP. So basically you must call this function:

session_start();

on each pageload.

If the temptrip.php is called directly, you can add that line at the top of the script, before anything sent to the browser, not even a single space. If the top of the script are HTML tags, then add this line on topmost of it:

<?php session_start(); ?>

If all navigations on your site is only calling for a single file, let's say index.php (and all other files are called/managed from within that file), it'll be easier... Just add session_start(); on top of index.php

Look at the file in the address bar when you're viewing codes in temptrip.php and temptripform.php, that's the main file.

:::::

After that, open up the last temptrip.php (I assume it worked) and modify the mailer part at the bottom:

<?php
$to = 'defining to';
$subject = 'defining subject';
$headers = 'defining headers';
$headers .= 'defining headers';
$headers .= 'defining headers';

$message .= 'defining message';


// then, don't do the mail, but put data into session instead
// the session names are not the same with var names to saves troubles when
// you have register_globals on
$_SESSION['themail_to'] = $to;
$_SESSION['themail_subject'] = $subject;
$_SESSION['themail_headers'] = $headers;
$_SESSION['themail_message'] = $message;
?>

Then you add this line somewhere in the views of temptrip.php:

<a href="temptripmailer.php">Send this report to e-mail</a>

Then here's the content of temptripmailer.php

---
Site of the Day



Report Offensive Follow Up For Removal

Response Number 14
Name: swati
Date: July 26, 2005 at 09:29:49 Pacific
Subject: How to email a report
Reply: (edit)

<? include("protection.php"); ?>
is the 1st line of every single php file that we have..does that help ?

It contains info. on db name, passwords and other such stuff.

I will go with the assumption that we use session and see.


Report Offensive Follow Up For Removal

Response Number 15
Name: Laler
Date: July 26, 2005 at 10:01:35 Pacific
Subject: How to email a report
Reply: (edit)

<?php
session_start();
include("protection.php");
?>

if you're sure that the file is called directly, not included from some other files.

and btw, did the HTML email works?

---
Site of the Day


Report Offensive Follow Up For Removal

Response Number 16
Name: swati
Date: July 29, 2005 at 09:03:45 Pacific
Subject: How to email a report
Reply: (edit)

hi,
I have been sick the past few days - just about recovered enough to look at a screen without my eyes blurring. the html mail did not work. foll. is what i did and couldnt figure what went wrong because i dont get the email although matter appears on screen.

Temptripform.php
================
<?php
session_start();
include("protection.php");
?>

<td width="85%" align="left" valign="top">Please enter the month and the year for which you want to see the report

<form method="post" action="temptrip.php">
<table border="0">
<tr><td>Name:</td><td>
<select name="temp" >
<option value="">[Select One]
<?php

mysql_connect("localhost", $dbname, $dbpasswd )
or die ("Unable to connect to server.");

mysql_select_db($database)
or die ("Unable to select database.");

$result = mysql_query("SELECT * FROM `Temps`");

if ($myrow = mysql_fetch_array($result)) {
do {
printf("<option>
%s", $myrow["Temp"]);
} while ($myrow = mysql_fetch_array($result));

}
?>
</select></p>
<tr><td>Month: </td><td><select name="month">
<option value="">[Select One]
<option value="1">January
<option value="2">February
<option value="3">March
<option value="4">April
<option value="5">May
<option value="6">June
<option value="7">July
<option value="8">August
<option value="9">September
<option value="10">October
<option value="11">November
<option value="12">December
</select> Mandatory field</td></tr>
<tr><td>Year: </td><td><select name="year">
<option value="">[Select One]
<option>2005
<option>2006
</selecttr></td>

</table>
<input type="submit" name="submit" value="Get Report"> </p>
</form>
Back
Sign Out
</tr>
</table>
</div>

Temptrip.php
=======================
<?php
session_start();
include("protection.php");
?>
<? print(date("l F d, Y")); ?>


Monthly Report


Name :
<?php
$month = $HTTP_POST_VARS["month"];
$year = $HTTP_POST_VARS["year"];
$serial = 0;

mysql_connect("localhost", $dbname, $dbpasswd )
or die ("Unable to connect to server.");

mysql_select_db($database)
or die ("Unable to select database.");

$result = mysql_query("SELECT * FROM `Temps` WHERE Temp='$temp' ");
$myrow = mysql_fetch_array($result);

echo "$temp
Month : ";
if ($month == 1)
{
$month_name = "January";
echo $month_name;
}
if ($month == 2)
{
$month_name = "February";
echo $month_name;
}

echo ", $year";

?>


<table border="1" style="border-collapse: collapse" bordercolor="#111111" width="100%">
<form method="post" action="temp.php?name=<? echo $temp ?>">
<tr bgcolor="#D3D3D3">
<td>Serial</td>
<td>Date</td>
<td>Trip</td>
<td>From</td>
<td>To</td>

</tr>


<?php
$serial = 0;
mysql_connect("localhost", $dbname, $dbpasswd )
or die ("Unable to connect to server.");

mysql_select_db($database)
or die ("Unable to select database.");

$result = mysql_query("SELECT * FROM `Drive` WHERE Temp='$temp' && MONTH(`Date`)=$month && YEAR(`Date`)=$year ORDER BY `Date` asc");

// Here
$tmp_message = "Serial | Date | Trip | From | To
================================
";
// To here

if ($myrow = mysql_fetch_array($result)) {


do {
$spot=$myrow["id"];
$result1 = mysql_query("SELECT * FROM `Drive` WHERE TDEID='$spot' ");
$myrow2 = mysql_fetch_array($result1);
$Name=$myrow2["Name"];
$serial+=1;
printf("<tr>
<td> %s </td>
<td align=\"center\"> %s </td>
<td align=\"center\"> %s </td>
<td> %s </td>
<td> %s </td>
</tr>",
$serial,
calculatedate($myrow["Date"]),
$myrow["Trip"],
$myrow["From"],
$myrow["To"]
);

// Here
$thedate = calculatedate($myrow["Date"]);
$tmp_message .= $serial." | ".$thedate." | ".$myrow["Trip"]." | ".$myrow["From"]." | ".$myrow["To"];
$tmp_message .= "\r\n";
// To here

} while ($myrow = mysql_fetch_array($result));


} else {

// Here
$tmp_message .= "Sorry, no records were found!";
// To here

echo "Sorry, no records were found!";
}
?>
</table>
</div>
</body>
</html>


<?php
$to = abc@def.com;
$subject = 'Trip Details Submitted On: " . date("l F d, Y", time ())';
$headers = form@def.com;
//$headers .= 'defining headers';
//$headers .= 'defining headers';

$message .= 'Monthly Report';


$_SESSION['abc@def.com'] = $to;
$_SESSION['Trip Details Submitted On: " . date("l F d, Y", time ())'] = $subject;
$_SESSION['From form@def.com'] = $headers;
$_SESSION['Monthly Report'] = $message;
?>

Email this report to jfk@def.com))



Report Offensive Follow Up For Removal

Response Number 17
Name: Laler
Date: August 1, 2005 at 11:35:15 Pacific
Subject: How to email a report
Reply: (edit)

ouch, so the HTML didnt work :D

I tested out sending HTML email with that code, and it's accepted perfectly (not in bulk) by Yahoo and Gmail (and outlook of course).

The difference in my testing code is, I don't put anything from your script, so it's only the table header and sample hardcoded rows.

So maybe I've made a mistake when trying to create the looping row from your code.

Sorry, it's really hard to do when I can't test anything :D But basically the concept is the same with the looping in the plain text e-mail, only now it created table rows instead of the looped output only.

---
Site of the Day


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: How to email a report  

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software