2015-12-02 89 views
0

我有一個可用的郵件腳本,但是我想讓它看起來更好用一些HTML。我添加了HTML,但$_POST數據丟失了,它只是顯示[contents]。添加的HTML工作原因是[contents]在html中。顯示[內容]而不是實際內容的郵件

這裏是我所看到的截圖:screenshot

這是郵件腳本:

<?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); 

} 
?> 

有誰知道我做錯了嗎? HTML正在工作,但未顯示來自foreach的數據。

+1

你有[內容]字符串,你期望發生什麼? – Nick

+0

是的,我忘了將它添加到原始文章中,但我現在編輯它。 – twan

回答

2

您所定義的$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;"> 
// Snipped to brevity... 

更新您的代碼做這樣的事情:

$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... 

您的代碼並沒有無論如何都要值從foreach轉換爲您$message變量[內容]部分。

+0

是啊,現在這麼簡單,我想起它..謝謝你的答案! – twan

0

小錯字可能:的$message .= '<table ...$message = '<table ...代替