Send data of a query by email with html format

0

Someone could help me put my query data in a table, the email is sent to me but it does not show me the stored data.

 <?php
    $conect = new mysqli("localhost", "onethost_horacio", "MPADT-2018", "onethost_MPA");
    /*$v3=$_POST['codigo'];*/
    //$sql = "SELECT * from InformacionGeneral WHERE Delegacion like '%$v3%'";
    $sql = "SELECT * from InformacionGeneral";
    $resultado=mysqli_query($conect,$sql);
    while($fila=mysqli_fetch_assoc ($resultado)){
    ?>   
        <tr>
        <td><?php $idgeneral=$fila["Id_General"]?>
        <td><?php $delegacion=$fila["Delegacion"]?>
        <td><?php $parroquia=$fila["Parroquia"]?>
        <td><?php $tios=$fila["Tios_Rensponsables"]?>
        <td><?php $asesor=$fila["Asessor_Espiritual"]?>
        <td><?php $fecha=$fila["Fecha"]?>
        <td><?php $tiosresponsables=$fila["Tios_apoyo"]?>
         </tr>
    <?php
    $mensaje1 = "<html>
        <head><title>Email con HTML</title></head>
        <body><h1>Email con HTML</h1>
        Esto es un email que se envía en el formato HTML
        <hr>
        Enviado por mi programa en PHP
        <hr>
        <table>
        <tr>
          <th>Id General</th><th>Delegacion</th><th>Parroquia</th><th>Tios</th><th>Asesor</th><th>Fecha</th><th>Tios Responsables</th>
        </tr>
        <tr>
          <td><?php echo $idgeneral.;?></td><td><?php echo $delegacion.;?></td><td><?php echo $parroquia.;?></td><td><?php echo $tios.;?></td><td><?php echo $asesor.;?></td><td><?php echo $fecha.;?></td><td><?php echo $tiosresonsables.;?></td>
        </tr>
        </table>
        </body>
        </html>";

    ////////////////////////////
    $asunto = "Datos de usuario";
    ///////////////////////////
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    //////////////////////////////////
    $headers .= "From: Jose Luis Santiago < [email protected] >\r\n";
    ///////////////////////////////////////////////
    $para='[email protected]';
    /////////////////////////////////

    }
    $bool = mail($para,$asunto,$mensaje1,$headers);
    ?>
    
asked by Jose Luis Santiago Pacheco 03.09.2018 в 04:18
source

2 answers

1

The problem lies in the way you are generating the html because you can not include the PHP tags within a variable, so that it works correctly, you would have to implement your code in the following way:

<?php
    $conect = new mysqli("localhost", "onethost_horacio", "MPADT-2018", "onethost_MPA");
    /*$v3=$_POST['codigo'];*/
    //$sql = "SELECT * from InformacionGeneral WHERE Delegacion like '%$v3%'";
    $sql = "SELECT * from InformacionGeneral";
    $resultado=mysqli_query($conect,$sql);
    $contenido = '';
    while($fila=mysqli_fetch_assoc ($resultado))
    {
        $idgeneral         = $fila["Id_General"];
        $delegacion        = $fila["Delegacion"];
        $parroquia         = $fila["Parroquia"];
        $tios              = $fila["Tios_Rensponsables"];
        $asesor            = $fila["Asessor_Espiritual"];
        $fecha             = $fila["Fecha"];
        $tiosresponsables  = $fila["Tios_apoyo"];

        $contenido .= '
         <tr>
           <td> '.$idgeneral.' </td>
           <td> '.$delegacion.'  </td>
           <td> '.$parroquia.'  </td>
           <td> '.$tios.'  </td>
           <td> '.$asesor.'  </td>
           <td> '.$fecha.'  </td>
           <td> '.$tiosresponsables.' </td>
         </tr>';

    }

    $mensaje1 = "
      <html>
        <head><title>Email con HTML</title></head>
        <body><h1>Email con HTML</h1>
        Esto es un email que se envía en el formato HTML
        <hr>
        Enviado por mi programa en PHP
        <hr>
        <table>
        <tr>
          <th>Id General</th><th>Delegacion</th><th>Parroquia</th><th>Tios</th><th>Asesor</th><th>Fecha</th><th>Tios Responsables</th>
        </tr>
        ".$contenido."
        </table>
        </body>
        </html>";
    ////////////////////////////
    $asunto = "Datos de usuario";
    ///////////////////////////
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    //////////////////////////////////
    $headers .= "From: Jose Luis Santiago < [email protected] >\r\n";
    ///////////////////////////////////////////////
    $para='[email protected]';
    /////////////////////////////////

    $bool = mail($para,$asunto,$mensaje1,$headers);
    ?>

I hope my answer has been useful, if it was so do not forget to leave your positive rating.

    
answered by 03.09.2018 / 04:45
source
2

You have a spelling error in the code, after the while you are assigning a variable, tiosresponsables , and in mensaje1 you are calling a different one, tiosresonsables (missing the P ). Besides that, the code could be better structured, such as eliminating the HTML tags in the assignments after the while because they are not necessary and also eliminating the echo that are inside mensaje1 since it is a PHP variable, therefore not he needs them. The improved code would look like this:

<?php
    $conect = new mysqli("localhost", "onethost_horacio", "MPADT-2018", "onethost_MPA");
    $sql = "SELECT * from InformacionGeneral";
    $resultado=mysqli_query($conect,$sql);
    while($fila=mysqli_fetch_assoc($resultado)){
        $idgeneral=$fila["Id_General"];
        $delegacion=$fila["Delegacion"];
        $parroquia=$fila["Parroquia"];
        $tios=$fila["Tios_Rensponsables"];
        $asesor=$fila["Asessor_Espiritual"];
        $fecha=$fila["Fecha"];
        $tiosresponsables=$fila["Tios_apoyo"];

        $mensaje1 = "<html>
        <head><title>Email con HTML</title></head>
        <body><h1>Email con HTML</h1>
        Esto es un email que se envía en el formato HTML
        <hr>
        Enviado por mi programa en PHP
        <hr>
        <table>
        <tr>
          <th>Id General</th><th>Delegacion</th><th>Parroquia</th><th>Tios</th><th>Asesor</th><th>Fecha</th><th>Tios Responsables</th>
        </tr>
        <tr>
          <td>$idgeneral</td><td>$delegacion</td><td>$parroquia</td><td>$tios</td><td>$asesor</td><td>$fecha</td><td>$tiosresponsables</td>
        </tr>
        </table>
        </body>
        </html>";

    ////////////////////////////
    $asunto = "Datos de usuario";
    ///////////////////////////
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    //////////////////////////////////
    $headers .= "From: Jose Luis Santiago < [email protected] >\r\n";
    ///////////////////////////////////////////////
    $para='[email protected]';
    /////////////////////////////////

    }
    $bool = mail($para,$asunto,$mensaje1,$headers);
    ?>
    
answered by 03.09.2018 в 04:40