drvxclagw656708070 2015-12-02 09:22
浏览 41
已采纳

邮件显示[内容]而不是实际内容

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 = 'mail@gmail.com';

$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>
";
    }

    $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:info@website.nl" style="font-family:calibri;color: #5C5C5C; text-decoration: none; border-bottom: 1px #5C5C5C dotted;">info@website.nl</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 . '>' . "
";
    $headers .= "Reply-To: " .  $email . "
";
    $headers .= "MIME-Version: 1.0
";
    $headers .= "Content-Type: text/html; charset=UTF-8
";

    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.

  • 写回答

2条回答 默认 最新

  • dougaoxian8922 2015-12-02 09:54
    关注

    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>
    ";
    }
    

    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>
    ";
    }
    
    $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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab