0

I have an email (html) template loaded from db, which contains foreach loop (or may be some other PHP operation), to dynamically load/process content of API response.

$content = html_entity_decode($template); //$template loaded from db

$content's html

<p style="text-align:center">Hi {name},</p>
<p style="text-align:center">This is header</p>
<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Qty</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($response as $item){ ?>
        <tr>
            <td><img src="<?php echo $item['img']; ?>" atl="<?php echo $item['name']; ?>" /></td>
            <td><?php echo $item['name']; ?></td>
            <td><?php echo $item['qty']; ?></td>
            <td><?php echo $item['price']; ?></td>
        </tr>
        <?php } ?>
    </tbody>
</table>

Response of API

$response = array(
    0 => array(
        'name' => 'abc',
        'img'  => 'linkToImg',
        'price'=> '12',
        'qty'  => 10
    ),
    1 => array(
        'name' => 'xyz',
        'img'  => 'linkToImg',
        'price'=> '15',
        'qty'  => 2
    ),
);

Before sending email, I replace token using str_replace('{name}', $name, $content); , But I have no idea, how can i execute the foreach (or may be some other PHP operation) of $content to populate all dynamic data of $response and then send email.

My Try

I tried using token for $response as well, but,that approach is not good if I need multiple designs of $template and multiple response of API (for other purpose email).For example, in this template, I have Table for products but in other template i need <ul> <li> with inline style for listing some features etc.

Please suggest me how to achive this.

PS: I am using TinyMCE in frontEnd to design email template and store in DB.

Michael Fromin
  • 13,131
  • 2
  • 20
  • 31

2 Answers2

1

I found the solution.

In short, I need to use Output Buffering e.g. ob_start();

Explanition

Step 1: I store the template in .html file.

Step 2: After the response from API, put ob_start();

Step 3: Include template using include('path/to/file.html')

Step 4: Get the output of buffer and send as Email Message.

My Code

//Consider HTML template in question is saved as template.html

//Response from API 
$response = array(
    0 => array(
        'name' => 'abc',
        'img'  => 'linkToImg',
        'price'=> '12',
        'qty'  => 10
    ),
    1 => array(
        'name' => 'xyz',
        'img'  => 'linkToImg',
        'price'=> '15',
        'qty'  => 2
    ),
);

ob_start();
include('path/to/template.html');
$message = ob_get_clearn();

//now use $message in sending email
$this->emnail->message($message);

ob_start() will turn on the Buffer and hold the output of HTML, execute the PHP inside it and then ob_get_clean() return the output to $message and clean the buffer.

Benefit

By this approach, I can use any html template in sendEmail(). I only need to design Email Templates, rest thing will be done by Output Buffer

0

From what I understand is that you want to add more information/data to your email template.


Solution #1 Assuming that the "$content" have the same message you want to include in your template you can do something like this:

<?php
$content = " Thank You!!!";   // some data
foreach($response as $item){ ?>
        <tr>
            <td><img src="<?php echo $item['img']; ?>" atl="<?php echo $item['name']; ?>" /></td>
            <td><?php echo $item['name']; ?></td>
            <td><?php echo $item['qty']; ?></td>
            <td><?php echo $item['price']; ?></td>
            <td><?php echo $content; ?></td>  // just add the variable.
        </tr>
        <?php } ?>

Solution #2: Assuming that the $content itself is an array then you might wanna use :

foreach (array_combine($courses, $sections) as $course => $section)

Check the post for more details on foreach loop on multiple arrays: Multiple index variables in PHP foreach loop hope it helps

Ahsan Aman
  • 56
  • 1
  • 4