dotschko
dotschko

Reputation: 15

Including a for each loop of array within PHP mail as HTML table

This is the method I am attempting to use whilst trying to "send" a shopping cart to the admin. It may be an unorthodox method please any suggestions would be more than welcome.

This information is coming from a session plus a separate script which is all working well. It displays cart contents which I then am trying to send via email as well as some personal information:

<?php foreach ($quotes as $quote): ?>
  <tr>
    <td class="quoteTdL"><h3><?php echo htmlspecialchars($quote['name'], ENT_QUOTES,'UTF-8'); ?></h3></td>
    <td class="quoteTdR"><p><?php echo htmlspecialchars($quote['text'], ENT_QUOTES,'UTF-8'); ?></p></td>
  </tr>
<?php endforeach; ?>

Essentially I would like to include this information within this cut down mail script:

  //send email
  $to = "";

  $fname = $_REQUEST['fname'] ;
  $sname = $_REQUEST['sname'] ;
  $email = $_REQUEST['email'] ;
  $pnum = $_REQUEST['pnum'] ;
  $mnum = $_REQUEST['mnum'] ;
  $content = $_REQUEST['content'] ;

  $subject = 'Email from SAiGE Longlife website';

  $msg = '
  <html>
    <head>
      <title>SAiGE Longlife Decking enquiry</title>
    </head>
    <body>
      <table width="600" border="0" align="center" cellpadding="10">
        <tr> 
          <td colspan="2" align="center" bgcolor="#FF9933" style="font: 18px Arial,Georgia,Serif; color:#fff;">SAiGE Longlife Decking enquiry</td>
        </tr>
        <tr> 
          <td width="200" bgcolor="#CCCCCC" style="font: 18px Arial,Georgia,Serif; color:#fff;">Name:</td>
          <td width="400" bgcolor="#FF9933" style="font: 18px Arial,Georgia,Serif; color:#000;">';
        $msg .=$fname;
        $msg .='&nbsp;';
        $msg .=$sname;
        $msg .='
          </td>
        </tr>
        <tr> 
          <td bgcolor="#CCCCCC" style="font: 18px Arial,Georgia,Serif; color:#fff;">E-mail</td>
          <td bgcolor="#FF9933" style="font: 18px Arial,Georgia,Serif; color:#000;">';
        $msg .=$email;
        $msg .='
          </td>
        </tr>
        <tr> 
          <td bgcolor="#CCCCCC" style="font: 18px Arial,Georgia,Serif; color:#fff;">Phone numbers:</td>
          <td bgcolor="#FF9933" style="font: 18px Arial,Georgia,Serif; color:#000;">';
        $msg .=$pnum;
        $msg .='
          </td>
        </tr>
        <tr> 
          <td bgcolor="#CCCCCC">&nbsp;</td>
          <td bgcolor="#FF9933" style="font: 18px Arial,Georgia,Serif; color:#000;">';
        $msg .=$mnum;
        $msg .='
          </td>
        </tr>
        <tr> 
          <td colspan="2">&nbsp;</td>
        </tr>
        <tr> 
          <td colspan="2" bgcolor="#CCCCCC" style="font: 18px Arial,Georgia,Serif; color:#fff;">Message:</td>
        </tr>
        <tr> 
          <td colspan="2" bgcolor="#FF9933" style="font: 18px Arial,Georgia,Serif; color:#000;">';
        $msg .=$content;
        $msg .=$cartHtml;
        $msg .='

          </td>
        </tr>
      </table>
    </body>
    </html>
    ';

I have tried imploding and integrating the foreach side of things but to no luck and have spent a long time on it!

Is it possible to output the loop first as a variable and then include or is this unnecessary?

All help would be appreciated.

Many thanks,

Tom

Upvotes: 0

Views: 2556

Answers (3)

dotschko
dotschko

Reputation: 15

For love nor money I could not get the ob_start solution to work. I tried it numerous times before posting this question. All I can imagine is that posting to another page clears the buffer and so there is no data to be echoed?

This was the final code I used for the mail.

Message: '; $msg .=$content;

    foreach ($quotes as $quote):

      $name = htmlspecialchars($quote['name'], ENT_QUOTES,'UTF-8');
      $text = htmlspecialchars($quote['text'], ENT_QUOTES,'UTF-8');

    $msg .= '
        <tr>
          <td class="quoteTdL"><h3>';

    $msg .= $name;

    $msg .= ' 
    </h3></td>
          <td class="quoteTdR"><p>';

    $msg .=$text;

    $msg .= '</p></td>
        </tr>';

      endforeach;
    $msg .='

      </td>
    </tr>

Hope this helps,

Tom

Upvotes: 0

Yes, it is possible to output the loop first. You need to call output buffering functions:

ob_start();
echo "Some output here";
$out = ob_get_clean();

The $out variable will contain the output you need

Upvotes: 0

Jeff Parker
Jeff Parker

Reputation: 7517

Quick example attached.

foreach ($quotes as $quote):

    $name = htmlspecialchars($quote['name'], ENT_QUOTES,'UTF-8');
    $text = htmlspecialchars($quote['text'], ENT_QUOTES,'UTF-8');

    $msg .= <<<EOS
  <tr>
    <td class="quoteTdL"><h3>{$name}</h3></td>
    <td class="quoteTdR"><p>{$text}</p></td>
  </tr>
EOS;
endforeach;

// -- The output has now been added to $msg.

Upvotes: 1

Related Questions