Insert HTML page in email via php

2

Good morning, I have a doubt. I am trying to send a page in html via mail, when executing a function in php, instead of directly placing the html inside the function.

To see a clear example of what I need would be something like this: Button assigned to the send mail function that the body of that mail has the content, including the css, of an html page.

I just want to remove the html from the message and put a kind of reference to a web, take this content and put it in the mail

$para  = '[email protected]';
 
// Asunto
$titulo = 'NUEVO PEDIDO';
 
// Cuerpo o mensaje
$mensaje = '
<html>
<head>
  <title>Hay un nuevo pedido</title>
</head>
<body>
  <h2>Hay un nuevo pedido</h2>
	<a href="https://www.miweb.com/pedidos/last.php">CLICK AQUI</a>
</body>
</html>
';
 
$cabeceras  = 'MIME-Version: 1.0' . "\r\n";
$cabeceras .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 

$cabeceras .= 'From: Recordatorio <[email protected]>' . "\r\n";

mail($para, $titulo, $mensaje, $cabeceras);
    
asked by Tefef 17.01.2018 в 16:46
source

1 answer

1

Since the content you want to include is in the same domain, it would be best to use the include() function from PHP, which exists for those purposes. Then, with ob_get_contents() you can save the contents of the file included in a variable to send it in the email.

The code would be like this:

Script to send the email

$para  = '[email protected]';

// Asunto
$titulo = 'NUEVO PEDIDO';

// Cuerpo o mensaje

ob_start();
 /*
    *Si last.php y el script de email no están en el mismo directorio
    *deberás indicar en el include la ruta correcta de last.php
 */
include "last.php";
$mensaje = ob_get_contents();
ob_end_clean();

$cabeceras  = 'MIME-Version: 1.0' . "\r\n";
$cabeceras .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";   
$cabeceras .= 'From: Recordatorio <[email protected]>' . "\r\n";    
$bolEnviar=mail($para, $titulo, $mensaje, $cabeceras);

if($bolEnviar){

    echo "Mensaje enviado exitosamente";

}else{

    echo "Hubo un error ".error_get_last()['message'];
}

last.php

<!DOCTYPE html>
<html lang="es">
    <head>
        <title>Hay un nuevo pedido</title>  
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
      <h2>Hay un nuevo pedido</h2>
    <?php

    /*Código PHP del archivo*/

    ?>
    </body>
</html>
    
answered by 17.01.2018 / 17:37
source