Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

PHP mail with HTML

the mail() function

Moderators: macek, egami, gesf

PHP mail with HTML

Postby Abet » Mon Apr 11, 2011 6:03 am

Hi guys, I am quite new to PHP and I was wondering if there is a way where we can send out the results of the database table which was created by using PHP/MYSQL and have it print out the results in table format exactly how it shows up and send it as a email exactly how it is shown. The table was created when we added the data through the form.

------------------------------------
| Student | Grade | Course |
------------------------------------
| Jack | Grade B | Algebra 2 |
------------------------------------


Is this possible? Thanks for your help.
Abet
New php-forum User
New php-forum User
 
Posts: 4
Joined: Mon Apr 11, 2011 5:41 am

Re: PHP mail with HTML

Postby johnj » Tue Apr 12, 2011 8:44 am

Yes this is possible provided you can recreate the whole thing in html. How to send the html by email - please read egami's reply on a similar topic here,
viewtopic.php?f=17&t=12178
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: PHP mail with HTML

Postby Abet » Wed Apr 13, 2011 12:29 pm

thanks johnj, I have read the article and am now able to send the email as HTML but I am still having trouble trying to figure out how to grab the data from the database as there is PHP involved in the coding so under $message, could i post $message=POST[$email] for the email section? So would I have to fill in the form everytime before getting the results such as this link shown but with the variables I have?

http://www.w3schools.com/php/php_mail.asp

this is how the table would show and I would just like to have a button and send the exact results as shown:
http://lulzimg.com/i17/3c65b4.png
Abet
New php-forum User
New php-forum User
 
Posts: 4
Joined: Mon Apr 11, 2011 5:41 am

Re: PHP mail with HTML

Postby johnj » Thu Apr 14, 2011 2:00 am

Assuming that the table showed in the image is <table><tr><td>Product</td>......</table>". Move this to a php variable.e.g. $myMessage = "<table><tr><td>Field1</td>......</table>"; Then, continue with sending the email..use $myMessage as the message to be sent.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: PHP mail with HTML

Postby Abet » Thu Apr 14, 2011 2:40 pm

Hi John,

I did what you stated and now when I click on the button to send it gives me this error message: SELECT Error: No database selected

Here is the code for the Mail section:

Code: Select all
<html>
<head>

<?php
// recipient
$to  = 'somebody@aol.com' ;

// subject
$subject = 'test Email';


$result1 = mysql_query( "SELECT product, issueTime, hostServer, issue, impact, escalateTo, resolution,
technician
 FROM nguyen_hien_duc_reports WHERE reportDate = '$today' AND shift = 'First'
 ORDER BY product" )
   or die("SELECT Error: ".mysql_error());
   $num_rows = mysql_num_rows($result1);
   //print "There are $num_rows records.<br><br>";
   print "<table width=1240 border=1 >";
   
   print "$today2  7AM - 4PM";
   
   
   //print

"<tr><th>report_id</th><th>product</th><th>reportDate</th><th>issueTime</th><th>hostServer</th><th

>issue</th><th>impact</th><th>escalateTo</th><th>resolution</th>
   print "<tr bgcolor=#00ffff><th><b>Product</b></th><th><b>Issue

Time</b></th><th><b>Host/Server</b></th><th><b>Issue</b></th><th><b>Impact</b></th><th><b>Esca

late To</b></th><th><b>Resolution</b></th>
   <th><b>Technician</b></th></tr>";
   while ($get_info = mysql_fetch_row($result1)){
   print "<tr>";
   foreach ($get_info as $field)
   print "<td><center>$field</center>&nbsp;</td>";
   print "</tr>";   
   }
print "</table>";
mysql_close($link);
?>

$myMessage =
$result1

// message
$message = '
$myMessage
</body>
</html>
';

// 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 .= 'From: Joe <joe@aol.com>' . "\r\n";

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



If i understood what you were saying then that should be correct.

also here is the code that creates the table so you have a understanding of where I may have made the mistake.

Code: Select all
<html><head><title>Issues Display Record Today</title>
<style type="text/css">
body {text-align:center;}
th, td {font-family: tahoma, arial, verdana; text-align:center; font-size: 10pt; font-weight: 500}
</style>
</head>
<body>

<?
include('NGUYEN_HIEN_DUC/db_connection_info.inc');
 
$link = mysql_connect('localhost',$cs85Username,$cs85Password);
/* Change next two lines if using online*/
$db="albums";
//$link = mysql_connect('localhost', 'root', '');
if (! $link)
die(mysql_error());
mysql_select_db(albums , $link)
or die("Couldn't open $db: ".mysql_error());
mysql_select_db(albums , $link) or die("Select Error: ".mysql_error());

$day = mktime(0,0,0,date("m"),date("d"),date("Y"));
// $issue=$_POST['issue'];
$today = date("Y-m-d", $day);
$today2 = date("m/d/Y", $day);
$yesterday = date("m/d/Y", $day - 1);
$yesterday2 = date("Y-m-d", $day - 1);
// $issue=$_POST['issue'];
//  $issueTime=$_POST['issueTime'];

   $result1 = mysql_query( "SELECT product, issueTime, hostServer, issue, impact, escalateTo, resolution, technician
 FROM nguyen_hien_duc_reports WHERE reportDate = '$today' AND shift = 'First'
 ORDER BY product" )
   or die("SELECT Error: ".mysql_error());
   $num_rows = mysql_num_rows($result1);
   //print "There are $num_rows records.<br><br>";
   print "<table width=1240 border=1 >";
   
   print "$today2  7AM - 4PM";
   
   
   //print "<tr><th>report_id</th><th>product</th><th>reportDate</th><th>issueTime</th><th>hostServer</th><th>issue</th><th>impact</th><th>escalateTo</th><th>resolution</th>
   print "<tr bgcolor=#00ffff><th><b>Product</b></th><th><b>Issue Time</b></th><th><b>Host/Server</b></th><th><b>Issue</b></th><th><b>Impact</b></th><th><b>Escalate To</b></th><th><b>Resolution</b></th>
   <th><b>Technician</b></th></tr>";
   while ($get_info = mysql_fetch_row($result1)){
   print "<tr>";
   foreach ($get_info as $field)
   print "<td><center>$field</center>&nbsp;</td>";
   print "</tr>";   
   }
print "</table>";
mysql_close($link);
?>
Abet
New php-forum User
New php-forum User
 
Posts: 4
Joined: Mon Apr 11, 2011 5:41 am

Re: PHP mail with HTML

Postby johnj » Fri Apr 15, 2011 6:17 am

You need to establish a connection with the database, select a database etc...please take a look at this page
http://www.php.net/manual/en/function.m ... ect-db.php
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: PHP mail with HTML

Postby Abet » Sat Apr 16, 2011 1:20 am

Hi John,

thank you for your response...I now have the email page connected to the database and it shows me the result when I click on the button but I it's not sending the message. Am I missing something? I hope I did get the previous results correct from what you stated :)

Code: Select all
<html>
<head>

<?php
// connect to database
include('NGUYEN_HIEN_DUC/db_connection_info.inc');
 $day = mktime(0,0,0,date("m"),date("d"),date("Y"));
// $issue=$_POST['issue'];
$today = date("Y-m-d", $day);
$today2 = date("m/d/Y", $day);
$yesterday = date("m/d/Y", $day - 1);
$yesterday2 = date("Y-m-d", $day - 1);
// $issue=$_POST['issue'];
//  $issueTime=$_POST['issueTime'];

 
$link = mysql_connect('localhost',$cs85Username,$cs85Password);
/* Change next two lines if using online*/
$db="albums";
//$link = mysql_connect('localhost', 'root', '');
if (! $link)
die(mysql_error());
mysql_select_db(albums , $link)
or die("Couldn't open $db: ".mysql_error());
mysql_select_db(albums , $link) or die("Select Error: ".mysql_error());

// current db
$db_selected = mysql_select_db('albums', $link);
if (!$db_selected) {
    die ('Can\'t use albums : ' . mysql_error());
}

// recipient
$to  = 'daniel.hua@hypermediasystems.com' ;

// subject
$subject = 'test Email';


$result1 = mysql_query( "SELECT product, issueTime, hostServer, issue, impact, escalateTo, resolution,

technician
 FROM nguyen_hien_duc_reports WHERE reportDate = '$today' AND shift = 'First'
 ORDER BY product" )
   or die("SELECT Error: ".mysql_error());
   $num_rows = mysql_num_rows($result1);
   //print "There are $num_rows records.<br><br>";
   print "<table width=1240 border=1 >";
   
   print "$today2  7AM - 4PM";
   
   
   //print

"<tr><th>report_id</th><th>product</th><th>reportDate</th><th>issueTime</th><th>hostServer</th><th

>issue</th><th>impact</th><th>escalateTo</th><th>resolution</th>
   print "<tr bgcolor=#00ffff><th><b>Product</b></th><th><b>Issue

Time</b></th><th><b>Host/Server</b></th><th><b>Issue</b></th><th><b>Impact</b></th><th><b>Esca

late To</b></th><th><b>Resolution</b></th>
   <th><b>Technician</b></th></tr>";
   while ($get_info = mysql_fetch_row($result1)){
   print "<tr>";
   foreach ($get_info as $field)
   print "<td><center>$field</center>&nbsp;</td>";
   print "</tr>";   
   }
print "</table>";
mysql_close($link);
?>

$myMessage =
$result1

// message
$message = '
$myMessage
</body>
</html>
';

// 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 .= 'From: Daniel Hua <daniel.hua@hypermediasystems.com>' . "\r\n";

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


http://lulzimg.com/view/ed1dd0.png
Abet
New php-forum User
New php-forum User
 
Posts: 4
Joined: Mon Apr 11, 2011 5:41 am

Re: PHP mail with HTML

Postby johnj » Sat Apr 16, 2011 6:16 am

a). First, print all these ($to, $subject, $message, $headers) to see if all these has appropriate required values.

b). Then, to to your error log to see what error message does php post

c). You need to code your functions with appropriate error validation: for e.g. mail() should be mail() or die("error")...or something similar...

d). Check your php.ini for the name of the smtp server, port, from email address etc
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to PHP coding => Mail

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron