Search code examples
phphtmlforeachhtml-tableinline-styles

PHP foreach adding random gaps/whitespace


I have the following PHP. Here I am creating a table, looping through the keys and values of my incoming data, adding them to the rows. When receiving the mail, it is adding random gaps/whitespaces in the inline styles, for example:

see bord er:4px solid #E4F5FD;

<td valign="top" style="padding:5px;border-collapse:collapse;bord er:4px solid #E4F5FD;text-transform:capitalize;font-family:arial;font-size:12px;background:#FFFFFF;">

and

see background:#FFF FFF;

padding:5px;border-collapse:collapse;border:4px solid #E4F5FD;text-transform:capitalize;font-family:arial;font-size:12px;background:#FFF  FFF;color:#888888;

I have no idea why it is doing this, has anyone encountered this problem before?

Please keep in mind I am learning PHP, so be kind.

$data_output .= '<table align="center" border="0" cellspacing="0" cellpadding="0" style="border-collapse:collapse;background:#E4F5FD;border:0;width:680px;">';

    foreach ($data_html as $key => $value) {

        $data_output .= '<tr>';
        $data_output .= '<td valign="top" style="padding:5px;border-collapse:collapse;border:4px solid #E4F5FD;text-transform:capitalize;font-family:arial;font-size:12px;background:#FFFFFF;"><b>'.str_replace('_',' ',$key).'</b></td>';

        $values = '';
        foreach($value as $v) {
          $values .= $v.'<br />';
        }

        $z = '';
        if($values == ''){
            $z = $value;
        } else {
            $z = $values;
        }

        $data_output .= '<td valign="top" style="padding:5px;border-collapse:collapse;border:4px solid #E4F5FD;text-transform:capitalize;font-family:arial;font-size:12px;background:#FFFFFF;color:#888888;">'.str_replace('_',' ',$z).'</td>';
        $data_output .= '</tr>';
    }

    $data_output .= '</table>';


$from = 'website@bla.clo.uk';
$to = $_POST['email'];
$cc = 'me@bla.co.uk';
$subject = 'bla stuff';
$body = $data_output;

$headers = '';
$headers .= "From: $from\n";
$headers .= "Cc: $cc\r\n";
$headers .= "Reply-to: $from\n";
$headers .= "Return-Path: $from\n";
$headers .= "Message-ID: <" . md5(uniqid(time())) . "@" . $_SERVER['SERVER_NAME'] . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\n";
$headers .= "Date: " . date('r', time()) . "\n";

if (mail($to, $subject, $body, $headers)){
    echo 'sent';
} else {
    echo 'fail';
    die();
}

Solution

  • Make sure that each line of content does not exceed 78 characters in length, nor that you exceed 998 continuous characters.

    This was the issue, I added "\n" at the end of each row and cell to break the line content.

    foreach ($data_html as $key => $value) {
            $data_output .=
            '<tr>'."\n".
            '<td style="border:4px solid #DADADA;background:#FFFFFF;text-transform:capitalize;">' . str_replace('_',' ',$key) . '</td>'."\n".
            '<td style="border:4px solid #DADADA;background:#FFFFFF;">' . $value . '</td>'."\n".
            '</tr>'."\n";
        }