Send email automatically with same appearance using php

I have this code:
<?php 
error_reporting(E_ALL);
ini_set('display_errors' ,1);
require "connection.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;
    }
    </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>

Open in new window


This code displays a report of the id, rows and insurance; I want to be able for it to automatically send an email daily to me with the same content and the same style it is shown on the table. I tried this:

<?php

require "connection.php";


  if (isset($_REQUEST['email']))  {


  $admin_email = "admin@hotmail.com";
  $email = "jasmine@hotmail.com";  //my email
  $subject = 'Date';

  //send email
  mail($admin_email, "$subject", $comment, "From:" . $email);



  //if "email" variable is not filled out, display the form

?>
<?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;
}
</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
  }
?>

Open in new window


However, it doesn't even send me an email at all. I'm not sure what to do or how to fix it. Thanks in advance!
LVL 1
Jazzy 1012Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Julian HansenConnect With a Mentor Commented:
I don't understand what you are doing here.
Lines 9 - 14 you send a blank email.
Then you build the template after the email has been sent?
Here is how you build an email
1. Create the content and save it to a variable
2. Establish who are sending to
3. call the mail() function with the above information

The mail command expects this

mail($to, $subject, $message)

You are sending $comment as the message but you are not defining it anywhere.

Suggestion:
First establish if you can send email
Create this script and load it in your browser - do you get the email
<?php
// CHANGE TO YOUR EMAIL
$to = "jasmine@hotmail.com";
$subject = "Testing email functionality";
$message = "This is a test to see if mail is working";

mail($to, $subject, $message);

Open in new window

If that does not work then you have a problem with how mail is configured on your server.

If it does work then I would suggest you use PHPMailer https://github.com/PHPMailer/PHPMailer
To send your report above would look something like this
<?php
require "connection.php";
require 'PHPMailerAutoload.php';
// GET THE email PARAMETER
$email = !empty($_REQUEST['email']) ? $_REQUEST['email'] : false;

// CHECK IT IS A VALID EMAIL
$email = filter_var($email, FILTER_SANITIZE_EMAIL);

// IF IT IS VALID
if ($email)  {
  // CREATE A new PHPMailer OBJECT
  $mail = new PHPMailer;
  
  // SET THE ADDRESS INFORMATION
  $mail->setFrom('jasmine@hotmail.com','Jasmine Ikhreishi'); // Change to what you want people to see in the From field
  $mail->addAddress($email);
  $mail->addAddress('admin@hotmail.com'); // If you want to send a copy somewhere
  
  // SET THE SUBJET
  $mail->Subject = 'Sample email from website';
  
  // LOAD THE TEMPLATE
  ob_start();
  include ("html_template.php");
  
  // SET THE Body TO THE CONTENT GENERATED BY THE TEMPLATE
  $mail->Body = ob_get_clean();
  // THIS IS OPTIONAL BUT USEFUL FOR IMPROVING SPAM SCORE
  // YOU CAN USE A DIFFERENT TEMPLATE (AS ABOVE) OR SIMPLY
  // HAVE A STATIC MESSAGE
  $mail->AltBody = "Set text only body here";
  
  // SEND AND DEAL WITH RESULT
  if (!$mail->send()) {
    // HANDLE SEND ERROR HERE
  }
  else {
    // HANDLE SUCCESS HERE
  }
}
// HANDLE INVALID EMAIL HERE

Open in new window

Notes
1. You will need to install PHPMailer from the link above
2. You need to change the path to the PHPMailerAutoload.php - if it is not in the root folder of your site. I recommend you create a subfolder for it and then change the path to point to the subfolder.
3. The body is set from a PHP template - put all the code you want to create the message with in the a PHP and make sure that the include ("html_template.php"); line points to that file.

That should be all that is required.
0
 
Mukesh YadavFull Stack DeveloperCommented:
Try this ;)

Replace
mail($admin_email, "$subject", $comment, "From:" . $email);

Open in new window

with
mail($email, $subject, $comment, "From:" . $admin_email);

Open in new window

0
 
Jazzy 1012Author Commented:
It doesnt display anything in the email Just the header which is hi
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
KimputerCommented:
So email is sent, that's at least something. was the content of $comment in this case really "hi", then the code is working on the basic level and we can continue with the next problem. If the $comment was not "hi", then we still have a problem.

It's better you do an echo $comment before sending out the email, therefore you know if the email intended to be sent is also what you receive.
0
 
Jazzy 1012Author Commented:
It sends me a blank email, is there a way to send these without installing anything, through just manual code ? Or do i need to install ??
0
 
Julian HansenCommented:
Is this what sent you a blank message?
<?php
// CHANGE TO YOUR EMAIL
$to = "jasmine@hotmail.com";
$subject = "Testing email functionality";
$message = "This is a test to see if mail is working";

mail($to, $subject, $message);

Open in new window

You should not need to install anything but your PHP must be configured to find an SMTP server to send the email through. If you are hosting this site then your ISP should be able to confirm whether or not this is enabled for your hosting package. If this is your own server you need to configure your PHP.INI file with the correct SMTP settings.

Please confirm if the above code results in an email that you receive.
0
 
Jazzy 1012Author Commented:
Yes a blank message, so what do I need to do from there
0
 
Julian HansenCommented:
Firstly you need to stop opening up new questions - you are only going to confuse the issue. You have opened this question
https://www.experts-exchange.com/questions/28970759/Sending-an-email-with-html-and-php-contents.html

Which is essentially dealing with the same issue.

If you are getting a blank message then it is not working - you should get an email with the following text in the body
This is a test to see if mail is working

Open in new window

Until you solve that problem we cannot move forward.
Are you with a hosting company or is this your own server that you setup and configured?
0
All Courses

From novice to tech pro — start learning today.