Sending an email with html and php contents

<?php
error_reporting(E_ALL);
ini_set('display_errors' ,1);
require "connection.php";


$to = "jasmine@hotmail.com";
$subject = "CURRENT_DATE";
?>
<?php

$query= "SELECT client_id, count(client_id) AS rows, array_agg(insurance) AS insurance from vouchers WHERE parsing_date=CURRENT_DATE GROUP BY client_id";
$result = pg_query($conn,$query);

?>

<!DOCTYPE html>
<html>
<head>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<link href = "http://fonts.googleapis.com/css?family=Roboto:400">

<style>
.responstable {
  margin: 1em 0;
  width: 100%;
  overflow: hidden;
  background: #FFF;
  color: #024457;
  border-radius: 10px;
  border: 1px solid #167F92;
  word-wrap: break-word;
}
.responstable tr {
  border: 1px solid #D9E4E6;
}
.responstable tr:nth-child(odd) {
  background-color: #EAF3F3;
}

</style>

</head>

<body>
<div class="container-fluid">
        <div>
        
          <h1>Clients</h1>
   
          
        </div>
        
<table class="responstable" rules='all' style='border-collapse: collapse;'>
<thead>
	<tr>
		<th>Client id</th>
		<th>Number of rows</th>
		<th>Insurance</th>
	 
	</tr>
</thead>
  <?php 
  	while($row = pg_fetch_array($result))
	{ 




$find ='{';
$replace = '' ;
$arr = $row['insurance'];
$insurance3= str_replace($find,$replace,$arr);
$insurance = str_replace('"','  ',$insurance3);

  ?>

  <tbody>


     <td><?php echo $row['client_id']; ?></td>
     <td><?php echo $row['rows'];?></td>   
     <td><?php echo $insurance; ?></td> 
      
    </tr>
  <?php  }

  
  ?>  </tbody>
</table>
 
</div>

</body>
</html> 


<?php 
$headers = "HI";
mail($to,$subject,$message,$headers);

?>

Open in new window


I want to send this email, but right now it only sends a blank email, I want it to send me the contents thats in the code, which is a report of 3 columns, I tried putting "$message = " are the start but I just got error.
LVL 1
Jazzy 1012Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
By default, mail() is going to send text content, so you have to do 3 things:

1. You have to capture that table output into your $message variable. Otherwise, it'll just be in the output and nothing else. There's no code in there that says, "Hey PHP, store this table into the $message variable and then send it!"

The fastest way to do this with your current code is to use output buffering, which is basically just a couple of commands that tell PHP to store output into memory instead of spitting it out right away. Once the output is in memory, you can save it to a variable that you can email out.

So to start, add:
<?php ob_start(); ?>

Open in new window

...above your <table> tag. This starts the output buffer. Any output after that line will get put into that output buffer.

Then, after your final </table> tag, add this:
<?php
$message = ob_get_flush();
?>

Open in new window

This tells PHP to finish the output buffer and flush (display) the contents, which should give you exactly what you see today, but it will ALSO store the content into a variable called $message.

If you do just that one step, you should now get emails with HTML tags in text format in your email.

2. So the next thing you have to do after that is add email headers to tell PHP to send HTML-formatted email. If you look at the PHP manual page for the mail() command, example #3 currently has the correct headers, which looks like this:
// 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";

Open in new window

Just add those headers right above your mail() command.

3. The final thing to take into consideration is that you're likely using heavy styling. It's worth noting that email clients are not exactly great at CSS styling. You might get the desired effect in Thunderbird and it'll look terrible in Outlook or vice-versa, or on Gmail. So my advice here is don't expect too much on CSS styling in HTML emails. There ARE ways of making the content look the same across all clients, but it usually means that you have to create separate sets of code for each major difference.

If you keep the HTML simple and straightforward, you should get better results.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gr8gonzoConsultantCommented:
I -know- my answer was correct unless the OP left out details about his/her situation. If the OP doesn't need the answer anymore, that's fine, but my comment should be accepted.
0
Jazzy 1012Author Commented:
Actually its not letting me send any php , I just receive a blank email
0
gr8gonzoConsultantCommented:
Hi Jasmine,

If you're interested in resolving the issue, we can continue working through it, although we need feedback from you if something doesn't work as expected. So if you want to continue getting this resolved, post your latest code after the changes I suggested.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.