Solved

Sending an email with html and php contents

Posted on 2016-09-19
6
36 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

9 Experts available now in Live!

Get 1:1 Help Now