twan
twan

Reputation: 2659

Mail showing [contents] instead of actual content

I have a mail script that works, but I wanted to make it look a bit better with some HTML. I added the HTML, but the $_POST data is lost, and it just shows [contents] instead. The added HTML works because [contents] is in the html.

Here is a screenshot of what I see: screenshot

This is the mail script:

<?php
session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));

header('Content-type: application/json');

// Enter your email address
$to = '[email protected]';

$subject = 'Aanvraag op website door '.$_POST['name'].'';

if($to) {
    $name = $_POST['name'];
    $email = $_POST['email'];

    $fields = array(
        0 => array(
            'text' => 'Naam',
            'val' => $_POST['name']
        ),
        1 => array(
            'text' => 'Email adres',
            'val' => $_POST['email']
        ),
        2 => array(
            'text' => 'Adres',
            'val' => $_POST['adres']
        ),
        3 => array(
            'text' => 'Afleveradres',
            'val' => $_POST['afleveradres']
        ),
        4 => array(
            'text' => 'Postcode',
            'val' => $_POST['postcode']
        ),
        5 => array(
            'text' => 'Plaats',
            'val' => $_POST['plaats']
        ),
        6 => array(
            'text' => 'Tweede plaats',
            'val' => $_POST['plaats2']
        ),
        7 => array(
            'text' => 'Telefoonnummer',
            'val' => $_POST['telefoonnr']
        ),
        8 => array(
            'text' => 'Mobiel nummer',
            'val' => $_POST['mobielnr']
        ),
        9 => array(
            'text' => 'Type Raam',
            'val' => implode(',', $_POST['checkbox']) 
        ),
        10 => array(
            'text' => 'Werkzaamheden',
            'val' => implode(',', $_POST['werkzaamheden']) 
        ),
        11 => array(
            'text' => 'Contactpersoon',
            'val' => $_POST['contactpersoon']
        ),
        12 => array(
            'text' => 'Bericht',
            'val' => $_POST['message']
        )
    );




    $message = "";
    foreach($fields as $field) {
        $message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
    }

    $message = '
    <table border="0" width="100%" cellspacing="0" cellpadding="0" style="font-family:calibri;color: #5C5C5C; font-size:10pt;line-height:22px;">
    <tr>
    <td width="160" valign="top" style="font-family:calibri;padding-left:10px;padding-top:20px;">
    [contents]
    </td>
    </tr>
    <tr>
    <td width="160" valign="top" style="font-family:calibri;padding-left:10px;padding-top:20px;">
    <br><br>Met vriendelijke groet,<br><br>
    Helpdesk<br>
    <b>website</b><br>
    <p></p>
    </td>
    </tr>
    </table>
    <table height="120" border="0" width="100%" cellspacing="0" cellpadding="0" style="font-family:calibri;color: #5C5C5C; font-size:10pt;line-height:22px;">
    <tr>
    <td width="250" valign="top" style="font-family:calibri;padding-left:10px;padding-top:20px;border-top: 1px #000000 dotted; border-bottom: 1px #000000 dotted;">
    E:&nbsp;&nbsp;
    <a href="mailto:[email protected]" style="font-family:calibri;color: #5C5C5C; text-decoration: none; border-bottom: 1px #5C5C5C dotted;">[email protected]</a><br>
    T:&nbsp;&nbsp;
    <a href="tel:0615086609" style="font-family:calibri;color: #5C5C5C; text-decoration: none; border-bottom: 1px #5C5C5C dotted;">0615086609</a><br>
    W:&nbsp;&nbsp;
    <a href="http://website.nl" style="font-family:calibri;color: #5C5C5C; text-decoration: none; border-bottom: 1px #5C5C5C dotted;" target="_blank">www.website.nl</a><br>
    </td>
    <td align="right" style="font-family:calibri;padding-right:10px;padding-top:5px;border-top: 1px #000000 dotted; border-bottom: 1px #000000 dotted;">
    <a href="http://website.nl/" target="_blank" title="Ga naar de website">
    <img src="http://www.website.nl/_extern/website/images/logo.png" alt="Ga naar de website" style="font-family:calibri;text-align:right;margin:0px;padding:10px 0 10px 0;" border="0" width="232">
    </a>
    </td>
    </tr>
    <tr>
    <td colspan="2" style="font-family:calibri;color:#a3a3a3;font-size:11px;margin-top:6px;line-height:14px;">
    <br>Dit e-mailbericht is uitsluitend bestemd voor de geadresseerde. Als dit bericht niet voor u bestemd is, wordt u vriendelijk verzocht dit aan de afzender te melden. website staat door de elektronische verzending van dit bericht niet in voor de juiste en volledige overbrenging van de inhoud, noch voor tijdige ontvangst daarvan. Voor informatie over website raadpleegt u <a href="http://website.nl" style="font-family:calibri;color: #5C5C5C; text-decoration: none; border-bottom: 1px #5C5C5C dotted;" target="_BLANK">website</a>.<br><br>
    </td>
    </tr>
    </table>';

    $headers = '';
    $headers .= 'From: ' . $name . ' <' . $email . '>' . "\r\n";
    $headers .= "Reply-To: " .  $email . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

    if (mail($to, $subject, $message, $headers)){
        $arrResult = array ('response'=>'success');
    } else{
        $arrResult = array ('response'=>'error');
    }

    echo json_encode($arrResult);

} else {

    $arrResult = array ('response'=>'error');
    echo json_encode($arrResult);

}
?>

Does anyone know what I am doing wrong? The HTML is working, but the data from the foreach is not shown.

Upvotes: 0

Views: 49

Answers (2)

Kirk Beard
Kirk Beard

Reputation: 9843

You are defining the $message variable:

$message = "";

Then you are adding new items to it:

foreach($fields as $field) {
    $message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
}

Then you are replacing it with a new value:

$message = '
<table border="0" width="100%" cellspacing="0" cellpadding="0" style="font-family:calibri;color: #5C5C5C; font-size:10pt;line-height:22px;">
// Snipped to brevity...

Update your code to do something like this:

$contents = "";
foreach($fields as $field) {
    $contents .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
}

$message = '
<table border="0" width="100%" cellspacing="0" cellpadding="0" style="font-family:calibri;color: #5C5C5C; font-size:10pt;line-height:22px;">
<tr>
<td width="160" valign="top" style="font-family:calibri;padding-left:10px;padding-top:20px;">
'. $contents .'
</td>
// Snipped to brevity...

Your code did not have anyway to convert the values from the foreach to the [contents] part of your $message variable.

Upvotes: 2

sim0ne
sim0ne

Reputation: 312

Small typo maybe : $message = '<table ... instead of $message .= '<table ...

Upvotes: 0

Related Questions