Solved

Send email automatically with same appearance using php

Posted on 2016-09-16
8
40 Views
Last Modified: 2016-09-22
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!
0
Comment
Question by:Jasmine Ikhreishi
8 Comments
 
LVL 6

Expert Comment

by:Mukesh Yadav
ID: 41801442
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
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41801460
It doesnt display anything in the email Just the header which is hi
0
 
LVL 35

Expert Comment

by:Kimputer
ID: 41801490
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
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41801516
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41804655
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
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41804723
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
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41804733
Yes a blank message, so what do I need to do from there
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41804772
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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to dynamically set the form action using jQuery.
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.

705 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

20 Experts available now in Live!

Get 1:1 Help Now