PHP数组到HTML表格的电子邮件

时间:2018-04-25 18:06:59

标签: php html email

我尝试将PHP数组中的数据放入HTML表格并使用它发送电子邮件。我似乎无法将数组与HTML结合使用。

我想创建一个两列宽的表,其下面有字段数组详细信息。 我试图使用一个专家,但没有成功。

 $subject = 'Nieuwe reservering';

$fields = array('kosten' => 'kosten' , 'name' => 'Naam', 'surname' => 'Achternaam', 'phone' => 'Telefoonnummer', 'kamer' => 'Kamer', 'aankomst' => 'aankomst', 'vertrek' => 'vertrek', 'email' => 'Email', 'message' => 'Bericht');

$okMessage = 'Je bericht is verstuurd, dankjewel!';

$errorMessage = 'Oei er ging iets fout, geeft niks. Probeer het later opnieuw.';

error_reporting(E_ALL & ~E_NOTICE);

try
{
    if(count($_POST) == 0) throw new \Exception('Form is empty');

    foreach ($_POST as $key => $value) {
        if (isset($fields[$key])) {
            $emailText .="<table>";
            $emailText .= "<td>" "$fields[$key]: $value\n" "</td>";
            $emailText .="</table>";
        }
    }

    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers = array('Content-type:text/html;charset=UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
    );

    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}

1 个答案:

答案 0 :(得分:0)

您可能想要更改此内容:

foreach ($_POST as $key => $value) {
        if (isset($fields[$key])) {
            $emailText .="<table>";
            $emailText .= "td" "$fields[$key]: $value\n" "</td>";
            $emailText .="</table>";
        }
    }

致:

$emailText ="<table>";
foreach ($_POST as $key => $value) {
        if (isset($fields[$key])) {
            $emailText .= "<tr><td>{$fields[$key]}: $value</td></tr>";
        }
    }
$emailText .="</table>";

或者,如果您希望所有这些人都在同一个<tr>

$emailText ="<table><tr>";
foreach ($_POST as $key => $value) {
     if (isset($fields[$key])) {
          $emailText .= "<td>{$fields[$key]}: $value</td>";
        }
     }
$emailText .="</tr></table>";