Search code examples
phphtmllaraveloutputlaravel-9

How can I save the code of my generated HTML page?


Let me explain the problem :

I'm going to get my data from my local db and then send it as an email to my subscribers, but in an email I can't send within the HTML code, a php script, there is no server to interpret it. The only solution that I have is to do all of the work (get data and stuff) and generate the output of it as an HTML code, by doing so I wouldn't have any script php or any other type of scripts; and then I'm going to use that generated HTML code to send it via a Sendy's API. But my question is how can I save that generated HTML code ? After some research I found that I can use the output buffering, but I don't know how to use it and where exactly I have to start/pause it.

Here is an exemple of my HTML code(it's just the strict minimal structure that's I'm going to use) :

<!DOCTYPE html>
<html lang="en" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style type="text/css">* {
            box-sizing: border-box;
        }

        body {
            margin: 0;
            padding: 0;
        }
    </style>
</head>
<body style="background-color: transparent; margin: 0; padding: 0; -webkit-text-size-adjust: none; text-size-adjust: none;">
  <?php
    $username = "";
    $password = "";
    $hostname = "";
    $database="";
    $conn = new mysqli($servername, $username, $password, $dbname);
    $sql = "";
    $data = $conn->query($sql);
?>
<table border="0" cellpadding="0" cellspacing="0" class="nl-container" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: transparent;" width="100%">
    <tbody>
        <tr>
            <td>
                <table align="center" border="0" cellpadding="0" cellspacing="0" class="row row-1" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt;" width="100%">
                    <tbody>
                        <tr>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </tbody>
</table>
</body>
</html>

PS : I would love to do this with Laravel 9 instead of php if it's possible


Solution

  • To achieve this, you can use output buffering in the following way:

    <?php
    
    function callback($buffer)
    {
      // $buffer is a string containing your html output
      $response = some_email_send_function($buffer);
      return $response;
    }
    
    ob_start("callback");
    
    ?>
    
    <!DOCTYPE html>
    <html lang="en" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml">
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style type="text/css">* {
                box-sizing: border-box;
            }
    
            body {
                margin: 0;
                padding: 0;
            }
        </style>
    </head>
    <body style="background-color: transparent; margin: 0; padding: 0; -webkit-text-size-adjust: none; text-size-adjust: none;">
      <?php
        $username = "";
        $password = "";
        $hostname = "";
        $database="";
        $conn = new mysqli($servername, $username, $password, $dbname);
        $sql = "";
        $data = $conn->query($sql);
    ?>
    <table border="0" cellpadding="0" cellspacing="0" class="nl-container" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: transparent;" width="100%">
        <tbody>
            <tr>
                <td>
                    <table align="center" border="0" cellpadding="0" cellspacing="0" class="row row-1" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt;" width="100%">
                        <tbody>
                            <tr>
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>
    </body>
    </html>
    
    <?php
    
    ob_end_clean();
    
    ?>