how to send an email that has a html and php code message?

3

I need to send an email where the message is with HTML and PHP code, what does the code in html is to assemble the table and with the php and mysql must bring the data and put it in the table, when I only put html if it sends the msj, when I put the php I get error 500

I leave the code

<?php
// Varios destinatarios
$para  = '[email protected]';

// título
$título = 'Recordatorio ';


$enlace =  mysql_connect('192.168.1.15', 'bdas', '1452');
if (!$enlace) {
    die('No pudo conectarse: ' . mysql_error());
}
//echo 'Conectado satisfactoriamente';
mysql_select_db('aspatper_prueba') or die('No se pudo seleccionar la base de datos');
$query = 'SELECT DISTINCT per.PERSC_Nombre as nombre , per.PERSC_ApellidoPaterno as apellido_paterno ,per.PERSC_ApellidoMaterno as apellido_materno
FROM cji_paciente pa
JOIN cji_persona per ON per.PERSP_Codigo = pa.PERSP_Codigo
JOIN cji_consulta con ON con.PACI_Codigo = pa.PACI_Codigo
JOIN cji_diagnostico dg ON dg.CONS_Codigo = con.CONS_Codigo
JOIN cji_diagnostico_fase dgf ON dgf.DIAG_Codigo = dg.DIAG_Codigo
JOIN cji_emprestablecimiento eme ON eme.EESTABP_Codigo = con.COMPP_Codigo
WHERE eme.EESTABP_Codigo =  "11"
AND dgf.FASDIA_TipoDias =  "2"/* FASDIA_TipoDias=0 lunes a sabado(fase 1) FASDIA_TipoDias=1 lun-mir-vier FASDIA_TipoDias=2 mar-jue-sa FASDIA_TipoDias=3 lu a sabado */
AND dgf.FASDIA_FlagFase =  "1"
AND con.PROD_Codigo =  "30852"
AND NOT 
EXISTS (
SELECT * 
FROM cji_consulta
WHERE PACI_Codigo = pa.PACI_Codigo
AND CONS_FechaRegistro = CURDATE( )
)';
$result = mysql_query($query) or die('Consulta fallida: ' . mysql_error());

// mensaje
$mensaje = '
<html>

 <head>

<meta http-equiv="Content-Type" content="text/html"; charset="utf-8"/>
</head>

<body style="margin: 0; padding: 0;">

 <table align="center" border="1" cellpadding="0" cellspacing="0" width="600">

 <tr>

     <td align="center" bgcolor="#FFFFFF" style="padding: 40px 0 30px 0;">

     <img src="http://www.aspatperu.org.pe/AspatPeru.png" alt="Creating Email Magic" width="300" height="230" style="display: block;" />

 </td>

 </tr>

 <tr>

<td bgcolor="#ffffff">

 <table border="1" cellpadding="0" cellspacing="0" width="100%">

<tr>

 <td>

   BIENVENIDO AL SISTEMA SISBIO-TB

 </td>

</tr>   

<tr>

 <td style="padding: 20px 0 30px 0;font-size: 12px; font-family: Arial, Helvetica, sans-serif;">
    PACIENTES QUE NO ASISTIERON A SU TRATAMIENTO EL DIA DE HOY :
    <br>

<br>
<table border="1">  
      <tr>
        <th>Nombre</th>
        <th>Apellido Paterno</th>
        <th>Aperllido Materno</th>
      </tr>'
          while($row=mysql_fetch_row($result))

          {

      '<tr>
        <td>'echo $row[0]'</td>
        <td>'echo $row[1]'</td>
        <td>'echo $row[2]'</td>
      </tr>'
     }
  '</table>

 </td>

</tr>


 </table>

</td>

 </tr>



</table>

</body>


</html>
';

// Para enviar un correo HTML, debe establecerse la cabecera Content-type
$cabeceras  = 'MIME-Version: 1.0' . "\r\n";
$cabeceras .="Content-Type: text/html; charset=iso-8859-1\n";

// Cabeceras adicionales
$cabeceras .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n";
$cabeceras .= 'From: Recordatorio <[email protected]>' . "\r\n";
$cabeceras .= 'Cc: [email protected]' . "\r\n";
$cabeceras .= 'Bcc: [email protected]' . "\r\n";

// Enviarlo
mail($para, $título, $mensaje, $cabeceras);
?>
    
asked by ingswsm 18.04.2017 в 18:34
source

1 answer

7

Good afternoon.

Note that the problem you are having is that when you assemble the variable message you are including the php code in there, when in reality you should arm the beginning of the string, concatenate the contents of the table, and then add the end of the the chain.

    // mensaje
    $mensaje = '
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html"; charset="utf-8"/>
    </head>
    ...
    <th>Nombre</th>
        <th>Apellido Paterno</th>
        <th>Aperllido Materno</th>
    </tr>';
    while($row=mysql_fetch_row($result))
    {
        $mensaje .= '
        <tr>
            <td>' .$row[0].'</td>
            <td>' .$row[1].'</td>
            <td>' .$row[2].'</td>
        </tr>';
    }
    $mensaje .= '</table>
    </td>
    </tr>
    </table>...';

Please also take into account security risks when running querys, see the following link

    
answered by 18.04.2017 / 19:26
source