Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sending an email with html and php contents

Posted on 2016-09-19
6
Medium Priority
?
70 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 2000 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 35

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 35

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

688 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