Solved

Sending an email with html and php contents

Posted on 2016-09-19
6
40 Views
Last Modified: 2016-09-22
<?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.
0
Comment
Question by:Jasmine Ikhreishi
  • 3
6 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 41804921
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
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 41810870
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
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41810877
Actually its not letting me send any php , I just receive a blank email
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 41811007
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Please show me easiest post form. 3 31
Should I use subdomains or addon domains? 3 33
Update from TABLE-A to TABLE-B 5 38
How do I put a session for a radio button? 4 12
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now